Questions tagged [gets]

Anything related to C or C++ standard library functions gets (C) or std::gets (C++). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string. DO NOT USE THESE FUNCTIONS: they are deprecated, and since C11 gets is no longer part of the standard.

Anything related to C or C++ standard library functions gets (defined in <stdio.h> C standard header) or std::gets (defined in <cstdio> C++ standard header). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string. There are no constraints on the functions to prevent them writing outside the bounds of the array that is passed.

DO NOT USE THESE FUNCTIONS: they are dangerous and deprecated for security reasons because they are susceptible for easily causing buffer overflow. For more information about why the use of gets is harmful, consider the link below.

Since C11 (ISO/IEC 9899:2011) gets has been removed from the C standard library. Annex K of the C standard defines an optional replacement function called gets_s for backwards-compatibility reasons, but makes the following recommendation to use fgets whenever possible:

ISO 9899:2011 K.3.5.4.1

Recommended practice

The fgets function allows properly-written programs to safely process input lines too long to store in the result array. In general this requires that callers of fgets pay attention to the presence or absence of a new-line character in the result array. Consider using fgets (along with any needed processing based on new-line characters) instead of gets_s.

Note that gets_s() is not generally available except on Windows using the Microsoft C library.

See CPPreference.com:

422 questions
306
votes
13 answers

Why is the gets function so dangerous that it should not be used?

When I try to compile C code that uses the gets() function with GCC, I get this warning: (.text+0x34): warning: the `gets' function is dangerous and should not be used. I remember this has something to do with stack protection and security, but…
Vinit Dhatrak
  • 6,814
  • 8
  • 27
  • 30
145
votes
3 answers

What is going on with 'gets(stdin)' on the site coderbyte?

Coderbyte is an online coding challenge site (I found it just 2 minutes ago). The first C++ challenge you are greeted with has a C++ skeleton you need to modify: #include #include using namespace std; int FirstFactorial(int…
bolov
  • 72,283
  • 15
  • 145
  • 224
48
votes
7 answers

C - scanf() vs gets() vs fgets()

I've been doing a fairly easy program of converting a string of Characters (assuming numbers are entered) to an Integer. After I was done, I noticed some very peculiar "bugs" that I can't answer, mostly because of my limited knowledge of how the…
Marko
  • 491
  • 1
  • 5
  • 6
25
votes
0 answers

Safe Alternative to gets

I wanna read a whole line from standard input, including the whitespace between two words. When using gets on gcc I get the following message: send.c:(.text+0x2a): warning: the `gets' function is dangerous and should not be used. What's a better…
andandandand
  • 21,946
  • 60
  • 170
  • 271
18
votes
2 answers

Implicit declaration of 'gets'

I understand that an 'implicit declaration' usually means that the function must be placed at the top of the program before calling it or that I need to declare the prototype. However, gets should be in the stdio.h files (which I have included). Is…
Death_by_Ch0colate
  • 379
  • 1
  • 2
  • 7
16
votes
2 answers

Why is gets throwing an error when arguments are passed to my ruby script?

I'm using gets to pause my script's output until the user hits the enter key. If I don't pass any arguments to my script then it works fine. However, if I pass any arguments to my script then gets dies with the following error: ruby main.rb…
Herms
  • 37,540
  • 12
  • 78
  • 101
14
votes
10 answers

What's the difference between gets and scanf?

If the code is scanf("%s\n",message) vs gets(message) what's the difference?It seems that both of them get input to message.
Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
13
votes
3 answers

Why can I use gets() in gcc -std=c11?

The gets() function has been removed from the C language. No such function exists in the standard. Yet I compile the following code: #include int main (void) { (void) gets (NULL); } using gcc -std=c11 -pedantic-errors -Wall…
Lundin
  • 195,001
  • 40
  • 254
  • 396
13
votes
4 answers

Ruby 'gets' that works over multiple lines

Using the IRB, I want to enter a multiple line string in order to strip certain characters from it. "gets" only allows a single line - is there a similar function for multiple lines. ASCII_project.rb(main):002:0* puts = "What's the text you want to…
Joseph
  • 914
  • 2
  • 14
  • 29
11
votes
2 answers

scanf("%[^\n]s",a) vs gets(a)

I have been told that scanf should not be used when user inputs a string. Instead, go for gets() by most of the experts and also the users on StackOverflow. I never asked it on StackOverflow why one should not use scanf over gets for strings. This…
niko
  • 9,285
  • 27
  • 84
  • 131
11
votes
8 answers

Input in C. Scanf before gets. Problem

I'm pretty new to C, and I have a problem with inputing data to the program. My code: #include #include #include int main(void) { int a; char b[20]; printf("Input your ID: "); scanf("%d", &a); …
Dmitri
  • 2,451
  • 6
  • 35
  • 55
10
votes
1 answer

exploiting Buffer Overflow using gets() in a simple C program

I am new to Buffer Overflow exploits and I started with a simple C program. Code #include #include void execs(void){ printf("yay!!"); } void return_input (void) { char array[30]; gets(array); } int main() { …
Panther Coder
  • 1,058
  • 1
  • 16
  • 43
10
votes
5 answers

Is there any function to get an unlimited input string from standard input

The condition is: I want to input a line from standard input, and I don't know the size of it, maybe very long. method like scanf, getsneed to know the max length you may input, so that your input size is less than your buffer size. So Is there any…
Daizy
  • 331
  • 2
  • 5
  • 12
8
votes
10 answers

Disable warning: the `gets' function is dangerous in GCC through header files?

I am using the function gets() in my C code. My code is working fine but I am getting a warning message (.text+0xe6): warning: the `gets' function is dangerous and should not be used. I want this warning message not to pop up. Is there any way? I…
Biswajyoti Das
  • 7,881
  • 11
  • 34
  • 26
7
votes
5 answers

Recovering from a broken TCP socket in Ruby when in gets()

I'm reading lines of input on a TCP socket, similar to this: class Bla def getcmd @sock.gets unless @sock.closed? end def start srv = TCPServer.new(5000) @sock = srv.accept while ! @sock.closed? ans = getcmd …
asussex
  • 163
  • 3
  • 5
1
2 3
28 29