I have the following function from some legacy code that I am maintaining.
long getMaxStart(long start, long count, const myStruct *s1, ...)
{
long i1, maxstart;
myStruct *s2;
va_list marker;
maxstart = start;
/*BUGFIX: 003 */
/*(va_start(marker, count);*/
va_start(marker, s1);
for (i1 = 1; i1 <= count; i1++)
{
s2 = va_arg(marker, myStruct *); /* <- s2 is assigned null here */
maxstart = MAX(maxstart, s2->firstvalid); /* <- SEGV here */
}
va_end(marker);
return (maxstart);
}
When the function is called with only one myStruct argument, it causes a SEGV. The code compiled and run without crashing on Windows XP when I compiled it using VS2005. I have now moved the code to Ubuntu Karmic and I am having problems with the stricter compiler on Linux. Is anyone able to spot what is causing the parameter not to be read correctly in the var_arg() statement?
I am compiling using gcc version 4.4.1
Edit
The statement that causes the SEGV is this one:
start = getMaxStart(start, 1, ms1);
The variables ‘start’ and ‘ms1’ have valid values when the code execution first reaches this line.
As written, when you pass in only one
myStructargument,s1is bound to that argument and yourva_listwill be empty. Then, the first thing you do in the loop is to get the argument from that empty list, hence the NULL.If you require at least one argument and want the compiler to type-check that for you, you’d have to do something like this:
Otherwise, you’re better off just removing
s1from the function definition like Potatoswatter mentioned: