Why is gets function dangerous




















Phases: Implementation; Requirements Ban the use of dangerous functions. Use their safe equivalent. Weakness Ordinalities. Ordinality Description Primary. This information is often useful in understanding where a weakness fits within the context of external information sources. Taxonomy Mappings. Chapter 5. McGraw-Hill Osborne Media. Chapter 5, "gets and fgets" Page Microsoft Press. Content History. More information is available — Please select a different filter. Page Last Updated: July 20, ID Lookup:.

Class - a weakness that is described in a very abstract fashion, typically independent of any specific language or technology. Use of Prohibited Code. Category - a CWE entry that contains a set of other entries that share a common characteristic. Phase: Testing Use grep or static analysis tools to spot usage of dangerous functions. So I suggest you continue to warn it. For correctness! This problem should be mainly caused by the different encoding of the newline characters in the files of linux and windows.

But the file should be compiled under windows, so there will be a two character mismatch. It is recommended that you can use dos2unix filename in the linux terminal to process the newline character, it should be OK.

Tech Community Register Log in. FaceBook Share. In the previous example this means that if you input "abcdefghijklmnopqrts" maybe, unpredictably, it will overwrite also array2 or whatever.

You should not use gets since it has no way to stop a buffer overflow. If the user types in more data than can fit in your buffer, you will most likely end up with corruption or worse. In fact, ISO have actually taken the step of removing gets from the C standard as of C11, though it was deprecated in C99 which, given how highly they rate backward compatibility, should be an indication of how bad that function was.

The correct thing to do is to use the fgets function with the stdin file handle since you can limit the characters read from the user. To that end, almost every C coder at some point in their career will write a more useful wrapper around fgets as well. Here's mine:. It provides the same protections as fgets in that it prevents buffer overflows but it also notifies the caller as to what happened and clears out the excess characters so that they do not affect your next input operation.

Feel free to use it as you wish, I hereby release it under the "do what you damn well want to" licence If you would, many applications would no longer compile or run at all. This is the reason that one reference gives:. Reading a line that overflows the array pointed to by s results in undefined behavior. The use of fgets is recommended. You'll be happy to know that the committee just voted unanimously, as it turns out to remove gets from the draft as well.

The fgets function allows properly-written programs to safely process input lines too long to store in the result array. It can't detect the end of available memory, so if you allocate an amount of memory too small for the purpose, it can cause a seg fault and crash.

Sometimes it seems very unlikely that a user will type letters into a prompt meant for a person's name, but as programmers, we need to make our programs bulletproof. The C gets function is dangerous and has been a very costly mistake. The whole hour is worth watching but for his comments view from 30 minutes on with the specific gets criticism around 39 minutes. Hopefully this whets your appetite for the whole talk, which draws attention to how we need more formal correctness proofs in languages and how language designers should be blamed for the mistakes in their languages, not the programmer.

This seems to have been the whole dubious reason for designers of bad languages to push the blame to programmers in the guise of 'programmer freedom'.

I would like to extend an earnest invitation to any C library maintainers out there who are still including gets in their libraries "just in case anyone is still depending on it": Please replace your implementation with the equivalent of. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why is the gets function so dangerous that it should not be used? Ask Question. Asked 12 years ago. Active 19 days ago. Viewed k times.

How can I remove this warning and why is there such a warning about using gets? If gets is so dangerous then why can't we remove it? Improve this question. Anne Vinit Dhatrak Vinit Dhatrak 6, 8 8 gold badges 25 25 silver badges 27 27 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. TylerH Thomas Owens Thomas Owens k 94 94 gold badges silver badges bronze badges.

It's actually not gcc which warns, it's the glibc which contains a pragma or attribute on gets that causes the compiler to emit a warning when used.



0コメント

  • 1000 / 1000