tru64-unix-managers (formerly alpha-osf-managers) (1994-2012)
 help / color / mirror / Atom feed
* snprintf problems on Tru64 4.0g
@ 2002-04-15 13:51 Paul Clayton
  0 siblings, 0 replies; only message in thread
From: Paul Clayton @ 2002-04-15 13:51 UTC (permalink / raw)
  To: tru64-unix-managers

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


---


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-04-15 13:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-04-15 13:51 snprintf problems on Tru64 4.0g Paul Clayton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox