Newbie: bug in argument casting to digit

From: Jobs'R'Us (kirstenjmacdonald_at_hotmail.com)
Date: 08/16/04


Date: 16 Aug 2004 06:52:01 -0700

Hello,

I have a bug in a simple validation for an argument that must be an
int from 1 to 99.

If in the command prompt I enter the following, the program behaves
perfectly:
programname filename.txt doghouse 5

If in the command prompt I enter the following, the program still
behaves perfectly, it explains that the digit ("5all") is incorrect:
programname filename.txt doghouse 5all

The bug is, if I enter the following, the program incorrectly accepts
"5a" and interprets it exactly like like "5":
programname filename.txt doghouse 5a

Someone kindly point out what I'm doing wrong?

Also, I would like to know why gcc gives me a warning about the line
with atoi() being used implicitely?

The code:

// too much in a hurry to remove the ones that do not apply to this
code snippet:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

//arguments expected: <a file name> <a string> <a small int>
int main(int argc, char *argv[])
{

         FILE *file;
        char searchedword[50];
        int searcheddigit;

        if(argc != 4)
            {
                printf("Sorry, wrong number of parameters...\n\n");
                printf("Usage: blah blah blah\n\n");
                return 1;
        }
        else if(strlen(argv[2]) > 50)
        {
                printf("Sorry, the searched word is too long...\n\n");
                printf("Usage: blah blah blah\n\n");
                return 2;
        }
        else if(strlen(argv[3]) > 2)
        {
                printf("Sorry, the number is wrong, must be a digit from 1 to
99...\n\n");
                printf("Usage: blah blah blah\n\n");
                return 3;
        }
        else
        {
                file = fopen(argv[1], "r");

                if(file == NULL)
                {
                        printf("Sorry, can't open the indicated file.\n\n");
                        printf("Usage: blah blah blah\n\n");
                        return 4;
                  }
                  else
                  {
                        strcpy(searchedword,argv[2]);
                        searcheddigit = atoi(argv[3]);

                        if((searcheddigit < 1) || (searcheddigit > 99))
                        {

                                printf("Sorry, the number of words to the left and to the right,
needs to be a positive digit between 1 and 99...\n\n");
                                printf("Usage: blah blah blah\n\n");
                                return 5;
                        }
/* Program continues... */

Thank you for the help and please keep your explanations geared for a
seven year old!
Kirst



Relevant Pages

  • Re: Newbie: bug in argument casting to digit
    ... >If in the command prompt I enter the following, ... >programname filename.txt doghouse 5all ... >The bug is, if I enter the following, the program incorrectly accepts ...
    (comp.lang.c)
  • [PATCH] Memory management livelock
    ... Here is a patch for MM livelock. ... The original bug report follows after the ... I declared a mutex starvation_barrier in struct ... int nr_pages; ...
    (Linux-Kernel)
  • Re: What *is* a CtrlID?
    ... It was a bug we knew ... This dates back to the days when C only had one type, 'int', which it ... This is an infinite loop (and it is subtle to detect, ... I found that just using signed integer even for data that ...
    (microsoft.public.vc.mfc)
  • Re: [Bug #12263] Sata soft reset filling log
    ... The following bug entry is on the current list of known regressions ... int tries = 5; ... * @buf: data buffer ...
    (Linux-Kernel)
  • Re: [PATCH 2/2] tracing/events/lockdep: move tracepoints within recursive protection
    ... RSP ... Either there's a bug in the vbin_printf, or we have some crazy lock->name? ... TP_PROTO(struct lockdep_map *lock, unsigned int subclass, ... The trace points in lockdep are called all the time. ...
    (Linux-Kernel)