From the course: Secure Coding in C (2019)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Solution: Fix the code - C Tutorial
From the course: Secure Coding in C (2019)
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Formatting preprocessor directives1m 23s
-
(Locked)
Using an assignment as a condition1m 31s
-
(Locked)
Avoiding putchar() in a while loop1m 35s
-
(Locked)
Using the system() call1m 58s
-
(Locked)
Accessing elements beyond the array size2m 2s
-
(Locked)
Converting integers2m 26s
-
(Locked)
Looping with floating point values1m 39s
-
(Locked)
Using return values1m 58s
-
(Locked)
Confirming when EOF has been read2m 31s
-
(Locked)
Challenge: Fix the code1m 12s
-
(Locked)
Solution: Fix the code2m 12s
-
-
-
-
-