eliminate unnecessary strncpy use.

This commit is contained in:
Enno Rehling 2017-01-06 21:08:49 +01:00
parent f9fbe60766
commit 9463642687
4 changed files with 30 additions and 15 deletions

View File

@ -413,7 +413,8 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc
} }
else { else {
char substr[32]; char substr[32];
strncpy(substr, syntaxp, cstr - syntaxp); assert(sizeof(substr) > (cstr - syntaxp));
memcpy(substr, syntaxp, cstr - syntaxp);
substr[cstr - syntaxp] = 0; substr[cstr - syntaxp] = 0;
locp = LOC(lang, mkname("spellpar", substr)); locp = LOC(lang, mkname("spellpar", substr));
syntaxp = substr + 1; syntaxp = substr + 1;

View File

@ -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) 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; bool firstline;
static char buf[REPORTWIDTH + 1]; // FIXME: static buffer, artificial limit static char buf[REPORTWIDTH + 1]; // FIXME: static buffer, artificial limit
size_t len = strlen(s); size_t len = strlen(s);
@ -883,7 +879,7 @@ void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned
if (!cut) { if (!cut) {
cut = s + _min(len, REPORTWIDTH); cut = s + _min(len, REPORTWIDTH);
} }
strncpy(buf + indent, s, cut - s); memcpy(buf + indent, s, cut - s);
buf[indent + (cut - s)] = 0; buf[indent + (cut - s)] = 0;
addstrlist(SP, buf); // TODO: too much string copying, cut out this function addstrlist(SP, buf); // TODO: too much string copying, cut out this function
while (*cut == ' ') { while (*cut == ' ') {

View File

@ -101,16 +101,23 @@ locale *get_or_create_locale(const char *name)
void make_locales(const char *str) void make_locales(const char *str)
{ {
const char *tok = str; const char *tok = str;
while (*tok) { while (tok) {
char zText[32]; char zText[16];
while (*tok && *tok != ',') size_t len;
++tok;
strncpy(zText, str, tok - str); tok = strchr(str, ',');
zText[tok - str] = 0; if (tok) {
get_or_create_locale(zText); len = tok - str;
if (*tok) { assert(sizeof(zText) > len);
str = ++tok; memcpy(zText, str, len);
str = tok + 1;
} }
else {
len = strlen(str);
memcpy(zText, str, len);
}
zText[len] = 0;
get_or_create_locale(zText);
} }
} }

View File

@ -16,9 +16,20 @@ static void test_language(CuTest *tc)
test_cleanup(); 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 *get_language_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_language); SUITE_ADD_TEST(suite, test_language);
SUITE_ADD_TEST(suite, test_make_locales);
return suite; return suite;
} }