From d71d5e413a1eeba0e509397a1ba97969fd233fc9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 6 Jan 2017 21:08:49 +0100 Subject: [PATCH] eliminate unnecessary strncpy use. --- src/report.c | 3 ++- src/reports.c | 6 +----- src/util/language.c | 25 ++++++++++++++++--------- src/util/language.test.c | 11 +++++++++++ 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/report.c b/src/report.c index 34da143de..4e12f1261 100644 --- a/src/report.c +++ b/src/report.c @@ -413,7 +413,8 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc } else { char substr[32]; - strncpy(substr, syntaxp, cstr - syntaxp); + assert(sizeof(substr) > (cstr - syntaxp)); + memcpy(substr, syntaxp, cstr - syntaxp); substr[cstr - syntaxp] = 0; locp = LOC(lang, mkname("spellpar", substr)); syntaxp = substr + 1; diff --git a/src/reports.c b/src/reports.c index 8ffb823a5..fa8a722d9 100644 --- a/src/reports.c +++ b/src/reports.c @@ -849,10 +849,6 @@ const struct unit * u, struct skill * sv, int *dh, int days) void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned int width, char mark) { - - /* Die Liste SP wird mit dem String s aufgefuellt, mit indent und einer - * mark, falls angegeben. SP wurde also auf 0 gesetzt vor dem Aufruf. - * Vgl. spunit (). */ bool firstline; static char buf[REPORTWIDTH + 1]; // FIXME: static buffer, artificial limit size_t len = strlen(s); @@ -883,7 +879,7 @@ void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned if (!cut) { cut = s + _min(len, REPORTWIDTH); } - strncpy(buf + indent, s, cut - s); + memcpy(buf + indent, s, cut - s); buf[indent + (cut - s)] = 0; addstrlist(SP, buf); // TODO: too much string copying, cut out this function while (*cut == ' ') { diff --git a/src/util/language.c b/src/util/language.c index d8b899742..cb91d7419 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -101,16 +101,23 @@ locale *get_or_create_locale(const char *name) void make_locales(const char *str) { const char *tok = str; - while (*tok) { - char zText[32]; - while (*tok && *tok != ',') - ++tok; - strncpy(zText, str, tok - str); - zText[tok - str] = 0; - get_or_create_locale(zText); - if (*tok) { - str = ++tok; + while (tok) { + char zText[16]; + size_t len; + + tok = strchr(str, ','); + if (tok) { + len = tok - str; + assert(sizeof(zText) > len); + memcpy(zText, str, len); + str = tok + 1; } + else { + len = strlen(str); + memcpy(zText, str, len); + } + zText[len] = 0; + get_or_create_locale(zText); } } diff --git a/src/util/language.test.c b/src/util/language.test.c index 236eac497..40a6775fa 100644 --- a/src/util/language.test.c +++ b/src/util/language.test.c @@ -16,9 +16,20 @@ static void test_language(CuTest *tc) test_cleanup(); } +static void test_make_locales(CuTest *tc) +{ + test_setup(); + make_locales("aa,bb,cc"); + CuAssertPtrNotNull(tc, get_locale("aa")); + CuAssertPtrNotNull(tc, get_locale("bb")); + CuAssertPtrNotNull(tc, get_locale("cc")); + test_cleanup(); +} + CuSuite *get_language_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_language); + SUITE_ADD_TEST(suite, test_make_locales); return suite; }