Skip to content

Commit

Permalink
desktop.c: purge strncpy() and replace with custom pstrcpy()
Browse files Browse the repository at this point in the history
  • Loading branch information
01micko authored and johanmalm committed Jun 18, 2024
1 parent efed019 commit 6f60e87
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ static char name_llcc[64] = { 0 };
static char generic_name_ll[64] = { 0 };
static char generic_name_llcc[64] = { 0 };

/*
* This snippet borrowed from qemu
* Copyright (C) Fabrice Bellard 2006-2017 GPL-2.0
* https://github.com/qemu/qemu/blob/master/util/cutils.c
* Replaces strncpy
*/
void
pstrcpy(char *buf, int buf_size, const char *str)
{
int c;
char *q = buf;

if (buf_size <= 0)
return;

for(;;) {
c = *str++;
if (c == 0 || q >= buf + buf_size - 1)
break;
*q++ = c;
}
*q = '\0';
}

static void
i18n_init(void)
{
Expand All @@ -48,14 +72,14 @@ i18n_init(void)
}

/* ll_CC */
strncpy(llcc, p, sizeof(llcc));
pstrcpy(llcc, sizeof(llcc), p);
p = strrchr(llcc, '.');
if (p) {
*p = '\0';
}

/* ll */
strncpy(ll, llcc, sizeof(ll));
pstrcpy(ll, sizeof(ll), llcc);
p = strrchr(ll, '_');
if (p) {
*p = '\0';
Expand Down Expand Up @@ -303,8 +327,8 @@ process_file(char *filename, const char *path)
return;
}
size_t len = strlen(path);
strncpy(fullname, path, sizeof(fullname));
strncpy(fullname + len, filename, sizeof(fullname) - len);
pstrcpy(fullname, sizeof(fullname), path);
pstrcpy(fullname + len, sizeof(fullname) - len, filename);
FILE *fp = fopen(fullname, "r");
if (!fp) {
fprintf(stderr, "warn: could not open file %s", filename);
Expand Down

0 comments on commit 6f60e87

Please sign in to comment.