Skip to content

Commit 6f60e87

Browse files
01mickojohanmalm
01micko
authored andcommitted
desktop.c: purge strncpy() and replace with custom pstrcpy()
1 parent efed019 commit 6f60e87

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

desktop.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ static char name_llcc[64] = { 0 };
2626
static char generic_name_ll[64] = { 0 };
2727
static char generic_name_llcc[64] = { 0 };
2828

29+
/*
30+
* This snippet borrowed from qemu
31+
* Copyright (C) Fabrice Bellard 2006-2017 GPL-2.0
32+
* https://github.com/qemu/qemu/blob/master/util/cutils.c
33+
* Replaces strncpy
34+
*/
35+
void
36+
pstrcpy(char *buf, int buf_size, const char *str)
37+
{
38+
int c;
39+
char *q = buf;
40+
41+
if (buf_size <= 0)
42+
return;
43+
44+
for(;;) {
45+
c = *str++;
46+
if (c == 0 || q >= buf + buf_size - 1)
47+
break;
48+
*q++ = c;
49+
}
50+
*q = '\0';
51+
}
52+
2953
static void
3054
i18n_init(void)
3155
{
@@ -48,14 +72,14 @@ i18n_init(void)
4872
}
4973

5074
/* ll_CC */
51-
strncpy(llcc, p, sizeof(llcc));
75+
pstrcpy(llcc, sizeof(llcc), p);
5276
p = strrchr(llcc, '.');
5377
if (p) {
5478
*p = '\0';
5579
}
5680

5781
/* ll */
58-
strncpy(ll, llcc, sizeof(ll));
82+
pstrcpy(ll, sizeof(ll), llcc);
5983
p = strrchr(ll, '_');
6084
if (p) {
6185
*p = '\0';
@@ -303,8 +327,8 @@ process_file(char *filename, const char *path)
303327
return;
304328
}
305329
size_t len = strlen(path);
306-
strncpy(fullname, path, sizeof(fullname));
307-
strncpy(fullname + len, filename, sizeof(fullname) - len);
330+
pstrcpy(fullname, sizeof(fullname), path);
331+
pstrcpy(fullname + len, sizeof(fullname) - len, filename);
308332
FILE *fp = fopen(fullname, "r");
309333
if (!fp) {
310334
fprintf(stderr, "warn: could not open file %s", filename);

0 commit comments

Comments
 (0)