From 17a6c7bda9ec97c03153ebfabf3a99c9f66dc7f5 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 28 Jun 2021 08:39:32 -0300 Subject: src/remembering-c.c: Fix usage of strcat() As it turns out, after doing a malloc(), strcat() can't be used directly on top of it. I'd guess that the implementation of strcat() goes through the first string until its end, the \0 NULL terminator, and then starts copying the characters to the new string, overwriting the existing terminator to allow the string to actually be continuous. However, strcat() can't do this in a string that was just malloced and has no content. So it will probably run over the end of the memory untill it finds a NULL byte, and concatenate starting from that. I didn't check this elsewhere, but this explanation sounds reasonable, and Valgrind's diagnostics seem to confirm that. The solution is to strcpy() the first string, and than strcat() the others after that, so that strcat() has an existing string to work on, while strcpy doesn't need that. Fixes #task-bd165b74-c559-48ee-1d29-eaa906aa0393. --- src/remembering-c.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/remembering-c.c') diff --git a/src/remembering-c.c b/src/remembering-c.c index 889dae2..b67e71c 100644 --- a/src/remembering-c.c +++ b/src/remembering-c.c @@ -79,7 +79,7 @@ static char *get_profile_file(const char *const profile, FILE *stream) { perror("get_profile_file() - malloc() - home_with_suffix"); return NULL; } - strcat(home_with_suffix, home); + strcpy(home_with_suffix, home); strcat(home_with_suffix, suffix); home = home_with_suffix; } else { @@ -100,7 +100,7 @@ static char *get_profile_file(const char *const profile, FILE *stream) { free(home); return NULL; } - strcat(profile_file, home); + strcpy(profile_file, home); strcat(profile_file, "/"); strcat(profile_file, profile); @@ -130,7 +130,7 @@ static void test_get_profile_file() { sizeof(char); char *const expected = malloc(expected_size); assert(expected); - strcat(expected, HOME); + strcpy(expected, HOME); strcat(expected, SUFFIX); strcat(expected, A_PROFILE); @@ -167,7 +167,7 @@ static void test_get_profile_file() { sizeof(char); char *const expected = malloc(expected_size); assert(expected); - strcat(expected, HOME); + strcpy(expected, HOME); strcat(expected, SUFFIX); strcat(expected, A_PROFILE); @@ -286,7 +286,7 @@ static void test_mkdir_p() { sizeof(char); char *const subdirectory = malloc(subdirectory_size); assert(subdirectory); - strcat(subdirectory, prefix); + strcpy(subdirectory, prefix); strcat(subdirectory, SUBDIRECTORY_SUFFIX); assert(stat(subdirectory, &s) == -1 && errno == ENOENT); -- cgit v1.2.3