From 3a40244cb9bc163dada1bb5cfa3d8aa267b2c76f Mon Sep 17 00:00:00 2001 From: 01micko <01micko@gmail.com> Date: Sun, 16 Jun 2024 16:54:58 +1000 Subject: [PATCH] desktop.c: purge strncpy() and replace with custom pstrcpy() --- desktop.c | 29 +++++++++++++++++++++++++++-- desktop.h | 1 + 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/desktop.c b/desktop.c index 28c7a47..81f5ec9 100644 --- a/desktop.c +++ b/desktop.c @@ -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) { @@ -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'; diff --git a/desktop.h b/desktop.h index 5f7720f..6fad5c2 100644 --- a/desktop.h +++ b/desktop.h @@ -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);