This preview shows page 1. Sign up to view the full content.
Unformatted text preview: n. It reads a line from the standard input, stopping when either a terminating newline character or some error condition is encountered. It copies this string to the location designated by argument s, and terminates the string with a null character. We show the use of gets in the function echo, which simply reads a line from standard input and echos it back to standard output. 168 CHAPTER 3. MACHINE-LEVEL REPRESENTATION OF C PROGRAMS
Stack Frame for caller Return Address %ebp Saved %ebp     buf Stack Frame for echo Figure 3.27: Stack Organization for echo Function. Character array buf is just below part of the saved state. An out-of-bounds write to buf can corrupt the program state. The problem with gets is that it has no way to determine whether sufﬁcient space has been allocated to hold the entire string. In our echo example, we have purposely made the buffer very small—just four characters long. Any string longer than three characters will cause an out-of-bounds write. Examining a portion of the assembly code for echo shows how the stack is organized.
1 2 3 4 5 6 7 8 9 echo: pushl %ebp movl %esp,%ebp subl $20,%esp pushl %ebx addl $-12,%esp l...
View Full Document
This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.
- Spring '10
- The American