fix a previously undetected bug in str_strlcpy.

This commit is contained in:
Enno Rehling 2018-11-27 20:01:47 +01:00
parent a597d06484
commit 3be5211a53
3 changed files with 66 additions and 12 deletions

19
scripts/write-reports.lua Normal file
View File

@ -0,0 +1,19 @@
local path = 'scripts'
if config.install then
path = config.install .. '/' .. path
end
package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua'
require 'eressea'
require 'eressea.xmlconf' -- read xml data
local rules = {}
if config.rules then
rules = require('eressea.' .. config.rules)
eressea.log.info('loaded ' .. #rules .. ' modules for ' .. config.rules)
else
eressea.log.warning('no rule modules loaded, specify a game in eressea.ini or with -r')
end
eressea.read_game(get_turn() .. '.dat')
init_reports()
write_reports()

View File

@ -78,7 +78,7 @@ size_t str_strlcpy(char *dst, const char *src, size_t len)
if (n == 0) {
if (len != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++);
return (d - dst); /* count does not include NUL */
}
return (s - src - 1); /* count does not include NUL */
@ -285,6 +285,7 @@ void sbs_strcat(struct sbstring *sbs, const char *str)
len = sbs->size - (sbs->end - sbs->begin);
len = str_strlcpy(sbs->end, str, len);
sbs->end += len;
assert(sbs->begin + sbs->size >= sbs->end);
}
void sbs_strcpy(struct sbstring *sbs, const char *str)
@ -294,23 +295,27 @@ void sbs_strcpy(struct sbstring *sbs, const char *str)
len = sbs->size - 1;
}
sbs->end = sbs->begin + len;
assert(sbs->begin + sbs->size >= sbs->end);
}
void sbs_cut(sbstring *sbp, int bytes)
void sbs_cut(sbstring *sbs, int bytes)
{
if (bytes > 0) {
size_t len = sbs_length(sbp) - bytes;
memmove(sbp->begin, sbp->begin + bytes, len + 1);
sbp->end = sbp->begin + len;
size_t len = sbs_length(sbs) - bytes;
memmove(sbs->begin, sbs->begin + bytes, len + 1);
sbs->end = sbs->begin + len;
}
else if (bytes < 0) {
size_t len = sbs_length(sbp) + bytes;
sbp->end = sbp->begin + len;
size_t len = sbs_length(sbs) + bytes;
sbs->end = sbs->begin + len;
}
assert(sbs->begin + sbs->size >= sbs->end);
assert(sbs->end[0] == '\0');
}
size_t sbs_length(const struct sbstring *sbs)
{
assert(sbs->begin + sbs->size >= sbs->end);
return sbs->end - sbs->begin;
}

View File

@ -115,16 +115,21 @@ static void test_str_strlcpy(CuTest * tc)
memset(buffer, 0x7f, sizeof(buffer));
CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 4));
CuAssertStrEquals(tc, "her", buffer);
CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 8)); /*-V666 */
CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 8));
CuAssertStrEquals(tc, "herp", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[5]);
CuAssertIntEquals(tc, 8, (int)str_strlcpy(buffer, "herpderp", 8));
CuAssertIntEquals(tc, 3, (int)str_strlcpy(buffer, "herp", 4));
CuAssertStrEquals(tc, "her", buffer);
CuAssertIntEquals(tc, 7, (int)str_strlcpy(buffer, "herpderp", 8));
CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[8]);
CuAssertIntEquals(tc, 8, (int)str_strlcpy(buffer, "herpderp", 16));
CuAssertStrEquals(tc, "herpderp", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[9]);
errno = 0;
}
@ -162,6 +167,30 @@ static void test_sbstring(CuTest * tc)
CuAssertStrEquals(tc, "123456789012345", sbs.begin);
}
static void test_sbs_strcat(CuTest * tc)
{
char buffer[8];
sbstring sbs;
sbs_init(&sbs, buffer, sizeof(buffer));
sbs_strcat(&sbs, "AB");
CuAssertStrEquals(tc, "AB", sbs.begin);
sbs_strcat(&sbs, "CD");
CuAssertStrEquals(tc, "ABCD", sbs.begin);
sbs_init(&sbs, buffer, sizeof(buffer));
sbs_strcat(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "1234567", sbs.begin);
sbs_strcat(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "1234567", sbs.begin);
sbs_init(&sbs, buffer, sizeof(buffer));
sbs_strncat(&sbs, "12345678901234567890", 4);
CuAssertStrEquals(tc, "1234", sbs.begin);
sbs_strncat(&sbs, "12345678901234567890", 4);
CuAssertStrEquals(tc, "1234123", sbs.begin);
}
CuSuite *get_strings_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -175,5 +204,6 @@ CuSuite *get_strings_suite(void)
SUITE_ADD_TEST(suite, test_str_strlcpy);
SUITE_ADD_TEST(suite, test_str_itoa);
SUITE_ADD_TEST(suite, test_sbstring);
SUITE_ADD_TEST(suite, test_sbs_strcat);
return suite;
}