:: [DNG] C coding subtlleties
Forside
Slet denne besked
Besvar denne besked
Skribent: Hendrik Boom
Dato:  
Til: dng
Gamle-emner: Re: [DNG] Systemd Shims
Emne: [DNG] C coding subtlleties
On Fri, Aug 21, 2015 at 04:10:19PM +0100, Rainer Weikusat wrote:
> Hendrik Boom <hendrik@???> writes:
>
> > On Fri, Aug 21, 2015 at 12:51:55PM +0100, Rainer Weikusat wrote:
> >>
> >> That's going to work with this particular problem which you incorrectly
> >> (the original path wasn't a macro) reduced to appending a string of
> >> unknown length to a constant string. Taking this into account, a
> >> solution without snprintf would become something like
> >>
> >> #define PATH "/tmp/"
> >>
> >> char *p;
> >>
> >> p = alloca(sizeof(PATH) + strlen(argv[1]));
> >
> > Should that be
> > p = alloca(sizeof(PATH) + strlen(argv[1]) + 1);
> >
> >> sprintf(p, "%s%s", PATH, argv[1]);
> >
> > so there's space for the terminating '\0'?
>
> The size of a string literal includes the terminating 0.


Ah! Of course. I missed that PATH was a strin literal. Of course, it
PATH had expanded to a char* expression, my answer would have been even
more wrong. It would have to be another strlen instead of sizeof.

-- hendrik
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng