C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-06-2008, 06:02 AM   #1
Day Dreamer
 
Join Date: Apr 2007
Posts: 45
Variable Arguments through Command Line

I was called for a Code review and I saw some code as below:
Code:
if(argc!=1)
Whoa!
Quote:
I raised a comment saying, the code doesn't restrict the user from entering any number of arguments he/she wants to and if disguised well, this could account for a buffer overflow!
To this I got the response from the programmer saying:
Quote:
Good Comment but the problem here is that the number of arguments that the program is accepting is variable and so I am finding it hard to implement.
Now my question is, what would be the better way to go?
Implementing a limit on the number of parameters that the user can send (there certainly has to be one as the code cant keep processing all possible values of argv[], I know argv[] is not of constant size anyway.)

I have never seen va_args being used to work on arguments passed from the command line.
__________________
I would love to change the world but they dont give me the source code!
zombiezparadize is offline   Reply With Quote
Old 05-06-2008, 06:05 AM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
argc = Argument count (the amount of arguments in the argv)
argv = Argument Vector (holds arguments)

I don't get your question, you want to stay in bounds? That's easy -- just keep within 0 to argc - 1

If you want to check if element n exists,
Code:
if(n < argc)
    n exists!
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 05-06-2008 at 06:18 AM.
zacs7 is offline   Reply With Quote
Old 05-06-2008, 06:58 AM   #3
Day Dreamer
 
Join Date: Apr 2007
Posts: 45
On going through the post again, yeah I agree it is asking a pretty vague or rather a question that is too obvious.
I was more concerned about the the comparison.
If the programmer were to write what he wrote
Code:
if(argc!=1)
then he should have also defined what should happen if argc=1.

Sorry about the vague post. Too many things in mind cause such blunders.
__________________
I would love to change the world but they dont give me the source code!
zombiezparadize is offline   Reply With Quote
Old 05-06-2008, 07:14 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Actually, if(argc != 1) is "is there any arguments". argc should never be able to be negative, and 0 is invalid, as argv[0] is the name of the program (or something along those lines).

However, there should probably be some "else" or similar to that if-statement - but I guess it is valid to just "do nothing" (or "do some default") if there are no arguments.

As long as argument access is within the bounds of 0 ... (argc-1), then it's valid.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-06-2008, 07:22 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,354
Quote:
and 0 is invalid, as argv[0] is the name of the program (or something along those lines).
0 is technically valid, in which case argv[0] is a null pointer, and that's about all that's defined for the case argc=0.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-06-2008, 07:35 AM   #6
Day Dreamer
 
Join Date: Apr 2007
Posts: 45
Quote:
Originally Posted by matsp View Post

However, there should probably be some "else" or similar to that if-statement - but I
guess it is valid to just "do nothing" (or "do some default") if there are no arguments.

--
Mats
The "do some default" or "do nothing" was exactly what was missing from the code!
Which means when there are no arguments passed to the program the code will merrily
throw SIGSEGV as it is going to access
Code:
 *++argv  /* As used by the programmer though I hate writing such operator precedence 
dependent code. I find it very unreadable*/
Anyway, thank you all for the inputs!
@laserlight: argc=0 . I didn't know that one. Thanks!
__________________
I would love to change the world but they dont give me the source code!
zombiezparadize is offline   Reply With Quote
Reply

Tags
"command line", "variable arguments"

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions with variable argument count.. 39ster C++ Programming 3 04-11-2009 09:18 AM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
Problem with a char variable set as a letter 7smurfs C++ Programming 6 12-10-2004 01:25 PM
Core Dump with Variable Arguments penney C Programming 1 06-20-2003 10:11 AM
(Variable Arguments,...) Sebastiani C Programming 3 09-07-2001 10:44 AM


All times are GMT -6. The time now is 12:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22