|
From: Jan M. <jan...@vo...> - 2007-07-17 15:03:32
|
Thanks, the compiler wrote also
the gets () should not be used.
But to be honest I did not know
why.
But the truth is I have seen it cannot be checked by any chance
how many characters will be send to the string.
Jan Mura
----- Original Message -----
From: "Per Westermark" <pw...@ia...>
To: "Adam Jones" <aj...@i-...>
Cc: <dev...@li...>
Sent: Tuesday, July 17, 2007 1:36 PM
Subject: Re: [Dev-C++] Problems with input
> I really don't understand your talk about malloc() and checking for
> sucessful allocation here.
>
> If doing
> char str = (char*)malloc(10);
> then the allocation may fail, the the application should test:
> if (str == NULL) fail;
> or
> if (!str) fail;
>
> However, when declaring:
> char str[10];
> then the only way the application can fail is if the operating system
> can't supply enough stack space. There are no pointer to test. If the
> allocation fails (the stack can't fit the variable) then the application
> will die.
>
>
> However, you are correct that gets() should not be used. Or more strongly:
> SHOULD NEVER BE USED. If doing C-style input, the function to use is
> fgets() since it takes an extra parameter to inform about the size of the
> input buffer. gets() is one of the hacker dream functions. If gets() is
> used, then the application is compromised.
>
> /pwm
>
> On Mon, 16 Jul 2007, Adam Jones wrote:
>
> > >>Hello,
> >
> > >>I have got a problem I don't understand very much.
> >
> > >>I have in my code:
> >
> > >>#include<stdio.h>
> > >>int main()
> > >>{
> > >>char str[10];
> >
> > >>getchar ();
> > >>gets (str);
> > >>printf ("%s\n", str);
> > >>return 0;
> > >>}
> >
> > It would be helpful to all of us if you could explain why you feel it
> > necessary to use getchar() the gets() the way you do. What are you
trying to
> > accomplish by doing it this way.
> > getchar() waits for any keypress to continue, and gets will wait until
it
> > encounters the newline or end of file before continuing.
> >
> > You also have a potential to corrupt memory here. What happens if the
person
> > enters a string longer than 10 Char. You also assume which is not very
good
> > that the system will allocate a char str[10] without checking to see if
it
> > can do it with a malloc first. On memory allocations you need to check
> > first, and if it fails exit your program gracefully.
> >
> > >>In this case it seems the program is ignoring the "gets ()" command.
After
> > I press some key and >CR>It stops and doesn't wait for line input. If I
do
> > not have there the "getchar () I >>can input a line.
> >
> > If you hit any key and the CR then the single key press goes to
getchar()
> > the <CR> goes to the gets() which terminates that call. You should just
hit
> > any key, then type in your string and then hit CR when you are done. The
> > getchar() does not require you to hit CR in order for it to accept the
key
> > press. This should work.
> >
> > -Adam Jones
> >
> >
>
> -------------------------------------------------------------------------
> > This SF.net email is sponsored by DB2 Express
> > Download DB2 Express C - the FREE version of DB2 express and take
> > control of your XML. No limits. Just data. Click to get it now.
> > http://sourceforge.net/powerbar/db2/
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> __________ Informace od NOD32 2399 (20070714) __________
>
> Tato zprava byla proverena antivirovym systemem NOD32.
> http://www.nod32.cz
>
>
|