Skip to content

Commit 6f9c366

Browse files
committed
fs: do not use str[n]{cpy,cat}
use snprintf[_ch] to simplify and possibly make safer
1 parent d5c0130 commit 6f9c366

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

src/utils/fs.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Martin Bela <492789@mail.muni.cz>
55
*/
66
/*
7-
* Copyright (c) 2018-2025 CESNET
7+
* Copyright (c) 2018-2026 CESNET, zájmové sdružení právnických osob
88
* All rights reserved.
99
*
1010
* Redistribution and use in source and binary forms, with or without
@@ -87,13 +87,10 @@ const char *get_temp_dir(void)
8787
}
8888
#else
8989
const char *req_tmp_dir = getenv("TMPDIR");
90-
if (req_tmp_dir) {
91-
temp_dir[sizeof temp_dir - 1] = '\0';
92-
strncpy(temp_dir, req_tmp_dir, sizeof temp_dir - 1);
93-
} else {
94-
strcpy(temp_dir, P_tmpdir);
90+
if (!req_tmp_dir) {
91+
req_tmp_dir = P_tmpdir;
9592
}
96-
strncat(temp_dir, "/", sizeof temp_dir - strlen(temp_dir) - 1);
93+
snprintf_ch(temp_dir, "%s/", req_tmp_dir);
9794
#endif
9895

9996
return temp_dir;
@@ -274,8 +271,7 @@ FILE *get_temp_file(const char **filename) {
274271
}
275272
return ret;
276273
#else
277-
strncpy(filename_buf, get_temp_dir(), sizeof filename_buf - 1);
278-
strncat(filename_buf, "/uv.XXXXXX", sizeof filename_buf - strlen(filename_buf) - 1);
274+
snprintf_ch(filename_buf, "%s%s", get_temp_dir(), "/uv.XXXXXX");
279275
umask(S_IRWXG|S_IRWXO);
280276
int fd = mkstemp(filename_buf);
281277
if (fd == -1) {
@@ -302,9 +298,8 @@ char *strdup_path_with_expansion(const char *orig_path) {
302298
return strdup(orig_path);
303299
}
304300
char *new_path = malloc(MAX_PATH_SIZE);
305-
new_path[MAX_PATH_SIZE - 1] = '\0';
306301
assert(new_path != NULL);
307-
strncpy(new_path, home, MAX_PATH_SIZE - 1);
308-
strncat(new_path, orig_path + 1, MAX_PATH_SIZE - strlen(new_path) - 1);
302+
int ret = snprintf(new_path, MAX_PATH_SIZE, "%s%s", home, orig_path + 1);
303+
assert(ret < MAX_PATH_SIZE);
309304
return new_path;
310305
}

0 commit comments

Comments
 (0)