Trouble with va_list processing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thierrydollar

    Trouble with va_list processing

    Hi,

    I have written a very simple program using variable arguments calls and
    I
    get strange things that I cannot explain.

    I have one function (add) that displays two parameters. It works well
    when I call it directly.
    Now I have a second function (set) that also calls add. But this
    doesn't
    work.

    Here is the output generated by my program. I don't understand why I
    get different result (I tried on both Borland C++ Builder and in Visual
    C++, and
    I got similar results).

    Any help would be greatly appreciated.
    Thanks
    Best Regards
    Thierry

    [color=blue][color=green][color=darkred]
    >>>ADD[/color][/color][/color]
    format = H%dW%d
    arg[] = 1234
    arg[] = 5678
    [color=blue][color=green][color=darkred]
    >>>SET[/color][/color][/color]
    format = H%dW%d
    arg[] = 1245060
    arg[] = 1245060




    #include <stdio.h>
    #include <stdarg.h>

    //---------------------------------------------------------------------------
    void add (char *format, ...)
    {
    va_list arg_ptr;
    va_start (arg_ptr, format);

    printf("format = %s\n",format);
    printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
    printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
    }
    //---------------------------------------------------------------------------
    void set (char *format, ...)
    {
    va_list arg_ptr;
    va_start (arg_ptr, format);
    add(format, arg_ptr);
    }
    //---------------------------------------------------------------------------
    int main()
    {
    printf("\n>>>AD D \n");
    add("H%dW%d",12 34,5678);

    printf("\n>>>SE T \n");
    set("H%dW%d",12 34,5678);

    return 0;
    }

  • Keith Thompson

    #2
    Re: Trouble with va_list processing

    "thierrydol lar" <thierry@tetrae dre.com> writes:[color=blue]
    > I have written a very simple program using variable arguments calls
    > and I get strange things that I cannot explain.
    >
    > I have one function (add) that displays two parameters. It works
    > well when I call it directly. Now I have a second function (set)
    > that also calls add. But this doesn't work.
    >
    > Here is the output generated by my program. I don't understand why I
    > get different result (I tried on both Borland C++ Builder and in
    > Visual C++, and I got similar results).[/color]

    I assume you were using both as C compilers; if you're using them as
    C++ compilers, you're in the wrong newsgroup. Your program appears to
    be within the common subset of C and C++, but there are subtle
    differences.

    [snip]
    [color=blue][color=green][color=darkred]
    >>>>ADD[/color][/color]
    > format = H%dW%d
    > arg[] = 1234
    > arg[] = 5678
    >[color=green][color=darkred]
    >>>>SET[/color][/color]
    > format = H%dW%d
    > arg[] = 1245060
    > arg[] = 1245060
    >
    >
    >
    >
    > #include <stdio.h>
    > #include <stdarg.h>
    >
    > //---------------------------------------------------------------------------
    > void add (char *format, ...)
    > {
    > va_list arg_ptr;
    > va_start (arg_ptr, format);
    >
    > printf("format = %s\n",format);
    > printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
    > printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
    > }
    > //---------------------------------------------------------------------------
    > void set (char *format, ...)
    > {
    > va_list arg_ptr;
    > va_start (arg_ptr, format);
    > add(format, arg_ptr);
    > }
    > //---------------------------------------------------------------------------
    > int main()
    > {
    > printf("\n>>>AD D \n");
    > add("H%dW%d",12 34,5678);
    >
    > printf("\n>>>SE T \n");
    > set("H%dW%d",12 34,5678);
    >
    > return 0;
    > }[/color]

    Your call to add() is incorrect, but is likely to work anyway.
    When it processes the call
    add("H%dW%d",12 34,5678);
    the compiler doesn't know that add() is expecting unsigned long
    arguments. The constants 1234 and 5678 are of type int. Try either
    add("%dW%d", 1234UL, 5678UL);
    or
    add("%dW%d", (unsigned long1234, (unsigned long)5678);
    or change the va_arg() invocations to use int.

    This is likely to "work" if int and long are the same size, or if
    they're passed with compatible calling conventions and the byte
    ordering is just right.

    (Also, since the standard *printf() functions use "%d" for type int,
    it seems misleading for add() to use it for unsigned long. Presumably
    the full version of add() pays attention to the format string -- but
    thank you for stripping out that part of the code.)

    You forgot to call va_end() in add().

    The add() function requires a char* argument followed by zero or more
    arguments of unknown types; since it invokes va_arg() with a type of
    unsigned long, it will invoke undefined behavior unless you call it
    with a char* and two unsigned longs. In set(), you call add() with a
    char* and a va_list. The add() function probably tries to interpret
    the va_list value as if it were an unsigned long, and then tries to
    interpret whatever garbage happens to be where the third argument
    *would* have been stored as if it were an unsigned long -- and that's
    only with some optimistic assumptions. It's undefined behavior.

    By analogy with the standard's printf() vs. vprintf(), you can create
    a second function, say vadd(), that takes a format string and a
    va_list. Your add() function could then be implemented as:

    void add(char *format, ...)
    {
    va_list arg_ptr;
    va_start(arg_pt r, format);
    vadd(format, arg_ptr);
    va_end(arg_ptr) ;
    }

    (I've only minimally tested a variant of this code; use with caution.)

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Thad Smith

      #3
      Re: Trouble with va_list processing

      thierrydollar wrote:
      [color=blue]
      > I have written a very simple program using variable arguments calls and
      > I get strange things that I cannot explain.[/color]
      [color=blue]
      > #include <stdio.h>
      > #include <stdarg.h>
      >
      > //---------------------------------------------------------------------------
      > void add (char *format, ...)
      > {
      > va_list arg_ptr;
      > va_start (arg_ptr, format);
      >
      > printf("format = %s\n",format);
      > printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
      > printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
      > }
      > //---------------------------------------------------------------------------
      > void set (char *format, ...)
      > {
      > va_list arg_ptr;
      > va_start (arg_ptr, format);
      > add(format, arg_ptr);[/color]

      The function add expects the second and third argument to be of type
      unsigned long, but you pass it a type arg_ptr for the first and nothing
      for the second. The result is undefined behavior.
      [color=blue]
      > }
      > //---------------------------------------------------------------------------
      > int main()
      > {
      > printf("\n>>>AD D \n");
      > add("H%dW%d",12 34,5678);[/color]

      Add expects two arguments of type unsigned long. You are providing two
      arguments of type int. It may work on your system if int and long are
      the same size, but is poor practice and not portable.
      [color=blue]
      > printf("\n>>>SE T \n");
      > set("H%dW%d",12 34,5678);
      >
      > return 0;
      > }
      >[/color]


      --
      Thad

      Comment

      • thierrydollar

        #4
        Re: Trouble with va_list processing

        The problem I described is not related to the type of arguments. I had
        also problems when passing strings.

        Comment

        • Chris Dollin

          #5
          Re: Trouble with va_list processing

          thierrydollar wrote:
          [color=blue]
          > The problem I described is not related to the type of arguments. I had
          > also problems when passing strings.[/color]

          We (usually) can't debug code we can't see.

          --
          Chris "understand ing is a three-edged sword" Dollin

          Comment

          • thierrydollar

            #6
            Re: Trouble with va_list processing


            I have written another example of the problem with va_list

            And here is the result

            format = %s
            arg[] = hello
            format = %s
            arg[] = ä ↕


            Here is the source code

            #include <stdio.h>
            #include <stdarg.h>

            //---------------------------------------------------------------------------
            void add (char *format, ...)
            {
            va_list args;
            va_start (args, format);

            printf("format = %s\n",format);
            printf("arg[] = %s\n", va_arg (args, char *));
            }
            //---------------------------------------------------------------------------
            void set (char *format, ...)
            {
            va_list args;
            va_start (args, format);
            add(format, args);
            }
            //---------------------------------------------------------------------------
            int main()
            {
            char txt[] = "hello";
            add ("%s",txt);
            set ("%s",txt);

            return 0;
            }
            //---------------------------------------------------------------------------

            Comment

            • Dag-Erling Smørgrav

              #7
              Re: Trouble with va_list processing

              "thierrydol lar" <thierry@tetrae dre.com> writes:[color=blue]
              > #include <stdio.h>
              > #include <stdarg.h>
              >
              > //---------------------------------------------------------------------------
              > void add (char *format, ...)
              > {
              > va_list args;
              > va_start (args, format);
              >
              > printf("format = %s\n",format);
              > printf("arg[] = %s\n", va_arg (args, char *));
              > }[/color]

              you don't call va_end().
              [color=blue]
              > //---------------------------------------------------------------------------
              > void set (char *format, ...)
              > {
              > va_list args;
              > va_start (args, format);
              > add(format, args);
              > }[/color]

              you don't call va_end().

              add() expects a char *, not a va_list.

              DES
              --
              Dag-Erling Smørgrav - des@des.no

              Comment

              • Jordan Abel

                #8
                Re: Trouble with va_list processing

                On 2006-02-01, thierrydollar <thierry@tetrae dre.com> wrote:[color=blue]
                > The problem I described is not related to the type of arguments. I had
                > also problems when passing strings.[/color]

                Probably because you're also doing it wrong when you try to pass
                strings.

                Comment

                • Keith Thompson

                  #9
                  Re: Trouble with va_list processing

                  "thierrydol lar" <thierry@tetrae dre.com> writes:[color=blue]
                  > The problem I described is not related to the type of arguments. I had
                  > also problems when passing strings.[/color]

                  What???

                  Please read <http://cfaj.freeshell. org/google/> *before* you post
                  another followup.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Peter Shaggy Haywood

                    #10
                    Re: Trouble with va_list processing

                    Groovy hepcat thierrydollar was jivin' on 31 Jan 2006 15:30:14 -0800
                    in comp.lang.c.
                    Trouble with va_list processing's a cool scene! Dig it!
                    [color=blue]
                    >#include <stdio.h>
                    >#include <stdarg.h>
                    >
                    >//---------------------------------------------------------------------------
                    >void add (char *format, ...)
                    >{
                    > va_list arg_ptr;
                    > va_start (arg_ptr, format);
                    >
                    > printf("format = %s\n",format);
                    > printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));[/color]

                    %d format specifier with unsigned long argument.
                    [color=blue]
                    > printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));[/color]

                    %d format specifier with unsigned long argument.
                    No invokation of the va_end macro.
                    Assumes 2 unsigned long arguments will always be passed after
                    format. (Makes a varargs function unnecessary and undesirable.)
                    Not adding anything. (Makes the function name confusing.)
                    [color=blue]
                    >}
                    >//---------------------------------------------------------------------------
                    >void set (char *format, ...)
                    >{
                    > va_list arg_ptr;
                    > va_start (arg_ptr, format);
                    > add(format, arg_ptr);[/color]

                    Passing a va_list to a function that is expecting two unsigned long
                    arguments.
                    No invokation of the va_end macro.
                    Not setting anything. (Makes the function name confusing.)
                    [color=blue]
                    >}
                    >//---------------------------------------------------------------------------
                    >int main()
                    >{
                    > printf("\n>>>AD D \n");
                    > add("H%dW%d",12 34,5678);[/color]

                    Passing two ints to function expecting two unsigned longs.
                    [color=blue]
                    > printf("\n>>>SE T \n");
                    > set("H%dW%d",12 34,5678);
                    >
                    > return 0;
                    >}[/color]

                    If you want to pass a va_list to some function, you must have a
                    version of the function that takes a va_list. For example:

                    void vadd(const char *fmt, va_list ap)
                    {
                    /* Your code using va_arg(ap, some_type) goes here. */
                    }

                    Then your add() function becomes a wrapper around this, like so:

                    void add(const char *fmt, ...)
                    {
                    va_list ap;

                    va_start(ap, fmt);
                    vadd(fmt, ap);
                    va_end(ap);
                    }

                    And your set() function is done similarly.
                    But you must make sure that a) you actually need varargs functions,
                    b) you don't make assumptions about the number and types of arguments
                    of varargs functions, c) when processing those arguments, you use them
                    correctly (eg., don't pass an unsigned long to printf() with a %d
                    conversion specifier - that's for signed int) and d) you don't pass
                    variadic arguments of the wrong type (eg., when the function expects
                    an unsigned long, and you want to pass 1234 to it, cast it to unsigned
                    long).

                    --

                    Dig the even newer still, yet more improved, sig!


                    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
                    I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

                    Comment

                    • Nelu

                      #11
                      Re: Trouble with va_list processing

                      thierrydollar wrote:[color=blue]
                      > I have written another example of the problem with va_list
                      >
                      > And here is the result
                      >
                      > format = %s
                      > arg[] = hello
                      > format = %s
                      > arg[] = ä ↕
                      >
                      >
                      > Here is the source code
                      >
                      > #include <stdio.h>
                      > #include <stdarg.h>
                      >
                      > //---------------------------------------------------------------------------
                      > void add (char *format, ...)
                      > {
                      > va_list args;
                      > va_start (args, format);
                      >
                      > printf("format = %s\n",format);
                      > printf("arg[] = %s\n", va_arg (args, char *));
                      > }
                      > //---------------------------------------------------------------------------
                      > void set (char *format, ...)
                      > {
                      > va_list args;
                      > va_start (args, format);
                      > add(format, args);
                      > }
                      > //---------------------------------------------------------------------------
                      > int main()
                      > {
                      > char txt[] = "hello";
                      > add ("%s",txt);
                      > set ("%s",txt);
                      >
                      > return 0;
                      > }
                      > //---------------------------------------------------------------------------
                      >[/color]
                      Please read http://cfaj.freeshell.org/google/
                      Thank you!


                      --
                      Ioan - Ciprian Tandau
                      tandau _at_ freeshell _dot_ org (hope it's not too late)
                      (... and that it still works...)

                      Comment

                      • Nelu

                        #12
                        Re: Trouble with va_list processing

                        thierrydollar wrote:[color=blue]
                        > The problem I described is not related to the type of arguments. I had
                        > also problems when passing strings.
                        >[/color]
                        Please read http://cfaj.freeshell.org/google/
                        Thank you!

                        --
                        Ioan - Ciprian Tandau
                        tandau _at_ freeshell _dot_ org (hope it's not too late)
                        (... and that it still works...)

                        Comment

                        Working...