From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Fix the code

Solution: Fix the code - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Solution: Fix the code

(upbeat music) - [Instructor] Here is my solution where I identify and fix the eight issues with this code. First, the data type returned by the get char function must be an integer, not a character, and you see this change made at line nine for variable ch. Second, always confirm that memory has been allocated. So at line 15 the buffer pointer is compared with the null constant to confirm that the function didn't err. Appropriate action is taken when memory is unavailable. The third issue is to avoid get char as a looping condition for the wild loop at line 24. In the original code, get char wasn't compared with any value, which raises the question of how by itself could it end the loop? The fourth issue is very important. Nowhere in the loop is overflow tested. So at line 33 I added a statement if the value of variable index is equal to 31, which is the last character position in the buffer, then the loop is broken. This…

Contents