Calling vfprintf with too few arguments in the va_list

Discussion in 'C Programming' started by Spoon, Jul 25, 2007.

  1. Spoon

    Spoon Guest

    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.
    Spoon, Jul 25, 2007
    #1
    1. Advertising

  2. Spoon

    jacob navia Guest

    Spoon wrote:
    > 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.
    jacob navia, Jul 25, 2007
    #2
    1. Advertising

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

    --
    a signature
    those who know me have no need of my name, Jul 25, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anton muhin

    JNI: vfprintf hook

    anton muhin, Sep 23, 2004, in forum: Java
    Replies:
    2
    Views:
    2,141
    anton muhin
    Sep 28, 2004
  2. chellappa

    fprintf or vfprintf

    chellappa, Sep 16, 2005, in forum: C Programming
    Replies:
    2
    Views:
    344
    Malcolm
    Sep 16, 2005
  3. Murali
    Replies:
    2
    Views:
    559
    Jerry Coffin
    Mar 9, 2006
  4. Chuck Chopp
    Replies:
    0
    Views:
    393
    Chuck Chopp
    Sep 13, 2008
  5. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page