From 674a86e596b36394c3b25549ad1ff2e35a2cc3cc Mon Sep 17 00:00:00 2001 From: tmeissner Date: Tue, 27 May 2014 23:06:32 +0200 Subject: [PATCH] added NULL checks after malloc --- c_pointers/chapter_5.c | 55 ++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/c_pointers/chapter_5.c b/c_pointers/chapter_5.c index 1f4583b..b58a7b1 100644 --- a/c_pointers/chapter_5.c +++ b/c_pointers/chapter_5.c @@ -52,20 +52,23 @@ int main(void) { // pointer to char (with memory allocation) char *headerPtr = malloc(strlen("Media Player") + 1); - strcpy(headerPtr, "Media Player"); - printf("%s\n", headerPtr); - - *headerPtr = 'W'; - *(headerPtr + 1) = 'u'; - *(headerPtr + 2) = 'r'; - *(headerPtr + 3) = 's'; - *(headerPtr + 4) = 't'; - *(headerPtr + 5) = '\0'; - printf("%s\n", headerPtr); - for (size_t j = 0; j < strlen(headerPtr); j++) { - printf("headerPtr[%zu] Address: %p Value: %c\n", j, &headerPtr[j], headerPtr[j]); + if (headerPtr != NULL) { + strcpy(headerPtr, "Media Player"); + printf("%s\n", headerPtr); + + *headerPtr = 'W'; + *(headerPtr + 1) = 'u'; + *(headerPtr + 2) = 'r'; + *(headerPtr + 3) = 's'; + *(headerPtr + 4) = 't'; + *(headerPtr + 5) = '\0'; + printf("%s\n", headerPtr); + for (size_t j = 0; j < strlen(headerPtr); j++) { + printf("headerPtr[%zu] Address: %p Value: %c\n", j, &headerPtr[j], headerPtr[j]); + } + printf("\n"); + free(headerPtr); } - printf("\n"); // direct init with character pointer // don't create a new copy, instead a reference to string literal pool only @@ -78,10 +81,12 @@ int main(void) { // char *prefix = '+'; // better: char *prefix = malloc(2); - *prefix = '+'; - *(prefix + 1) = 0; - printf("%s\n", prefix); - free(prefix); + if (prefix != NULL) { + *prefix = '+'; + *(prefix + 1) = 0; + printf("%s\n", prefix); + free(prefix); + } // string initialisation from standard in // don't forget to allocate memory before read from standard in @@ -91,16 +96,20 @@ int main(void) { // scanf("%s", command); // better: char *command = malloc(11); - printf("%s: ", "Enter a command"); - scanf("%10s", command); - printf("You entered: %s with length: %zu\n", command, strlen(command)); - free(command); + if (command != NULL) { + printf("%s: ", "Enter a command"); + scanf("%10s", command); + printf("You entered: %s with length: %zu\n", command, strlen(command)); + free(command); + } // memset buffer overflow test // executes without warnings or errors on osx :D char *mem = malloc(11); - memset(mem, 0x42, 64); - free(mem); + if (mem != NULL) { + memset(mem, 0x42, 64); + free(mem); + } }