public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Alejandro Colomar (man-pages)" <alx.manpages@gmail.com>
To: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: printf_arginfo_size_function error handling
Date: Wed, 24 Mar 2021 20:39:43 +0100	[thread overview]
Message-ID: <865294a1-542d-3ed2-314d-c8abf9b5df12@gmail.com> (raw)
In-Reply-To: <da225884-112d-0f66-91af-a1ad42266147@gmail.com>

Hi,

On 3/24/21 8:30 PM, Alejandro Colomar (man-pages) wrote:
> Hi,
> 
> On 3/24/21 5:39 PM, Alejandro Colomar (man-pages) wrote:
>> Hi,
>>
>> I've been re-reading the docs for printf_register_specifier(), and
>> there's something in an example code that doesn't make much sense:
>>
>> <https://www.gnu.org/software/libc/manual/html_node/Printf-Extension-Example.html>
>> [[
>>
>> int
>> print_widget_arginfo (const struct printf_info *info, size_t n,
>>                       int *argtypes)
>> {
>>   /* We always take exactly one argument and this is a pointer to the
>>      structure.. */
>>   if (n > 0)
>>     argtypes[0] = PA_POINTER;
>>   return 1;
>> }
>>
>> ]]
>>
>> In the code above there is a check that n>0, but:
>>
>> What can a user do if n<=0 ?  There's no error reporting method, is there?
>> And, is it possible for such a case to happen?  What to do there?
>>
>> I think either using an assert() or not checking at all might make more
>> sense (depending on how possible is n<=0 to happen).  The current check
>> continues as if everything was fine, just without setting argtypes[0],
>> so if there's an error, it will be carried to some future stage, where
>> nasal daemons might unexpectedly happen.
> 
> After reading the sources, I see a few things:
> 
> - There's an undocumented error handling mechanism:  you can return a
> negative value to signify an error.  However, I couldn't understand what
> exactly happens when you report an error.  Does the printf_function get
> called?
> 
> - n seems to be nonnegative:  it's called in three places
> (<stdio-common/vfprintf-internal.c:1821>,
> <stdio-common/printf-parsemb.c:316>, and stdio-common/printf-prs.c:94),
> and all of them make sure n is positive, so the test in the example is
> useless.
> 
> - I could't understand how n>1 could be used from reading the
> (obfuscated) sources.  And there's no documentation.  Is it really
> supported?  And how?  Would you mind documenting that?  Maybe with an
> example that makes use of the non-obvious features of this API.
> 
> - Even though printf_arginfo_size_function supposedly handles the case
> of n>1, printf_function doesn't seem to handle it.  It gets the array of
> arguments, but it doesn't get the size of the array, so the only valid
> assumption is that the array is of size 1; anything else might fail
> silently.  If you use the misterious feature of multiple arguments for a
> single specifier, how do you guarantee that you'll receive as many slots
> in the array as you want?  By checking it in the arginfo function and
> reporting an error if n is not big enough?  Will that guarantee that
> printf_function is not called?  Again, I see no documentation at all
> about this, and the sources are not the easiest to read.
> 

If I understood correctly, the 3rd parameter argtypes of
printf_arginfo_size_function is an array, right?

So a more friendly prototype would be

typedef
int printf_arginfo_size_function(const struct printf_info *info,
                                 size_t n,
                                 int argtypes[static n],
                                 int *size);

Also, what about the last 'size' argument of
printf_arginfo_size_function?  Is it an array or just a pointer?  If
it's a pointer, how do you pass the size of each of the arguments you
set in argtypes?  Is the following prototype correct (I'm guessing)?:

typedef
int printf_arginfo_size_function(const struct printf_info *info,
                                 size_t n,
                                 int argtypes[static n],
                                 int size[static n]);

Thanks,

Alex

-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

      reply	other threads:[~2021-03-24 19:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-24 16:39 Alejandro Colomar (man-pages)
2021-03-24 19:30 ` Alejandro Colomar (man-pages)
2021-03-24 19:39   ` Alejandro Colomar (man-pages) [this message]

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=865294a1-542d-3ed2-314d-c8abf9b5df12@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=libc-alpha@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).