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.
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.