Skip to content

Commit 4c1d66a

Browse files
CopilotJohnAmadis
andcommitted
Use Dmod_SnPrintf for safer string copying with guaranteed null-termination
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent 4734c29 commit 4c1d66a

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/dmdevfs.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,14 @@ static void read_dir_name_from_path(const char* path, char* dir_name, size_t nam
10611061
// Extract the directory name
10621062
const char* name_start = (last_slash != NULL) ? last_slash + 1 : path;
10631063
size_t name_len = len - (name_start - path);
1064-
size_t copy_len = (name_len < name_size - 1) ? name_len : name_size - 1;
1065-
strncpy(dir_name, name_start, copy_len);
1066-
dir_name[copy_len] = '\0';
1064+
1065+
// Use snprintf for safe copying with guaranteed null-termination
1066+
int written = Dmod_SnPrintf(dir_name, name_size, "%.*s", (int)name_len, name_start);
1067+
if (written < 0 || (size_t)written >= name_size)
1068+
{
1069+
// Truncated, but snprintf ensures null-termination
1070+
dir_name[name_size - 1] = '\0';
1071+
}
10671072
}
10681073

10691074
/**

0 commit comments

Comments
 (0)