From the course: Secure Coding in C
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Solution: Fix the code - C Tutorial
From the course: Secure Coding in C
Solution: Fix the code
(upbeat music) - [Instructor] Here's my solution where I identify and fix the eight issues with this code. Issue #1, the data type returned by the getch function must be an integer, not a character. Issue #2, always confirm that memory has been allocated. Here the buffer pointer is compared with a null constant to confirm that the function didn't err. Appropriate action is taken when memory is unavailable. Issue #3, avoid using getchar as a looping condition, which I'm combining with issue #4, testing for overflow. In the original code, getchar wasn't compared with any value as a condition, which raises the question of how, by itself, it could end the loop? Now the condition keeps looping, while variable index is less than 31, which is one less than the buffer size. The final character is added here, properly capping the string as another security issue, though not covered in this part of the course. Issue #5 is testing for the EOF condition. Remember, input can be redirected. If so…
Contents
-
-
-
-
(Locked)
Formatting preprocessor directives1m 33s
-
(Locked)
Using an assignment as a condition1m 44s
-
(Locked)
Avoiding putchar() in a while loop2m 38s
-
(Locked)
Using the system() call2m 4s
-
(Locked)
Accessing elements beyond the array size2m 38s
-
(Locked)
Converting integers2m 57s
-
(Locked)
Looping with floating point values3m 10s
-
(Locked)
Using return values1m 52s
-
(Locked)
Confirming when EOF has been read3m 34s
-
(Locked)
Challenge: Fix the code1m 20s
-
(Locked)
Solution: Fix the code1m 54s
-
(Locked)
-
-
-
-