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
7
votes
3 answers

Replacing gets() with fgets()

I've been testing this struct out and I get warning about using gets. Somebody mentioned to use fgets instead and replace the end with '\0'. Any recommendation how I can change my code to do that? void regCars(Car reg[], int *pNrOfCars) { …
xxFlashxx
  • 261
  • 2
  • 13
7
votes
4 answers

Was gets ever useful?

It seems to me, people, especially when learning the C programming language, are still using the gets function to read in data from stdin. Despite that it has now been removed1 from the C11 standard, and a disclaimer on cppreference reads: The…
bitmask
  • 32,434
  • 14
  • 99
  • 159
6
votes
1 answer

fgets() call with redirection get abnormal data stream

I was about to write a shell with C language. Here is the source code below: #include #include #include #include #include int getcmd(char *buf, int nbuf) { memset(buf, 0, nbuf); fgets(buf,…
sun
  • 95
  • 1
  • 7
6
votes
5 answers

if one complains about gets(), why not do the same with scanf("%s",...)?

From man gets: 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 …
dbarbosa
  • 2,969
  • 5
  • 25
  • 29
6
votes
3 answers

How to timeout gets.chomp

I am trying to write a program that will ask the user to answer a question using gets.chomp in three seconds or the answer will automatically return false. I figured out everything except for the timeout part and I was wondering if anyone could…
pmckinney
  • 63
  • 4
6
votes
5 answers

Why was gets part of the C standard in the first place?

Every C programmer knows there is no way to securely use gets unless standard input is connected to a trusted source. But why didn't the developers of C notice such a glaring mistake before it was made an official part of the C standard? And why did…
flarn2006
  • 1,787
  • 15
  • 37
6
votes
2 answers

Take user input with JavaScript in the console

I need to get user input when running a .js in a console with spidermonkey like this: $ js myprogram.js What's the JavaScript equivalent of Ruby's gets?
alt
  • 13,357
  • 19
  • 80
  • 120
5
votes
5 answers

Is gets() officially deprecated?

Based on the most recent draft of C++11, C++ refers to ISO/IEC 9899:1999/Cor.3:2007(E) for the definitions of the C library functions (per §1.2[intro.refs]/1). Based on the most recent draft of C99 TC3, The gets function is obsolescent, and is…
Cubbi
  • 46,567
  • 13
  • 103
  • 169
5
votes
3 answers

In Ruby, how do I combine sleep with gets? I want to wait for user response for 1 min, otherwise continue

I'm running a loop, in which I wait for a user response using the "gets.chomp" command. How can I combine that with a sleep/timer command? For example. I want it to wait 1 min for the user to enter a word, otherwise it would continue back to the…
RSD
  • 53
  • 3
5
votes
2 answers

Question about "gets" in ruby

I was wondering why when I'm trying to gets to different inputs that it ignores the second input that I had. #!/usr/bin/env ruby #-----Class Definitions---- class Animal attr_accessor :type, :weight end class Dog < Animal attr_accessor :name …
Greg
  • 51
  • 1
  • 1
  • 2
5
votes
2 answers

gets() function and '\0' zero byte in input

Will the gets() function from C language (e.g. from glibc) stop, if it reads a zero byte ('\0') from the file ? Quick test: echo -ne 'AB\0CDE' Thanks. PS this question arises from comments in this question: return to libc - problem PPS the gets…
osgx
  • 90,338
  • 53
  • 357
  • 513
5
votes
1 answer

Exploit Development - GETS and Shellcode

Trying to learn more about exploit dev and building shellcodes, but ran into an issue I don't understand the reason behind. Why am I not able to run a shellcode such as execve("/bin/sh") and spawn a shell I can interact with? While on the other…
Eplox
  • 143
  • 7
5
votes
4 answers

Difference between fgets and gets

What is the difference between fgets() and gets()? I am trying break my loop when the user hits just "enter". It's working well with gets(), but I don't want to use gets(). I tried with fgets() and scanf() but I don't have the same results as with…
pureofpure
  • 1,060
  • 2
  • 12
  • 31
5
votes
1 answer

Ruby: gets.chomp with default value

Is there some simple way how to ask for a user input in Ruby WHILE providing a default value? Consider this code in bash: function ask_q { local PROMPT="$1" local DEF_V="$2" read -e -p "$PROMPT" -i "$DEF_V" REPLY echo…
Petr Cibulka
  • 2,452
  • 4
  • 28
  • 45
5
votes
2 answers

Prevent buffer overflows with gets

The declaration of gets is: char * gets ( char * str ); Note the glaring omission of a maximum size for str. cplusplus.com says2: Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the…
1
2
3
28 29