954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Strange bug with gets()

Hey

Im doing a loop with somthing similar to this:

char stn[25];
do
{
  printf ("Insert a number:\n");
  gets(stn);
}while (stn!="123");
if (str=="123")
{
 printf ("Good value!");
}


Now if I input 123, it enters the if that prints "Good value!" and the program ends naturally.

Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432 (all of this it would continue inside the do while loop) and finally I enter 123, it prints "Good value!" but the program itself crashes.

Very strange. I think it is a error with the buffer overfilling or something similar. I tried with fflush(stdin) before/after/both in the gets() loop but nothing.

How can I solve this?

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 1
 

Hey

Im doing a loop with somthing similar to this:

char stn[25];
do
{
  printf ("Insert a number:\n");
  gets(stn);
}while (stn!="123");
if (str=="123")
{
 printf ("Good value!");
}

Now if I input 123, it enters the if that prints "Good value!" and the program ends naturally.

Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432 (all of this it would continue inside the do while loop) and finally I enter 123, it prints "Good value!" but the program itself crashes.

Very strange. I think it is a error with the buffer overfilling or something similar. I tried with fflush(stdin) before/after/both in the gets() loop but nothing.

How can I solve this?


You can't use this

stn!="123"
str=="123"


Try looking up strcmp or the string library of functions

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

You can't use this

stn!="123"
str=="123"

Try looking up strcmp or the string library of functions

It is not a literal example :) Im very sorry that I did not state that.

void valid(int stringi,int *var)
 {
     char strings[20];
     itoa(stringi,strings,10);

        if (strlen(strings)==8)
        {
            if ((stringi>=11111111) || (stringi<=99999999))
            {
                    printf ("Valid!\n");
                    (*var)=1;
            }
        }
        else
        {
            printf ("Invalid!\n");
            (*var)=0;
        }
 }

 void insertstring(char *string)
 {
     int var=0;
     do
     {
        fflush(stdin);
        printf ("Insert:\n");
        gets(string);
        //fgets(string,sizeof(&string),stdin);
        valid(atoi(string),&var);
     }while(var==0);
 }

This is a quick rough up of my real example. fgets is something I tried as well but it doesnt work correctly.

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 1
 

Here's what my manpages say about gets

BUGS Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Try running this code...What do you notice about the output. Is match different than str?

#include <stdio.h>
#include <stdlib.h>

char match[] = "123";

int main(int argc, char**argv)
{
	char str[25];

	fputs("enter a value->", stdout);
	fgets(str, 25, stdin);
	

	fputs(str, stdout);
	fputs(match, stdout);


	exit(EXIT_SUCCESS);
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

>>Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432

gets() is a horrible function because if the input buffer is not large enough to hold all the characters you type then it will just scribble any excess characters all over memory. So if you typed that all on one line before hitting the key then gets() will crash your program. gets() doesn't stop at the first spaces, but only when '\n' is encountered.

A better way to do it is to use fgets(), which will limit the input to the size of the input buffer. The problem with fgets() is that it may put the '\n' in the buffer too, which you will have to remove.
fgets(stn, sizeof(stn), stdin);


[edit]gerard ^^^ posted the correct example.[/edit]

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

An additional thing that looks real bad and may be contributing to your problems ..

fflush(stdin);

you might read Why fflush(stdin) is wrong .

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Try running this code...What do you notice about the output. Is match different than str?

#include <stdio.h>
#include <stdlib.h>

char match[] = "123";

int main(int argc, char**argv)
{
	char str[25];

	fputs("enter a value->", stdout);
	fgets(str, 25, stdin);
	

	fputs(str, stdout);
	fputs(match, stdout);


	exit(EXIT_SUCCESS);
}


Well it seems that in the main function it works always but using 2 functions, it doesnt. So if you dont mind a example using 2 functions :)>>Now if I input 32432, 23432, 23234, 1233,9898, 87343, 238432

gets() is a horrible function because if the input buffer is not large enough to hold all the characters you type then it will just scribble any excess characters all over memory. So if you typed that all on one line before hitting the key then gets() will crash your program. gets() doesn't stop at the first spaces, but only when '\n' is encountered.

A better way to do it is to use fgets(), which will limit the input to the size of the input buffer. The problem with fgets() is that it may put the '\n' in the buffer too, which you will have to remove.
fgets(stn, sizeof(stn), stdin);
If I want it to only get 8 characters, I should set it to 9? (To include the /0 character)

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 1
 
Title: Strange bug with gets()

Answer: the "gets" function *IS* a bug. kill it before it breeds.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
Answer: the "gets" function *IS* a bug. kill it before it breeds.


The problem was that the size I was passing in the first function was a incorrect size and it had problems.

This is something simple so gets works OK for this :)

riahc3
Posting Pro
545 posts since May 2008
Reputation Points: 50
Solved Threads: 1
 

The problem was that the size I was passing in the first function was a incorrect size and it had problems.

This is something simple so gets works OK for this :)


NO. You're unfortunately, understandably, but undeniably WRONG.

the PROBLEM was that you were usinggets() in the first place.

gets() is never OK. ever. so get it out of your head.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: