Calling vfprintf with too few arguments in the va_list

S

Spoon

Hello everyone,

AFAIU, in C89, calling fprintf with too few arguments leads to UB.

4.9.6.1 The fprintf function

Synopsis

#include <stdio.h>
int fprintf(FILE *stream, const char *format, ...);

Description

The fprintf function writes output to the stream pointed to by
stream, under control of the string pointed to by format that specifies
how subsequent arguments are converted for output. If there are
insufficient arguments for the format, the behavior is undefined. If the
format is exhausted while arguments remain, the excess arguments are
evaluated (as always) but are otherwise ignored. The fprintf function
returns when the end of the format string is encountered.

IIUC, the following calls lead to UB:

fprintf(stdout, "%d");
fprintf(stdout, "%d %d", 12);

Is that correct?

Does calling vfprintf with too few arguments inside the va_list also
lead to undefined behavior?

void error(char *function_name, char *format, ...)
{
va_list args;

va_start(args, format);
/* print out name of function causing error */
fprintf(stderr, "ERROR in %s: ", function_name);
/* print out remainder of message */
vfprintf(stderr, format, args);
va_end(args);
}

Do the following calls lead to UB:

error("foo", "%d");
error("foo", "%d %d %d %d %d", 1, 2, 3);

Regards.
 
J

jacob navia

Spoon said:
Hello everyone,

AFAIU, in C89, calling fprintf with too few arguments leads to UB.

4.9.6.1 The fprintf function

Synopsis

#include <stdio.h>
int fprintf(FILE *stream, const char *format, ...);

Description

The fprintf function writes output to the stream pointed to by
stream, under control of the string pointed to by format that specifies
how subsequent arguments are converted for output. If there are
insufficient arguments for the format, the behavior is undefined. If the
format is exhausted while arguments remain, the excess arguments are
evaluated (as always) but are otherwise ignored. The fprintf function
returns when the end of the format string is encountered.

IIUC, the following calls lead to UB:

fprintf(stdout, "%d");
fprintf(stdout, "%d %d", 12);

Is that correct?

Does calling vfprintf with too few arguments inside the va_list also
lead to undefined behavior?

void error(char *function_name, char *format, ...)
{
va_list args;

va_start(args, format);
/* print out name of function causing error */
fprintf(stderr, "ERROR in %s: ", function_name);
/* print out remainder of message */
vfprintf(stderr, format, args);
va_end(args);
}

Do the following calls lead to UB:

error("foo", "%d");
error("foo", "%d %d %d %d %d", 1, 2, 3);

Regards.
This is the same for C99. Too few arguments is UB.
 
T

those who know me have no need of my name

in comp.lang.c i read:
AFAIU, in C89, calling fprintf with too few arguments leads to UB.
Is that correct?
yes.

Does calling vfprintf with too few arguments inside the va_list also
lead to undefined behavior?

yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top