473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re-using a 'va_list'

Hi folks!

I have a function that gets a 'va_list'. I am passing the
'va_list' two times to a function like 'vprintf' to print
it out.

I thought that this was portable until I came across a
platform (AS/400) where it fails: The 'va_list' is actually
defined as some some kind of an array, so passing it happens
by *reference*: The first user will destroy it and the second
time it does not work.

My question is: Does the standard allow this and AS/400 is
broken or am I invoking undefined behaviour?

Below is a test program

---------------- snip --------------------------
/*
* Test if a va_list argument can be used to traverse the
* variable arguments more than once.
*/
#include <stdio.h>
#include <stdarg.h>

static void scanargs(int dest[2],va_list ap)
{
dest[0] = va_arg(ap,int);
dest[1] = va_arg(ap,int);
}

static void teststdarg(int dest[4], ...)
{
va_list ap;

va_start(ap,dest);

scanargs(dest,ap);
scanargs(dest + 2,ap);

va_end(ap);
}

int main(int argc,char *argv[])
{
int a[4];

teststdarg(a,1,2,3,4);

if((a[0] == 1 && a[1] == 2) &&
(a[2] == 1 && a[3] == 2) || (a[2] == 3 && a[3] == 4)) {
if(a[2] == 1)
printf("We can use a 'va_list' at least twice.\n");
else
printf("WARNING: Reusing 'va_list' gives different results!\n");
} else {
fprintf(stderr,"ERROR: Something is very bad here!\n");
exit(20);
}

return 0;
}
---------------- snap --------------------------
Nov 14 '05 #1
2 3307
Joerg Schoen wrote:
I have a function that gets a 'va_list'. I am passing the
'va_list' two times to a function like 'vprintf' to print
it out.

I thought that this was portable until I came across a
platform (AS/400) where it fails: The 'va_list' is actually
defined as some some kind of an array, so passing it happens
by *reference*: The first user will destroy it and the second
time it does not work.
That's permissible behaviour. You should re-initialize the list
with va_start before attempting to use it again.
static void teststdarg(int dest[4], ...)
{
va_list ap;

va_start(ap,dest);

scanargs(dest,ap);
scanargs(dest + 2,ap);

va_end(ap);
}


This should be written as

static void teststdarg(int dest[4], ...)
{
va_list ap;

va_start(ap,dest);
scanargs(dest,ap);
va_end(ap);

va_start(ap, dest);
scanargs(dest + 2,ap);
va_end(ap);
}

Jeremy.
Nov 14 '05 #2
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> wrote:

That's permissible behaviour. You should re-initialize the list
with va_start before attempting to use it again.


Which is fine if you're creating the va_arg in the first place, but is a
bit difficult if it's comming from somewhere else (e.g., being passed to
you as a parameter). That's why C99 has va_copy().

-Larry Jones

Talk about someone easy to exploit! -- Calvin
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
1
by: Clarice Almeida Hughes | last post by:
tenho um index onde tenho o link pro arq css, como sao visualizados pelo include todas as paginas aderem ao css linkado no index. so q eu preciso de alguns links com outras cores no css, o q devo...
2
by: JW | last post by:
I wanted have this as part of a flood control script: <? echo ("Flood control in place - please wait " . $floodinterval . " seconds between postings."); sleep(5); // go back two pages echo...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.