public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: "Alexandre François Garreau" <galex-713@galex-713.eu>
Cc: libc-help@sourceware.org
Subject: Re: malloc’ing strcat
Date: Tue, 25 Feb 2020 09:41:00 -0000	[thread overview]
Message-ID: <87h7zfhulx.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <1920232.nxOQ8X6Npi@galex-713.eu> ("Alexandre =?utf-8?Q?Fran?= =?utf-8?Q?=C3=A7ois?= Garreau"'s	message of "Tue, 25 Feb 2020 04:57:33 +0100")

* Alexandre François Garreau:

>> I think I wouldn't use the NULL sentinel for the reasons indicated
>> (silent truncation after memory allocation or other failure, even under
>> valgrind), and use
>> 
>>   sizeof ((const char *[]) { __VA_ARGS__ }) / sizeof (const char *)
>> 
>> to compute the number of arguments, and pass that separately from the
>> pointer.
>
> you mean like that?
> #define anstrcat(...) \
> astrncat(sizeof((char *[]) {__VA_ARGS__}), \
>                 (char *[]) {__VA_ARGS__})
> char * anstrcat (size_t n; char *strarray[])
> {
>    char * result = malloc(1);
>    size_t len = 1;
>    result[0] = '\0';
>    for (off_t i = 0; i < n; ++i)
>    result = strcat(realloc(result, len += strlen(strarray[i])), 
> strarray[i]);
>    return result;
> }

Sort of, except with overflow checking and without the quadratic
behavior.

> Yet this only works with a macro, and is it really unreasonable to expect 
> not to give NULL args?

I think so.

But there is another complication: The NULL sentinel is not portable
because NULL can be defined as 0, which does not have the correct type
for use in an argument list of character pointers.

Thanks,
Florian

  reply	other threads:[~2020-02-25  9:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-24  2:58 Alexandre François Garreau
2020-02-24 11:17 ` Florian Weimer
2020-02-24 12:31   ` Alexandre François Garreau
2020-02-24 12:38     ` Florian Weimer
2020-02-24 20:23       ` Alexandre François Garreau
2020-02-24 21:17         ` Florian Weimer
2020-02-25  3:58           ` Alexandre François Garreau
2020-02-25  9:41             ` Florian Weimer [this message]
2020-02-25 12:48               ` Alexandre François Garreau
2020-02-25 12:53                 ` Florian Weimer
2020-02-25 13:04                   ` Alexandre François Garreau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h7zfhulx.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=galex-713@galex-713.eu \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).