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 committed Jun 16, 2024
1 parent efed019 commit 3a40244
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
29 changes: 27 additions & 2 deletions desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ static char name_llcc[64] = { 0 };
static char generic_name_ll[64] = { 0 };
static char generic_name_llcc[64] = { 0 };

/*
* this snippet stolen from Fabrice Bellard
* as used in qemu
* https://github.com/qemu/qemu/blob/master/util/cutils.c
* as an attempt to replace 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 +73,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
1 change: 1 addition & 0 deletions desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct app {
bool has_been_mapped;
};

void pstrcpy(char *buf, int buf_size, const char *str);
/* desktop_entries_create - parse system .desktop files */
GList *desktop_entries_create(void);
void desktop_entries_destroy(GList *apps);
Expand Down

0 comments on commit 3a40244

Please sign in to comment.