From the course: Secure Coding in C
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Using return values
- [Instructor] Many functions return values, which are often discarded, either out of expediency, or because the programmer assumes everything went well. Always check return values. For example, in this code, an if test at line nine confirms that the malloc function allocated memory. Even for a tiny quantity such as 32 bytes, always test this return value. Ditto for the F open function. Its return value is tested at line nine in this code. Never assume the file point returned is valid. Always test it against the null constant. Further, the F close function returns a value, which is tested here at line 17. It's possible to process an error condition, and determine why the file fails to close, but only when you retain that returned value. Functions that write data to a file return the number of bytes or characters written. This return value must also be checked to confirm that the write operation was successful, which…
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
-
-
-
-
-