Variable Arguments through Command Line

This is a discussion on Variable Arguments through Command Line within the C Programming forums, part of the General Programming Boards category; I was called for a Code review and I saw some code as below: Code: if(argc!=1) Whoa! I raised a ...

  1. #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!
    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:
    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!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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!
    Last edited by zacs7; 05-06-2008 at 06:18 AM.

  3. #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!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #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!

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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