fix a bug in the escape_string function.

remove static return variable from escape_string.
add test coverage.
This commit is contained in:
Enno Rehling 2014-08-24 22:54:40 +02:00
parent 920902f8eb
commit a3037950ff
15 changed files with 340 additions and 312 deletions

View File

@ -490,11 +490,12 @@ static void report_crtypes(FILE * F, const struct locale *lang)
for (kmt = mtypehash[i]; kmt; kmt = kmt->nexthash) {
const struct nrmessage_type *nrt = nrt_find(lang, kmt->mtype);
if (nrt) {
char buffer[DISPLAYSIZE];
unsigned int hash = kmt->mtype->key;
assert(hash > 0);
fprintf(F, "MESSAGETYPE %u\n", hash);
fputc('\"', F);
fputs(escape_string(nrt_string(nrt), NULL, 0), F);
fputs(escape_string(nrt_string(nrt), buffer, sizeof(buffer)), F);
fputs("\";text\n", F);
fprintf(F, "\"%s\";section\n", nrt_section(nrt));
}

View File

@ -314,7 +314,7 @@ helpmode helpmodes[] = {
*/
const char *dbrace(const struct race *rc)
{
static char zText[32];
static char zText[32]; // FIXME: static return value
char *zPtr = zText;
/* the english names are all in ASCII, so we don't need to worry about UTF8 */
@ -974,7 +974,7 @@ int modifier)
#ifndef NDEBUG
const char *strcheck(const char *s, size_t maxlen)
{
static char buffer[16 * 1024];
static char buffer[16 * 1024]; // FIXME: static return value
if (strlen(s) > maxlen) {
assert(maxlen < 16 * 1024);
log_warning("[strcheck] String wurde auf %d Zeichen verkürzt:\n%s\n", (int)maxlen, s);
@ -1911,7 +1911,7 @@ int check_param(const struct param *p, const char *key, const char *searchvalue)
static const char *g_datadir;
const char *datapath(void)
{
static char zText[MAX_PATH];
static char zText[MAX_PATH]; // FIXME: static return value
if (g_datadir)
return g_datadir;
return strcat(strcpy(zText, basepath()), "/data");
@ -1925,7 +1925,7 @@ void set_datapath(const char *path)
static const char *g_reportdir;
const char *reportpath(void)
{
static char zText[MAX_PATH];
static char zText[MAX_PATH]; // FIXME: static return value
if (g_reportdir)
return g_reportdir;
return strcat(strcpy(zText, basepath()), "/reports");

View File

@ -54,7 +54,7 @@ static const char *make_names(const char *monster, int *num_postfix,
int pprefix, int *num_name, int *num_prefix, int ppostfix)
{
int uv, uu, un;
static char name[NAMESIZE + 1];
static char name[NAMESIZE + 1]; // FIXME: static return value
char zText[32];
const char *str;
@ -222,7 +222,7 @@ static const char *generic_name(const unit * u)
static const char *dragon_name(const unit * u)
{
static char name[NAMESIZE + 1];
static char name[NAMESIZE + 1]; // FIXME: static return value
int rnd, ter = 0;
int anzahl = 1;
static int num_postfix;
@ -355,7 +355,7 @@ static const char *drac_suf[DRAC_SUF] = {
static const char *dracoid_name(const unit * u)
{
static char name[NAMESIZE + 1];
static char name[NAMESIZE + 1]; // FIXME: static return value
int mid_syllabels;
u = u;

View File

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -93,7 +93,8 @@ struct race *get_race(race_t rt) {
cache = cache_breaker;
memset(race_cache, 0, sizeof(race_cache));
return race_cache[rt] = rc_get_or_create(name);
} else {
}
else {
result = race_cache[rt];
if (!result) {
result = race_cache[rt] = rc_get_or_create(name);
@ -130,7 +131,7 @@ void racelist_clear(struct race_list **rl)
void racelist_insert(struct race_list **rl, const struct race *r)
{
race_list *rl2 = (race_list *) malloc(sizeof(race_list));
race_list *rl2 = (race_list *)malloc(sizeof(race_list));
rl2->data = r;
rl2->next = *rl;
@ -142,7 +143,7 @@ void free_races(void) {
while (races) {
race * rc = races->next;
free(races);
races =rc;
races = rc;
}
}
@ -233,7 +234,7 @@ const char *rc_name(const race * rc, name_t n)
default: assert(!"invalid name_t enum in rc_name");
}
if (postfix) {
static char name[64]; // FIXME: static variable return
static char name[64]; // FIXME: static return value
sprintf(name, "race::%s%s", rc->_name, postfix);
return name;
}
@ -257,7 +258,7 @@ const char *racename(const struct locale *loc, const unit * u, const race * rc)
const char *str, *prefix = raceprefix(u);
if (prefix != NULL) {
static char lbuf[80];
static char lbuf[80]; // FIXME: static return value
char *bufp = lbuf;
size_t size = sizeof(lbuf) - 1;
int ch, bytes;
@ -282,7 +283,7 @@ const char *racename(const struct locale *loc, const unit * u, const race * rc)
int
rc_specialdamage(const race * ar, const race * dr,
const struct weapon_type *wtype)
const struct weapon_type *wtype)
{
race_t art = old_race(ar);
int m, modifier = 0;
@ -330,7 +331,8 @@ variant read_race_reference(struct storage *store)
if (strcmp(zName, "none") == 0) {
result.v = NULL;
return result;
} else {
}
else {
result.v = rc_find_i(zName);
}
assert(result.v != NULL);

View File

@ -107,7 +107,7 @@ const char *combatstatus[] = {
const char *report_kampfstatus(const unit * u, const struct locale *lang)
{
static char fsbuf[64];
static char fsbuf[64]; // FIXME: static return value
strlcpy(fsbuf, LOC(lang, combatstatus[u->status]), sizeof(fsbuf));
if (fval(u, UFL_NOAID)) {
@ -1928,7 +1928,7 @@ f_regionid(const region * r, const faction * f, char *buffer, size_t size)
static char *f_regionid_s(const region * r, const faction * f)
{
static int i = 0;
static char bufs[4][NAMESIZE + 20];
static char bufs[4][NAMESIZE + 20]; // FIXME: static return value
char *buf = bufs[(++i) % 4];
f_regionid(r, f, buf, NAMESIZE + 20);
@ -2177,7 +2177,7 @@ static void eval_race(struct opstack **stack, const void *userdata)
static void eval_order(struct opstack **stack, const void *userdata)
{ /* order -> string */
const struct order *ord = (const struct order *)opop(stack).v;
static char buf[512];
char buf[512];
size_t len;
variant var;
@ -2193,7 +2193,7 @@ static void eval_resources(struct opstack **stack, const void *userdata)
const faction *report = (const faction *)userdata;
const struct locale *lang = report ? report->locale : default_locale;
const struct resource *res = (const struct resource *)opop(stack).v;
static char buf[1024]; /* but we only use about half of this */
char buf[1024]; /* but we only use about half of this */
size_t size = sizeof(buf) - 1;
variant var;
@ -2225,7 +2225,7 @@ static void eval_regions(struct opstack **stack, const void *userdata)
int i = opop(stack).i;
int end, begin = opop(stack).i;
const arg_regions *regions = (const arg_regions *)opop(stack).v;
static char buf[256];
char buf[256];
size_t size = sizeof(buf) - 1;
variant var;
char *bufp = buf;
@ -2262,7 +2262,7 @@ static void eval_trail(struct opstack **stack, const void *userdata)
const struct locale *lang = report ? report->locale : default_locale;
int i, end = 0, begin = 0;
const arg_regions *regions = (const arg_regions *)opop(stack).v;
static char buf[512];
char buf[512];
size_t size = sizeof(buf) - 1;
variant var;
char *bufp = buf;

View File

@ -241,7 +241,7 @@ static faction *factionorders(void)
}
else {
log_warning("orders for invalid faction %s\n", itoa36(fid));
log_debug("orders for invalid faction %s\n", itoa36(fid));
}
return f;
}

View File

@ -12,7 +12,7 @@
const char * keyword(keyword_t kwd)
{
static char result[KEYWORDSIZE];
static char result[KEYWORDSIZE]; // FIXME: static return value
if (!result[0]) {
strcpy(result, "keyword::");
}

View File

@ -100,7 +100,7 @@ extern int months_per_year;
static char *gamedate_season(const struct locale *lang)
{
static char buf[256];
static char buf[256]; // FIXME: static return value
gamedate gd;
get_gamedate(turn, &gd);
@ -517,7 +517,7 @@ void sparagraph(strlist ** SP, const char *s, int indent, char mark)
int i, j, width;
int firstline;
static char buf[REPORTWIDTH + 1];
static char buf[REPORTWIDTH + 1]; // FIXME: static return value
width = REPORTWIDTH - indent;
firstline = 1;

View File

@ -34,6 +34,7 @@ int RunAllTests(void)
ADD_TESTS(suite, bsdstring);
ADD_TESTS(suite, functions);
ADD_TESTS(suite, umlaut);
ADD_TESTS(suite, strings);
/* kernel */
ADD_TESTS(suite, faction);
ADD_TESTS(suite, build);

View File

@ -2,6 +2,7 @@ project(util C)
SET(_TEST_FILES
base36.test.c
strings.test.c
bsdstring.test.c
functions.test.c
umlaut.test.c

View File

@ -56,7 +56,7 @@ int atoi36(const char *str)
const char *itoab(int i, int base)
{
static char **as = NULL; /* STATIC_RETURN: used for return, not across calls */
static char **as = NULL; // FIXME: static return value
char *s, *dst;
static int index = 0; /* STATIC_XCALL: used across calls */
int neg = 0;

View File

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -59,7 +59,8 @@ locale *get_or_create_locale(const char *name)
if (!locales) {
nextlocaleindex = 0;
} else {
}
else {
while (*lp && (*lp)->hashkey != hkey) lp = &(*lp)->next;
if (*lp) {
return *lp;
@ -179,7 +180,8 @@ void locale_setstring(locale * lang, const char *key, const char *value)
find->hashkey = hkey;
find->key = _strdup(key);
find->str = _strdup(value);
} else {
}
else {
if (strcmp(find->str, value) != 0) {
log_warning("multiple translations for key %s\n", key);
}
@ -197,7 +199,8 @@ char *mkname_buf(const char *space, const char *name, char *buffer)
{
if (space && *space) {
sprintf(buffer, "%s::%s", space, name);
} else {
}
else {
strcpy(buffer, name);
}
return buffer;
@ -205,7 +208,7 @@ char *mkname_buf(const char *space, const char *name, char *buffer)
const char *mkname(const char *space, const char *name)
{
static char zBuffer[128]; /* STATIC_RESULT: used for return, not across calls */
static char zBuffer[128]; // FIXME: static return value
return mkname_buf(space, name, zBuffer);
}
@ -237,7 +240,7 @@ void free_locales(void)
int i;
locale * next = locales->next;
for (i=0; i!=SMAXHASH; ++i) {
for (i = 0; i != SMAXHASH; ++i) {
while (locales->strings[i]) {
struct locale_str * strings = locales->strings[i];
free(strings->key);

View File

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* libc includes */
#include <string.h>
#include <assert.h>
unsigned int hashstring(const char *s)
{
@ -34,16 +35,13 @@ const char *escape_string(const char *str, char *buffer,
unsigned int len)
{
const char *start = strchr(str, '\"');
if (!start) start = strchr(str, '\\');
assert(buffer);
if (start) {
static char s_buffer[4096]; /* STATIC_RESULT: used for return, not across calls */
const char *p;
char *o;
size_t skip = start - str;
if (buffer == NULL) {
buffer = s_buffer;
len = sizeof(s_buffer);
}
memcpy(buffer, str, skip);
o = buffer + skip;
p = str + skip;
@ -55,7 +53,8 @@ const char *escape_string(const char *str, char *buffer,
}
(*o++) = '\\';
len -= 2;
} else {
}
else {
if (len < 1) {
*o = '\0';
break;

21
src/util/strings.test.c Normal file
View File

@ -0,0 +1,21 @@
#include <CuTest.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "goodies.h"
static void test_escape_string(CuTest * tc)
{
char scratch[64];
CuAssertStrEquals(tc, "hello world", escape_string("hello world", scratch, sizeof(scratch)));
CuAssertStrEquals(tc, "hello \\\"world\\\"", escape_string("hello \"world\"", scratch, sizeof(scratch)));
CuAssertStrEquals(tc, "\\\"\\\\", escape_string("\"\\", scratch, sizeof(scratch)));
CuAssertStrEquals(tc, "\\\\", escape_string("\\", scratch, sizeof(scratch)));
}
CuSuite *get_strings_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_escape_string);
return suite;
}

View File

@ -30,7 +30,7 @@
#ifdef USE_LIBXML2
const xmlChar *xml_i(double number)
{
static char buffer[128];
static char buffer[128]; // FIXME: static return value
_snprintf(buffer, sizeof(buffer), "%.0f", number);
return (const xmlChar *)buffer;
}