Passing a single argument list to two v*printf() calls - c

Consider the following test case:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
void test(char **outa, char **outb, const char* fstra, const char* fstrb, ...) {
va_list ap;
va_start(ap, fstrb);
vasprintf(&outa, fstra, ap);
vasprintf(&outb, fstrb, ap);
va_end(ap);
}
int main(void) {
char *a, *b;
test(&a, &b, "%s", " %s\n", "foo", "bar");
/* ... */
}
The intent here is that the test() function takes two format strings and a list of parameters for both of them. The first format string is supposed to 'eat' as many arguments it needs, and the remaining ones are supposed to be used for the second format string.
So, the expected result here would be foo & bar and that's what I get with glibc. But AFAICS the machine running codepad (guess some *BSD it is), gives foo & foo and my guess is that it uses va_copy() on the argument list.
I guess I'm hitting an undefined (and ugly) behavior here; so the question is: is there a way to achieve double-format-string printf() without reimplementing it from scratch? And is there a nice way to check that behavior using autoconf without using AC_RUN_IFELSE()?
I guess some quick method of scanning format-string for the number of arguments to be consumed could work here as well (+va_copy()).

When you call one of the v*printf functions, this uses va_arg which means the value of ap is indeterminate on return.
The relevant bit lies in section 7.19.6.8 The vfprintf function in C99, which references the footnote:
As the functions vfprintf, vfscanf, vprintf, vscanf, vsnprintf, vsprintf, and vsscanf invoke theva_argmacro, the value ofargafter the return is indeterminate.
This has survived to the latest draft of C1x I have as well, so I suspect it's not going to change quickly.
There is no portable way to do what you're attempting using the higher-level v*printf functions although you could resort to using the lower level stuff.
The standard is very clear in that a called function using va_arg on a va_list variable renders it indeterminate in the caller. From C99 7.15 Variable Arguments <stdarg.h>:
The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and shall be passed to the va_end macro prior to any further reference to ap.
However, the value of ap when using va_arg on it within a single function is determinate (otherwise the whole variable arguments processing would fall apart). So you could write a single function which processed both format strings in turn, with these lower-level functions.
With the higher level stuff (as per the footnote), you are required to va_end/va_start to put the ap variable back in a determinate state and this will unfortunately reset to the start of the parameter list.
I'm not sure how much of a simplification your provided example code is but, if that's close to reality, you can acheive the same result by just combining the two format strings beforehand and using that to pass to vprintf, something like:
void test(const char* fstra, const char* fstrb, ...) {
char big_honkin_buff[1024]; // Example, don't really do this.
va_list ap;
strcpy (big_honkin_buff, fstra);
strcat (big_honkin_buff, fstrb);
va_start(ap, big_honkin_buff);
vprintf(big_honkin_buff, ap);
va_end(ap);
}

As the other answer already states, passing ap to a v*() function leaves ap in an undetermined state. So, the solution is to not depend on this state. I suggest an alternative workaround.
First, initialize ap as normal. Then determine the length of the first formatted string using vsnprintf(NULL, 0, fstra, ap). Concatenate the format strings, reinitialize ap, and split the output using the predetermined length of the first formatted string.
It should look something like the following:
void test(const char* fstra, const char* fstrb, ...) {
char *format, *buf;
char *a, *b;
int a_len, buf_len;
va_list ap;
va_start(ap, fstrb);
a_len = vsnprintf(NULL, 0, fstra, ap);
va_end(ap);
asprintf(&format, "%s%s", fstra, fstrb);
va_start(ap, fstrb);
buf_len = vasprintf(&buf, format, ap);
va_end(ap);
free(format);
a = malloc(a_len + 1);
memcpy(a, buf, a_len);
a[a_len] = '\0';
b = malloc(buf_len - a_len + 1);
memcpy(b, buf + a_len, buf_len - a_len);
b[buf_len - a_len] = '\0';
free(buf);
}
As also discussed in the other answer, this approach does not separate positional printf-style placeholders ("%1$s. I repeat, %1$s."). So the documentation for the interface should clearly state that both format strings share the same positional placeholder namespace—and that if one of the format strings uses positional placeholders then both must.

