[ofa-general] Re: [PATCH v2] Update mad formatting functions.

Jason Gunthorpe jgunthorpe at obsidianresearch.com
Thu Apr 16 09:53:11 PDT 2009


On Thu, Apr 16, 2009 at 12:16:43AM -0700, Ira Weiny wrote:
> Ok, v2 has a couple of changes.
> 
>    1) implements the mad_vsnprintf with vsnprintf.
>    2) change formatting char to 'm' since "F" is floating point
>    3) add 'M' for printing the "name" of the field specified.
> 
> The reason I did not use vsnprintf before was because of this statement in the
> vsnprintf man page.
> 
>        The functions vprintf(), vfprintf(), vsprintf(), vsnprintf() are equiv-
>        alent  to  the  functions  printf(),  fprintf(), sprintf(), snprintf(),
>        respectively, except that they are called with a va_list instead  of  a
>        variable  number  of  arguments. These functions do not call the va_end
>        macro. Consequently, the value of ap is undefined after the  call.  The
>               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>        application should call va_end(ap) itself afterwards.
> 
> I have made a comment in the patch where I am unsure of the call.  This seems
> to work just fine on my Linux systems with gcc.  Will this work on other
> systems/compilers?

The rules for va's are funny, what the above is saying is that there
is no guarentee what va_arg(ap) will return after vsnprintf. So to do
what you are trying the proper use is something like:

   va_copy(tmpva,args);
   vsnprintf(tmp,256,tf,tmpva);
   va_end(tmpva);
   va_arg(args,??);

Where it is somewhat challenging to compute ??

Relying on vsnprintf to advance args by exactly one is not portable.

Jason



More information about the general mailing list