snprintf problems on Tru64 4.0g

From: Paul Clayton (paul.clayton@intec.co.za)
Date: Mon Apr 15 2002 - 09:51:42 EDT


Hi managers,

This was sent to me by one of our developers.

Regarding snprintf on Tru64, here's a test scenario in which the problem
shows up, and an illustration of how odd it is.

When this machine is booted into the 5.1a installation these problems do not
happen.

The problem manifests as follows:
        snprintf is a common non-ansi C extension that allows a programmer
to avoid
        buffer overflows by specifying the size of the destination string a
format
        will be printed into. This extension is available on Tru64 according
to the
        man pages (man snprintf), and should be declared in the file
        /usr/include/stdio.h (it is not declared in this file on our 4.0g
installation).

        When compiling a small test program with the standard C compiler,
the
        compilation succeeds regardless of whether or not the stdio.h header
        is included.

        After renaming that same file to .cpp, attempting to compile it with
the
        cxx compiler fails, regardless of whether the stdio.h header is
included
        or not.

        After looking at the C library (nm /usr/shlib/libc.so | grep
^snprintf),
        I found that the function does in fact exist - output from the above
        command is:
                snprintf | 0004395900427136 | T |
0000000000000008

        So, by forward declaring the snprintf function in the test program,
I am able to
        compile it using the cxx compiler. This is rather unusual behaviour,
as the
        function should really be in /usr/include/stdio.h, especially if it
is present
        in the C library.

To compile the C program (listed below), I use:
        cc main.c

To compile the C++ program (listed below), I use:
        cxx main.cpp

Both commands produce a file called a.out.

To successfully compile the C++ test file, uncomment the forward
declaration.

Michael

---- main.c ----
#include <stdio.h> /* actually not needed */

int main()
{
        char test[20];
        snprintf( test, 20, "%s: %d", "test string", 20 );
        return 0;
}
---- end main.c ----
---- main.cpp ----
#include <stdio.h> /* pointless */

// uncomment the line below to compile...
// extern "C" int snprintf( char *, size_t, const char *, ... );

int main()
{
        char test[20];
        snprintf( test, 20, "%s: %d", "test string", 20 );
        return 0;
}
---- end main.cpp ----

---


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:48:38 EDT