diff options
author | EuAndreh <eu@euandre.org> | 2021-06-28 08:39:32 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-06-28 09:48:01 -0300 |
commit | 17a6c7bda9ec97c03153ebfabf3a99c9f66dc7f5 (patch) | |
tree | 398ee27665ffde668fef461b21f5d2f64883659c /src | |
parent | src/remembering-c.c: Use better grouping of parentheses for strcmp() (diff) | |
download | remembering-17a6c7bda9ec97c03153ebfabf3a99c9f66dc7f5.tar.gz remembering-17a6c7bda9ec97c03153ebfabf3a99c9f66dc7f5.tar.xz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/remembering-c.c | 10 |
1 files changed, 5 insertions, 5 deletions
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); |