To complete the other answers, which are correct, a word about what happens in common implementations.
In 32bit Linux (and I think Windows too), passing the same ap to two functions actually works.
This is because the va_list is just a pointer to the place on the stack where the parameters are. v*rintf functions get it, but don't change it (they can't, it's passed by value).
In 64bit Linux (don't know about Windows), it doesn't work.
va_list is a struct, and v*printf gets a pointer to it (because actually it's an array of size 1 of structs). When arguments are consumed, the struct is modified. So another call to v*printf will get the parameters not from the start, but after the last one consumed.
Of course, this doesn't mean you should use a va_list twice in 32bit Linux. It's undefined behavior, which happens to work in some implementations. Don't rely on it.

Related

In C, how to pass a variable number of arguments (say, 50+) to a function which va_start() understands prior to calling vsnprintf()?

Question
Is there a way to pass many arguments to MyPrint() below using some kind of array containing a list of pointers to strings that va_start() understands before calling vsnprintf()?
Example of a format string specifier. It would be nice to create an array of the corresponding values and pass that to MyPrint() rather than individually passing each argument. I don't know if it's possible for va_start() to understand it. :(
"[0x%llX][%u] %s --- A=%llu (0x%llX) B=%llu (0x%llX) C=%llu (0x%llX) X=%llu (0x%llX) Y=%llu (0x%llX) Z=%llu (0x%llX)"
Details
MyPrint() calls vsnprintf() which prints a formatted list of arguments to a character array. The declaration for vsnprintf() is shown below:
int vsnprintf(char *arr, size_t len, const wchar_t *format, va_list args);
Parameters
arr: Pointer to the character array where output is to be printed
len: Maximum number of characters that can be written to the array
format: Format in which the output will be printed
args: Pointer to the list of arguments to be printed
Demo
#include <stdio.h>
#include <stdarg.h>
int MyPrint(char* buffer, int bufferSize, const char *format, ...)
{
int len = 0;
va_list arguments;
va_start(arguments, format);
len = vsnprintf(buffer, bufferSize, format, arguments);
va_end(arguments);
return len;
}
int main()
{
char buffer[256];
MyPrint(buffer, 256, "%s %s","Hello","World");
printf("%s",buffer);
return 0;
}
Is there a way to pass many arguments to MyPrint() below using some kind of array containing a list of pointers to strings that va_start() understands before calling vsnprintf()?
The only defined ways to initialize a va_list, such as vsnprintf() requires as a parameter, are
via the va_start() macro, operating in the context of a variadic function to form a va_list from the function's variadic arguments, and
via the va_copy() macro, to make a copy of another va_list.
There is no mechanism in standard C to form a va_list from the elements of an array, except by passing them all, individually, to a variadic function.
Variadic functions are about coding flexibility, not data flexibility. If you want a function that handles arrays of data, then write a (non-variadic) one that does so.
Whenever you consider writing your own varargs function, smack yourself in the head and repeat the mantra: "varargs is not the answer". Only if you still have varargs in your head after a few iterations of that should you should consider actually investigating that option.

va_arg always runs 4 times

I am learning stdarg.h in c i am trying to print all arguments passed to function without knowing how many arguments are there but yet i have not come up with solution, during this this happened, no matter what i pass to strtest.
It always print 0. 1. 2. 3.
void strtest(char *fmt, ...){
va_list argp;
int i = 0;
va_start(argp, fmt);
while(va_arg(argp, char*))
printf("%d\t", i++ );
va_end(argp);
}
int main(int argc, char *argv[]){
strtest("s");
printf("\n");
return 0;
}
There's no standard mechanism that will tell you the number of arguments passed to a varargs function. Functions like printf() work because they can determine the number of arguments by examining the format string.
Here is an example showing one way to pass an unknown number of arguments.
#include <stdio.h>
#include <stdarg.h>
void print (char *first, ...)
{
va_list argptr;
char *next;
va_start (argptr, first);
next = first;
while (next) {
printf ("%s\n", next);
next = va_arg(argptr, char*);
}
va_end (argptr);
}
int main(void)
{
print("hello","world", NULL); // NULL as sentinel
return 0;
}
Program output
hello
world
Perhaps you can adapt this to your needs using int arguments.
This is the definition of stdarg.h in the ISO 9899 WG14 n1256
The header <stdarg.h> declares a type and defines four macros, for advancing
through a list of arguments whose number and types are not known to the called function
when it is translated
You have to pass the number of arguments, and possibly the types as well, to the caller. This doesn't have to be done by directly passing the number of arguments, there are other methods such as the one used in printf.
You could pass a sentinel to the function like this
strtest("s", (char*)0);
such that the function can notice that it is at the end of the argument list.
If you look at the man page for stdarg, va_arg includes this text
If there is no next argument, or if type is not compatible with the
type of the actual next argument (as promoted according to the
default argument promotions), random errors will occur.
Unless you call strtest with a NULL as the last argument, va_arg will just keep reading until it hits something that makes it stop. Think of what you are doing right now as equivalent to reading an array outside its bounds.
I'm surprised it was running 4 times no matter what though. I would have expected the count to be equal to the number of args you passed to strtest plus 2 or more.

Variadic functions in C

I have a function which looks like
void demo_function_v(const char * mask, va_list ap)
{
for (; *mask; mask++) {
// get one or more args from ap, depending on *mask
}
}
This runs on a AVR system which has different access techniques for the flash memory. In order to duplicate this function, I'd like to extract the main part of it:
void demo_function_1char(char mask, va_list ap)
{
// get one or more args from ap, depending on mask
}
void demo_function_v(const char * mask, va_list ap)
{
for (; *mask; mask++) {
demo_function_1char(*mask, ap)
}
}
Alas, this is not guaranteed to work, as
The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate.
And this is true - on x86, it doesn't work, on x64, it does. If it works on AVR, I'll be able to test tomorrow. But I'd rather not rely on this if it is described as "indeterminate" (which is supposedly a kind of UB).
Thus, I'd like to go another track:
void demo_function_1char(char mask, va_list * pap)
{
// get one or more args from *pap, depending on mask
}
void demo_function_v(const char * mask, va_list ap)
{
for (; *mask; mask++) {
demo_function_1char(*mask, &ap)
}
}
Is this supposed to work, or will I shoot my foot this way as well?
The C99 standard has the following footnote clarifying this:
It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns.
I believe that behavior was also the intent in C90 even if the standard didn't note it. The C90 rationale document does have this to say:
The va_list type is not necessarily assignable. However, a function can pass a pointer to its initialized argument list object, as noted below.
...
va_start must be called within the body of the function whose argument list is to be traversed. That function can then pass a pointer to its va_list object ap to other functions to do the actual traversal. (It can, of course, traverse the list itself.)
Making it pretty clear, I think, that accessing the va_list object through a pointer acts as you'd expect (that the va_list state is being maintained in the object instance that the address was taken from), even if it doesn't explicitly state that the original va_list object will pick up where the pointer usage left off. For it to not work this way, pointers to va_list objects would have to behave differently than pointers to other C objects.

Pass va_list or pointer to va_list?

Suppose I have a function which takes variadic arguments (...) or a va_list passed from another such function. The main logic is in this function itself (let's call it f1), but I want to have it pass the va_list to another function (let's call it f2) which will determine the next argument type, obtain it using va_arg, and properly convert and store it for the caller to use.
Is it sufficient to pass a va_list to f2, or is it necessary to pass a pointer to va_list. Unless va_list is required to be an array type or else store its position data at the location the va_list object points to (rather than in the object itself), I can't see how passing it by value could allow the calling function (f1) to 'see' the changes the called function made by va_arg.
Can anyone shed light on this? I'm interested in what the standard requires, not what some particular implementation allows.
It looks like you'll need to pass a pointer to the va_list. For more info, see the C99 standard document section 7.15.In particular, bullet point 3 states:
The object ap may be passed as an argument to
another function; if that function invokes the va_arg macro with parameter ap, the
value of ap in the calling function is indeterminate and shall be passed to the va_end
macro prior to any further reference to ap
[my italics]
Edit: Just noticed a footnote in the standard:
215) It is permitted to create a pointer to a va_list and pass that pointer to another function, in which
case the original function may make further use of the original list after the other function returns
So you can pass a pointer to the va_list and do va_arg(*va_list_pointer) in the called function.
In my understanding, you're supposed to pass the va_list directly (not a pointer to it). This seems to be supported by comp.lang.c:
"A va_list is not itself a variable argument list; it's really sort of a pointer to one. That is, a function which accepts a va_list is not itself varargs, nor vice versa. "
I find the texts quite ambiguous on this question. The simplest is perhaps to look in the standard how predefined functions with va_list are supposed to receive it, e.g vsnprintf. And this is clearly by value and not by reference.
You should pass a pointer to a va_list if you want to use it in a subfunction and then not have to immediately pass it to va_end afterwards. From C99:
It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns.
The standard allows this, however, on some 64-bit platforms where va_list is an array type, this does not work. As the address of an array is the same as the address of the first element of the array, passing a pointer to the va_list will segfault upon calling va_arg with that pointer to va_list as an argument.
A way to get around this is by receiving the va_list as an unconventional argument name (usually suffixed with one or more underscores) and then creating a new local va_list, like so:
#include <stdarg.h>
int vfoo(va_list ap_)
{
int ret;
va_list ap;
va_copy(ap, ap_);
ret = vbar(&ap);
/* do other stuff with ap */
va_end(ap);
return ret;
}
This is the approach I use in my vsnprintf implementation to call other functions from it for formatting.
Functions in standard C library pass va_list element itself (man 3 vprintf):
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
Passing a pointer to va_list works fine in 32 bit system. You can even fetch one parameter a time in the sub-routine.
But it doesn't seem to work in 64 bit system, will produce a segment fault at va_arg().

what's the difference between the printf and vprintf function families, and when should I use one over the other?

I understand that the difference between the printf, fprintf, sprintf etc functions and the vprintf, vfprintf, vsprintf etc functions has to do with how they deal with the function arguments. But how specifically? Is there really any reason to use one over the other? Should I just always use printf as that is a more common thing to see in C, or is there a legitimate reason to pick vprintf instead?
printf() and friends are for normal use. vprintf() and friends are for when you want to write your own printf()-like function. Say you want to write a function to print errors:
int error(char *fmt, ...)
{
int result;
va_list args;
va_start(args, fmt);
// what here?
va_end(args);
return result;
}
You'll notice that you can't pass args to printf(), since printf() takes many arguments, rather than one va_list argument. The vprintf() functions, however, do take a va_list argument instead of a variable number of arguments, so here is the completed version:
int error(char *fmt, ...)
{
int result;
va_list args;
va_start(args, fmt);
fputs("Error: ", stderr);
result = vfprintf(stderr, fmt, args);
va_end(args);
return result;
}
You never want to use vprintf() directly, but it's incredibly handy when you need to e.g. wrap printf(). For these cases, you will define the top-level function with variable arguments (...). Then you'll collect those into a va_list, do your processing, and finally call vprintf() on the va_list to get the printout happening.
The main difficulty with variadic arguments is not that there is a variable number of arguments but that there is no name associated with each argument. The va_start, va_arg macros parse the arguments in memory (in most C compilers they are on the stack) using the type information contained in the format string cf. Kernighan and Ritchie, second edition, section 7.3.
This example shows the elegance of Python. Since C/C++ cannot reconcile the difference between int error(char *fmt, ...) and int error(char *fmt, va_list ap), thus, for every function *printf, it has to create two versions, i.e., one taking in ..., the other taking in va_list, this essentially doubles the total number of functions. In Python, you can use *list() or **dict() to pass in a va_list as ....
Hopefully, future C/C++ can support this kind of argument processing scheme.

Resources