This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
function overloading and variadic arguments
- From: "Peter A. Felvegi" <petschy at praire-chicken dot com>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 20 Sep 2007 21:29:30 +0200
- Subject: function overloading and variadic arguments
- References: <1190310348.4793.ezmlm@gcc.gnu.org>
hello,
today i've spent a few hours debugging... consider the following code:
---->8---->8---->8---->8---->8---->8----
// test.cpp
#include <stdarg.h>
#include <stdio.h>
int prn(const char* fmt_);
int prn(const char* fmt_, ...) __attribute__((format(printf, 1, 2)));
int prn(const char* fmt_, va_list args_);
int prn(const char* fmt_)
{
return 0;
}
int prn(const char* fmt_, ...)
{
va_list args;
va_start(args, fmt_);
int r = prn(fmt_, args);
va_end(args);
return r;
}
int prn(const char* fmt_, va_list args_)
{
char out[4096];
return vsnprintf(out, sizeof(out), fmt_, args_);
}
int main(int argc_, char** argv_)
{
int s0 = prn("%s %s", "hello", "world");
int s1 = prn("hello %s", "world");
int s2 = prn("hello %s", argv_[0]);
int s3 = prn("hello %d", 42);
return 0;
}
---->8---->8---->8---->8---->8---->8----
it turns out, that when initializing s1, the function with the va_list
second arg is called, not the variadic version, which results in a sigsegv.
tried gcc-2.95, 3.3, 4.1 and 4.2, with -Wall -W[extra]. 4.2 warns about
the deprecated conversion from string constant to char* (which needs
some interpretation), but the others are silent about the issue.
a warning would be nice, if the compiler detects that there's an
ambiguity: passing an argument that would fit the variadic and the
va_list (or any other type) argument versions of the functions, too.
the solution is to name the functions differently, but unfortunately
pinning down the source of the bug took me quite some time (the original
code was more complex of course ;)
i'd like to hear your comments.
regards, p