Merge branch 'stacksize' into develop

This commit is contained in:
Enno Rehling 2018-11-27 21:55:24 +01:00
commit d8868315da
26 changed files with 373 additions and 95 deletions

View file

@ -29,7 +29,10 @@ function test_undead_reserve_other()
u1:clear_orders() u1:clear_orders()
u1:add_order("RESERVIERE 1 Holz") u1:add_order("RESERVIERE 1 Holz")
process_orders() process_orders()
-- Intermittent Failure (clang): expected 0 but was 2
assert_equal(0, u1:get_item("log")) assert_equal(0, u1:get_item("log"))
assert_equal(2, u2:get_item("log")) assert_equal(2, u2:get_item("log"))
end end

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

@ -271,8 +271,11 @@ static int *keys_update(int *base, int key, int val)
else { else {
int sz = keys_size(n); int sz = keys_size(n);
if (n + 1 > sz) { if (n + 1 > sz) {
void * tmp;
sz = keys_size(n + 1); sz = keys_size(n + 1);
base = realloc(base, (sz * 2 + 1) * sizeof(int)); tmp = realloc(base, (sz * 2 + 1) * sizeof(int));
if (!tmp) abort();
base = (int *)tmp;
} }
base[0] = n + 1; base[0] = n + 1;
kv = keys_get(base, l); kv = keys_get(base, l);

View file

@ -54,6 +54,7 @@ static const terrain_type *chaosterrain(void)
} }
if (numtypes > 0) { if (numtypes > 0) {
types = malloc(sizeof(terrain_type *) * numtypes); types = malloc(sizeof(terrain_type *) * numtypes);
if (!types) abort();
numtypes = 0; numtypes = 0;
for (terrain = terrains(); terrain != NULL; terrain = terrain->next) { for (terrain = terrains(); terrain != NULL; terrain = terrain->next) {
if ((terrain->flags & LAND_REGION) && terrain->herbs) { if ((terrain->flags & LAND_REGION) && terrain->herbs) {

View file

@ -17,9 +17,12 @@ without prior permission by the authors of Eressea.
/* tweakable features */ /* tweakable features */
#define RENDER_CRMESSAGES #define RENDER_CRMESSAGES
#define BUFFERSIZE 32768
#define RESOURCECOMPAT #define RESOURCECOMPAT
#define BUFFERSIZE 32768
/* riesig, wegen spionage-messages :-( */
static char g_bigbuf[BUFFERSIZE];
#include <spells/regioncurse.h> #include <spells/regioncurse.h>
/* modules include */ /* modules include */
@ -132,9 +135,12 @@ static const char *translate(const char *key, const char *value)
t = junkyard; t = junkyard;
junkyard = junkyard->next; junkyard = junkyard->next;
} }
else else {
t = malloc(sizeof(translation)); t = malloc(sizeof(translation));
if (!t) abort();
}
t->key = str_strdup(key); t->key = str_strdup(key);
if (!t->key) abort();
t->value = value; t->value = value;
t->next = translation_table[kk]; t->next = translation_table[kk];
translation_table[kk] = t; translation_table[kk] = t;
@ -264,13 +270,12 @@ cr_output_curses(struct stream *out, const faction * viewer, const void *obj, ob
msg = msg_curse(c, obj, typ, self); msg = msg_curse(c, obj, typ, self);
if (msg) { if (msg) {
char buf[BUFFERSIZE];
if (!header) { if (!header) {
header = 1; header = 1;
stream_printf(out, "EFFECTS\n"); stream_printf(out, "EFFECTS\n");
} }
nr_render(msg, viewer->locale, buf, sizeof(buf), viewer); nr_render(msg, viewer->locale, g_bigbuf, sizeof(g_bigbuf), viewer);
stream_printf(out, "\"%s\"\n", buf); stream_printf(out, "\"%s\"\n", g_bigbuf);
msg_release(msg); msg_release(msg);
} }
a = a->next; a = a->next;
@ -571,28 +576,26 @@ static void render_messages(FILE * F, faction * f, message_list * msgs)
{ {
struct mlist *m = msgs->begin; struct mlist *m = msgs->begin;
while (m) { while (m) {
char crbuffer[BUFFERSIZE]; /* gross, wegen spionage-messages :-( */
bool printed = false; bool printed = false;
const struct message_type *mtype = m->msg->type; const struct message_type *mtype = m->msg->type;
unsigned int hash = mtype->key; unsigned int hash = mtype->key;
#ifdef RENDER_CRMESSAGES #ifdef RENDER_CRMESSAGES
char nrbuffer[1024 * 32]; g_bigbuf[0] = '\0';
nrbuffer[0] = '\0'; if (nr_render(m->msg, f->locale, g_bigbuf, sizeof(g_bigbuf), f) > 0) {
if (nr_render(m->msg, f->locale, nrbuffer, sizeof(nrbuffer), f) > 0) {
fprintf(F, "MESSAGE %d\n", message_id(m->msg)); fprintf(F, "MESSAGE %d\n", message_id(m->msg));
fprintf(F, "%u;type\n", hash); fprintf(F, "%u;type\n", hash);
fwritestr(F, nrbuffer); fwritestr(F, g_bigbuf);
fputs(";rendered\n", F); fputs(";rendered\n", F);
printed = true; printed = true;
} }
#endif #endif
crbuffer[0] = '\0'; g_bigbuf[0] = '\0';
if (cr_render(m->msg, crbuffer, (const void *)f) == 0) { if (cr_render(m->msg, g_bigbuf, (const void *)f) == 0) {
if (crbuffer[0]) { if (g_bigbuf[0]) {
if (!printed) { if (!printed) {
fprintf(F, "MESSAGE %d\n", message_id(m->msg)); fprintf(F, "MESSAGE %d\n", message_id(m->msg));
} }
fputs(crbuffer, F); fputs(g_bigbuf, F);
} }
} }
else { else {
@ -605,6 +608,7 @@ static void render_messages(FILE * F, faction * f, message_list * msgs)
kmt = kmt->nexthash; kmt = kmt->nexthash;
if (kmt == NULL) { if (kmt == NULL) {
kmt = (struct known_mtype *)malloc(sizeof(struct known_mtype)); kmt = (struct known_mtype *)malloc(sizeof(struct known_mtype));
if (!kmt) abort();
kmt->nexthash = mtypehash[ihash]; kmt->nexthash = mtypehash[ihash];
kmt->mtype = mtype; kmt->mtype = mtype;
mtypehash[ihash] = kmt; mtypehash[ihash] = kmt;
@ -1218,7 +1222,7 @@ cr_borders(const region * r, const faction * f, seen_mode mode, FILE * F)
void cr_output_resources(stream *out, const faction * f, const region *r, bool see_unit) void cr_output_resources(stream *out, const faction * f, const region *r, bool see_unit)
{ {
char cbuf[BUFFERSIZE], *pos = cbuf; char *pos = g_bigbuf;
resource_report result[MAX_RAWMATERIALS]; resource_report result[MAX_RAWMATERIALS];
int n, size = report_resources(r, result, MAX_RAWMATERIALS, f, see_unit); int n, size = report_resources(r, result, MAX_RAWMATERIALS, f, see_unit);
@ -1251,8 +1255,8 @@ void cr_output_resources(stream *out, const faction * f, const region *r, bool s
result[n].level); result[n].level);
} }
} }
if (pos != cbuf) { if (pos != g_bigbuf) {
swrite(cbuf, 1, pos - cbuf, out); swrite(g_bigbuf, 1, pos - g_bigbuf, out);
} }
} }

View file

@ -31,8 +31,8 @@ extern "C" {
struct param; struct param;
struct _dictionary_; struct _dictionary_;
#define DISPLAYSIZE 8192 /* max. L<>nge einer Beschreibung, incl trailing 0 */ #define DISPLAYSIZE 4096 /* max. L<>nge einer Beschreibung, incl trailing 0 */
#define ORDERSIZE (DISPLAYSIZE*2) /* max. length of an order */ #define ORDERSIZE 4096 /* max. length of an order */
#define NAMESIZE 128 /* max. L<>nge eines Namens, incl trailing 0 */ #define NAMESIZE 128 /* max. L<>nge eines Namens, incl trailing 0 */
#define IDSIZE 16 /* max. L<>nge einer no (als String), incl trailing 0 */ #define IDSIZE 16 /* max. L<>nge einer no (als String), incl trailing 0 */
#define OBJECTIDSIZE (NAMESIZE+5+IDSIZE) /* max. L<>nge der Strings, die #define OBJECTIDSIZE (NAMESIZE+5+IDSIZE) /* max. L<>nge der Strings, die

View file

@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/language.h> #include <util/language.h>
#include <util/log.h> #include <util/log.h>
#include <util/path.h> #include <util/path.h>
#include <util/password.h>
#include "eressea.h" #include "eressea.h"
#ifdef USE_CURSES #ifdef USE_CURSES
@ -231,6 +232,10 @@ static int parse_args(int argc, char **argv)
i = get_arg(argc, argv, 2, i, &arg, 0); i = get_arg(argc, argv, 2, i, &arg, 0);
turn = atoi(arg); turn = atoi(arg);
break; break;
case 'w':
i = get_arg(argc, argv, 2, i, &arg, 0);
bcrypt_workfactor = arg ? atoi(arg) : 0xff;
break;
case 'q': case 'q':
verbosity = 0; verbosity = 0;
break; break;

View file

@ -105,6 +105,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
/* pre-C99 compatibility */
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)(-1))
#endif
#define ECHECK_VERSION "4.01" #define ECHECK_VERSION "4.01"
extern int *storms; extern int *storms;
@ -1521,29 +1526,81 @@ static int count_allies_cb(struct allies *all, faction *af, int status, void *ud
struct show_s { struct show_s {
sbstring sbs; sbstring sbs;
stream *out;
const faction *f; const faction *f;
int num_allies; int num_allies;
int num_listed;
size_t maxlen;
}; };
/* TODO: does not test for non-ascii unicode spaces. */
#define IS_UTF8_SPACE(pos) (*pos > 0 && *pos <= CHAR_MAX && isspace(*pos))
void pump_paragraph(sbstring *sbp, stream *out, size_t maxlen, bool isfinal)
{
while (sbs_length(sbp) > maxlen) {
char *pos, *begin = sbp->begin;
while (*begin && IS_UTF8_SPACE(begin)) {
/* eat whitespace */
++begin;
}
pos = begin;
while (pos) {
char *next = strchr(pos+1, ' ');
if (next == NULL) {
if (isfinal) {
swrite(begin, 1, sbp->end - begin, out);
newline(out);
}
return;
}
else if (next > begin + maxlen) {
ptrdiff_t len = pos - begin;
swrite(begin, 1, len, out);
newline(out);
while (*pos && IS_UTF8_SPACE(pos)) {
++pos;
++len;
}
sbs_substr(sbp, len, SIZE_MAX);
break;
}
pos = next;
}
}
if (isfinal) {
char *pos = sbp->begin;
while (*pos && IS_UTF8_SPACE(pos)) {
/* eat whitespace */
++pos;
}
swrite(pos, 1, sbp->end - pos, out);
newline(out);
}
}
static int show_allies_cb(struct allies *all, faction *af, int status, void *udata) { static int show_allies_cb(struct allies *all, faction *af, int status, void *udata) {
struct show_s * show = (struct show_s *)udata; struct show_s * show = (struct show_s *)udata;
const faction * f = show->f; const faction * f = show->f;
sbstring *sbp = &show->sbs;
int mode = alliance_status(f, af, status); int mode = alliance_status(f, af, status);
--show->num_allies;
if (sbs_length(&show->sbs) > 0) { if (show->num_listed++ != 0) {
/* not the first entry */ if (show->num_listed == show->num_allies) {
if (0 == show->num_allies) { /* last entry */
sbs_strcat(&show->sbs, LOC(f->locale, "list_and")); sbs_strcat(sbp, LOC(f->locale, "list_and"));
} }
else { else {
sbs_strcat(&show->sbs, ", "); /* neither first entry nor last*/
sbs_strcat(sbp, ", ");
} }
} }
sbs_strcat(&show->sbs, factionname(af)); sbs_strcat(sbp, factionname(af));
sbs_strcat(&show->sbs, " ("); pump_paragraph(sbp, show->out, show->maxlen, false);
sbs_strcat(sbp, " (");
if ((mode & HELP_ALL) == HELP_ALL) { if ((mode & HELP_ALL) == HELP_ALL) {
sbs_strcat(&show->sbs, LOC(f->locale, parameters[P_ANY])); sbs_strcat(sbp, LOC(f->locale, parameters[P_ANY]));
} }
else { else {
int h, hh = 0; int h, hh = 0;
@ -1573,58 +1630,60 @@ static int show_allies_cb(struct allies *all, faction *af, int status, void *uda
} }
if (p != MAXPARAMS) { if (p != MAXPARAMS) {
if (hh) { if (hh) {
sbs_strcat(&show->sbs, ", "); sbs_strcat(sbp, ", ");
} }
sbs_strcat(&show->sbs, LOC(f->locale, parameters[p])); sbs_strcat(sbp, LOC(f->locale, parameters[p]));
hh = 1; hh = 1;
} }
} }
} }
sbs_strcat(&show->sbs, ")"); if (show->num_allies == show->num_listed) {
sbs_strcat(sbp, ").");
pump_paragraph(sbp, show->out, show->maxlen, true);
}
else {
sbs_strcat(sbp, ")");
pump_paragraph(sbp, show->out, show->maxlen, false);
}
return 0; return 0;
} }
static void void report_allies(struct stream *out, size_t maxlen, const struct faction * f, struct allies * allies, const char *prefix)
show_allies(const faction * f, struct allies * allies, char *buf, size_t size)
{ {
int num_allies = 0; int num_allies = 0;
assert(maxlen <= REPORTWIDTH);
allies_walk(allies, count_allies_cb, &num_allies); allies_walk(allies, count_allies_cb, &num_allies);
if (num_allies > 0) { if (num_allies > 0) {
struct show_s show; struct show_s show;
char buf[REPORTWIDTH * 2];
show.f = f; show.f = f;
show.out = out;
show.num_allies = num_allies; show.num_allies = num_allies;
sbs_init(&show.sbs, buf, size); show.num_listed = 0;
show.maxlen = maxlen;
sbs_init(&show.sbs, buf, sizeof(buf));
sbs_strcpy(&show.sbs, prefix);
allies_walk(allies, show_allies_cb, &show); allies_walk(allies, show_allies_cb, &show);
sbs_strcat(&show.sbs, ".");
} }
} }
static void allies(struct stream *out, const faction * f) static void allies(struct stream *out, const faction * f)
{ {
const group *g = f->groups; const group *g = f->groups;
char buf[16384]; char prefix[64];
if (f->allies) { if (f->allies) {
int bytes; snprintf(prefix, sizeof(prefix), "%s ", LOC(f->locale, "faction_help"));
size_t size = sizeof(buf); report_allies(out, REPORTWIDTH, f, f->allies, prefix);
bytes = snprintf(buf, size, "%s ", LOC(f->locale, "faction_help"));
size -= bytes;
show_allies(f, f->allies, buf + bytes, size);
paragraph(out, buf, 0, 0, 0);
newline(out);
} }
while (g) { while (g) {
if (g->allies) { if (g->allies) {
int bytes; snprintf(prefix, sizeof(prefix), "%s %s ", g->name, LOC(f->locale, "group_help"));
size_t size = sizeof(buf); report_allies(out, REPORTWIDTH, f, g->allies, prefix);
bytes = snprintf(buf, size, "%s %s ", g->name, LOC(f->locale, "group_help"));
size -= bytes;
show_allies(f, g->allies, buf + bytes, size);
paragraph(out, buf, 0, 0, 0);
newline(out);
} }
g = g->next; g = g->next;
} }

View file

@ -14,21 +14,27 @@
#define H_GC_REPORT #define H_GC_REPORT
#include <stddef.h> #include <stddef.h>
#include <stdbool.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
struct stream; struct stream;
struct sbstring;
struct spellbook_entry; struct spellbook_entry;
struct region; struct region;
struct faction; struct faction;
struct locale; struct locale;
struct allies;
void register_nr(void); void register_nr(void);
void report_cleanup(void); void report_cleanup(void);
void write_spaces(struct stream *out, size_t num); void write_spaces(struct stream *out, size_t num);
void report_travelthru(struct stream *out, struct region * r, const struct faction * f); void report_travelthru(struct stream *out, struct region * r, const struct faction * f);
void report_region(struct stream *out, const struct region * r, struct faction * f); void report_region(struct stream *out, const struct region * r, struct faction * f);
void report_allies(struct stream *out, size_t maxlen, const struct faction * f, struct allies * allies, const char *prefix);
void pump_paragraph(struct sbstring *sbp, struct stream *out, size_t maxlen, bool isfinal);
void nr_spell_syntax(char *buf, size_t size, struct spellbook_entry * sbe, const struct locale *lang); void nr_spell_syntax(char *buf, size_t size, struct spellbook_entry * sbe, const struct locale *lang);
void nr_spell(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang); void nr_spell(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang);

View file

@ -3,6 +3,7 @@
#include "move.h" #include "move.h"
#include "travelthru.h" #include "travelthru.h"
#include <kernel/ally.h>
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/item.h> #include <kernel/item.h>
@ -143,6 +144,58 @@ static void test_report_region(CuTest *tc) {
test_teardown(); test_teardown();
} }
static void test_report_allies(CuTest *tc) {
stream out = { 0 };
char buf[1024];
char exp[1024];
size_t len, linebreak = 72;
struct locale *lang;
faction *f, *f1, *f2, *f3;
test_setup();
lang = test_create_locale();
locale_setstring(lang, "list_and", " und ");
mstream_init(&out);
f = test_create_faction(NULL);
f->locale = lang;
f1 = test_create_faction(NULL);
f2 = test_create_faction(NULL);
f3 = test_create_faction(NULL);
snprintf(exp, sizeof(exp), "Wir helfen %s (%s).\n",
factionname(f1),
LOC(lang, parameters[P_GUARD]));
ally_set(&f->allies, f1, HELP_GUARD);
report_allies(&out, linebreak, f, f->allies, "Wir helfen ");
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = 0;
CuAssertStrEquals(tc, exp, buf);
out.api->rewind(out.handle);
ally_set(&f->allies, f2, HELP_GIVE);
ally_set(&f->allies, f3, HELP_ALL);
snprintf(exp, sizeof(exp), "Wir helfen %s (%s), %s (%s)",
factionname(f1),
LOC(lang, parameters[P_GUARD]),
factionname(f2),
LOC(lang, parameters[P_GIVE]));
linebreak = strlen(exp);
snprintf(exp, sizeof(exp), "Wir helfen %s (%s), %s (%s)\nund %s (%s).\n",
factionname(f1),
LOC(lang, parameters[P_GUARD]),
factionname(f2),
LOC(lang, parameters[P_GIVE]),
factionname(f3),
LOC(lang, parameters[P_ANY]));
report_allies(&out, linebreak, f, f->allies, "Wir helfen ");
out.api->rewind(out.handle);
len = out.api->read(out.handle, buf, sizeof(buf));
buf[len] = 0;
CuAssertStrEquals(tc, exp, buf);
test_teardown();
}
static void test_report_travelthru(CuTest *tc) { static void test_report_travelthru(CuTest *tc) {
stream out = { 0 }; stream out = { 0 };
char buf[1024]; char buf[1024];
@ -302,6 +355,7 @@ CuSuite *get_report_suite(void)
SUITE_ADD_TEST(suite, test_write_many_spaces); SUITE_ADD_TEST(suite, test_write_many_spaces);
SUITE_ADD_TEST(suite, test_report_travelthru); SUITE_ADD_TEST(suite, test_report_travelthru);
SUITE_ADD_TEST(suite, test_report_region); SUITE_ADD_TEST(suite, test_report_region);
SUITE_ADD_TEST(suite, test_report_allies);
SUITE_ADD_TEST(suite, test_write_spell_syntax); SUITE_ADD_TEST(suite, test_write_spell_syntax);
return suite; return suite;
} }

View file

@ -1687,7 +1687,7 @@ int reports(void)
faction *f; faction *f;
FILE *mailit; FILE *mailit;
int retval = 0; int retval = 0;
char path[4096]; char path[PATH_MAX];
const char * rpath = reportpath(); const char * rpath = reportpath();
log_info("Writing reports for turn %d:", turn); log_info("Writing reports for turn %d:", turn);
@ -2156,7 +2156,7 @@ static void eval_resources(struct opstack **stack, const void *userdata)
const char *rname = const char *rname =
resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0); resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0);
sbs_strcat(&sbs, str_itoa(res->number)); sbs_strcat(&sbs, str_itoa(res->number));
sbs_strcat(&sbs, ""); sbs_strcat(&sbs, " ");
sbs_strcat(&sbs, LOC(lang, rname)); sbs_strcat(&sbs, LOC(lang, rname));
res = res->next; res = res->next;

View file

@ -63,6 +63,7 @@ void tsf_register(const char *name, tostring_f fun)
} }
if (tsf == NULL) { if (tsf == NULL) {
tsf = malloc(sizeof(tsf_list)); tsf = malloc(sizeof(tsf_list));
if (!tsf) abort();
tsf->fun = fun; tsf->fun = fun;
tsf->name = name; tsf->name = name;
tsf->next = tostringfs; tsf->next = tostringfs;
@ -102,12 +103,14 @@ void crt_register(const struct message_type *mtype)
} }
if (!crt) { if (!crt) {
crt = malloc(sizeof(crmessage_type)); crt = malloc(sizeof(crmessage_type));
if (!crt) abort();
crt->mtype = mtype; crt->mtype = mtype;
crt->next = crtypes[hash]; crt->next = crtypes[hash];
crtypes[hash] = crt; crtypes[hash] = crt;
if (mtype->nparameters > 0) { if (mtype->nparameters > 0) {
int i; int i;
crt->renderers = malloc(sizeof(tostring_f) * mtype->nparameters); crt->renderers = malloc(sizeof(tostring_f) * mtype->nparameters);
if (!crt->renderers) abort();
/* can be scrapped for memory vs. speed */ /* can be scrapped for memory vs. speed */
for (i = 0; i != mtype->nparameters; ++i) { for (i = 0; i != mtype->nparameters; ++i) {
crt->renderers[i] = tsf_find(mtype->types[i]->name); crt->renderers[i] = tsf_find(mtype->types[i]->name);

View file

@ -32,8 +32,11 @@ int *intlist_init(void)
int *intlist_add(int *i_p, int i) int *intlist_add(int *i_p, int i)
{ {
void *tmp;
i_p[0]++; i_p[0]++;
i_p = realloc(i_p, (i_p[0] + 1) * sizeof(int)); tmp = realloc(i_p, (i_p[0] + 1) * sizeof(int));
if (!tmp) abort();
i_p = (int *)tmp;
i_p[i_p[0]] = i; i_p[i_p[0]] = i;
return (i_p); return (i_p);

View file

@ -83,7 +83,7 @@ locale *get_or_create_locale(const char *name)
return *lp; return *lp;
} }
} }
*lp = l = (locale *)calloc(sizeof(locale), 1); *lp = l = (locale *)calloc(1, sizeof(locale));
assert_alloc(l); assert_alloc(l);
l->hashkey = hkey; l->hashkey = hkey;
l->name = str_strdup(name); l->name = str_strdup(name);
@ -206,6 +206,7 @@ void locale_setstring(locale * lang, const char *key, const char *value)
} }
if (!find) { if (!find) {
find = calloc(1, sizeof(struct locale_str)); find = calloc(1, sizeof(struct locale_str));
if (!find) abort();
find->nexthash = lang->strings[id]; find->nexthash = lang->strings[id];
lang->strings[id] = find; lang->strings[id] = find;
find->hashkey = hkey; find->hashkey = hkey;

View file

@ -120,6 +120,7 @@ unsigned int listlen(void *l)
void addstrlist(strlist ** SP, const char *s) void addstrlist(strlist ** SP, const char *s)
{ {
strlist *slist = malloc(sizeof(strlist)); strlist *slist = malloc(sizeof(strlist));
if (!slist) abort();
slist->next = NULL; slist->next = NULL;
slist->s = str_strdup(s); slist->s = str_strdup(s);
addlist(SP, slist); addlist(SP, slist);

View file

@ -52,6 +52,7 @@ static log_t *loggers;
log_t *log_create(int flags, void *data, log_fun call) { log_t *log_create(int flags, void *data, log_fun call) {
log_t *lgr = malloc(sizeof(log_t)); log_t *lgr = malloc(sizeof(log_t));
if (!lgr) abort();
lgr->log = call; lgr->log = call;
lgr->flags = flags; lgr->flags = flags;
lgr->data = data; lgr->data = data;
@ -147,7 +148,7 @@ static const char *log_prefix(int level) {
static int check_dupe(const char *format, int level) static int check_dupe(const char *format, int level)
{ {
static int last_type; /* STATIC_XCALL: used across calls */ static int last_type; /* STATIC_XCALL: used across calls */
static char last_message[32]; /* STATIC_XCALL: used across calls */ static char last_message[32] = { 0 }; /* STATIC_XCALL: used across calls */
static int dupes = 0; /* STATIC_XCALL: used across calls */ static int dupes = 0; /* STATIC_XCALL: used across calls */
if (strncmp(last_message, format, sizeof(last_message)) == 0) { if (strncmp(last_message, format, sizeof(last_message)) == 0) {
/* TODO: C6054: String 'last_message' might not be zero - terminated. */ /* TODO: C6054: String 'last_message' might not be zero - terminated. */

View file

@ -41,6 +41,7 @@ register_argtype(const char *name, void(*free_arg) (variant),
variant(*copy_arg) (variant), variant_type type) variant(*copy_arg) (variant), variant_type type)
{ {
arg_type *atype = (arg_type *)malloc(sizeof(arg_type)); arg_type *atype = (arg_type *)malloc(sizeof(arg_type));
if (!atype) abort();
atype->name = name; atype->name = name;
atype->next = argtypes; atype->next = argtypes;
atype->release = free_arg; atype->release = free_arg;
@ -90,7 +91,9 @@ message_type *mt_create(message_type * mtype, const char *args[], int nparameter
int i; int i;
mtype->nparameters = nparameters; mtype->nparameters = nparameters;
mtype->pnames = (char **)malloc(sizeof(char *) * nparameters); mtype->pnames = (char **)malloc(sizeof(char *) * nparameters);
if (!mtype->pnames) abort();
mtype->types = (arg_type **)malloc(sizeof(arg_type *) * nparameters); mtype->types = (arg_type **)malloc(sizeof(arg_type *) * nparameters);
if (!mtype->types) abort();
for (i = 0; args[i]; ++i) { for (i = 0; args[i]; ++i) {
const char *x = args[i]; const char *x = args[i];
const char *spos = strchr(x, ':'); const char *spos = strchr(x, ':');
@ -103,8 +106,8 @@ message_type *mt_create(message_type * mtype, const char *args[], int nparameter
assert(atype); assert(atype);
} }
else { else {
char *cp; char *cp = malloc(spos - x + 1);
cp = malloc(spos - x + 1); if (!cp) abort();
memcpy(cp, x, spos - x); memcpy(cp, x, spos - x);
cp[spos - x] = '\0'; cp[spos - x] = '\0';
mtype->pnames[i] = cp; mtype->pnames[i] = cp;
@ -162,8 +165,10 @@ message_type *mt_new(const char *name, const char *section)
return NULL; return NULL;
} }
mtype = (message_type *)malloc(sizeof(message_type)); mtype = (message_type *)malloc(sizeof(message_type));
if (!mtype) abort();
mtype->key = 0; mtype->key = 0;
mtype->name = str_strdup(name); mtype->name = str_strdup(name);
if (!mtype->name) abort();
mtype->section = section_find(section); mtype->section = section_find(section);
if (!mtype->section) { if (!mtype->section) {
mtype->section = section_add(section); mtype->section = section_add(section);
@ -219,7 +224,6 @@ static void free_arg(const arg_type * atype, variant data)
message *msg_create(const struct message_type *mtype, variant args[]) message *msg_create(const struct message_type *mtype, variant args[])
{ {
int i;
message *msg; message *msg;
assert(mtype != NULL); assert(mtype != NULL);
@ -228,11 +232,17 @@ message *msg_create(const struct message_type *mtype, variant args[])
return NULL; return NULL;
} }
msg = (message *)malloc(sizeof(message)); msg = (message *)malloc(sizeof(message));
if (!msg) abort();
msg->type = mtype; msg->type = mtype;
msg->parameters = (variant *)(mtype->nparameters ? calloc(mtype->nparameters, sizeof(variant)) : NULL);
msg->refcount = 1; msg->refcount = 1;
for (i = 0; i != mtype->nparameters; ++i) { msg->parameters = NULL;
msg->parameters[i] = copy_arg(mtype->types[i], args[i]); if (mtype->nparameters > 0) {
int i;
msg->parameters = (variant *)(mtype->nparameters ? calloc(mtype->nparameters, sizeof(variant)) : NULL);
if (!msg->parameters) abort();
for (i = 0; i != mtype->nparameters; ++i) {
msg->parameters[i] = copy_arg(mtype->types[i], args[i]);
}
} }
if (msg_log_create) if (msg_log_create)
msg_log_create(msg); msg_log_create(msg);

View file

@ -33,7 +33,19 @@ typedef struct nrmessage_type {
} nrmessage_type; } nrmessage_type;
#define NRT_MAXHASH 1021 #define NRT_MAXHASH 1021
static nrmessage_type *nrtypes[NRT_MAXHASH]; static nrmessage_type *nrtypes[NRT_MAXHASH] = { 0 };
void free_nrmesssages(void) {
int i;
for (i = 0; i != NRT_MAXHASH; ++i) {
while (nrtypes[i]) {
nrmessage_type *nr = nrtypes[i];
nrtypes[i] = nr->next;
free(nr->vars);
free(nr);
}
}
}
const char *nrt_string(const struct message_type *mtype, const char *nrt_string(const struct message_type *mtype,
const struct locale *lang) const struct locale *lang)
@ -81,7 +93,8 @@ nrt_register(const struct message_type *mtype)
int i; int i;
char zNames[256]; char zNames[256];
char *c = zNames; char *c = zNames;
nrt = malloc(sizeof(nrmessage_type)); nrt = calloc(1, sizeof(nrmessage_type));
if (!nrt) abort();
nrt->mtype = mtype; nrt->mtype = mtype;
nrt->next = nrtypes[hash]; nrt->next = nrtypes[hash];
nrtypes[hash] = nrt; nrtypes[hash] = nrt;
@ -92,6 +105,7 @@ nrt_register(const struct message_type *mtype)
c += str_strlcpy(c, mtype->pnames[i], sizeof(zNames)-(c-zNames)); c += str_strlcpy(c, mtype->pnames[i], sizeof(zNames)-(c-zNames));
} }
nrt->vars = str_strdup(zNames); nrt->vars = str_strdup(zNames);
if (!nrt->vars) abort();
} }
} }
@ -115,16 +129,3 @@ size_t size, const void *userdata)
buffer[0] = 0; buffer[0] = 0;
return 0; return 0;
} }
void free_nrmesssages(void) {
int i;
for (i = 0; i != NRT_MAXHASH; ++i) {
while (nrtypes[i]) {
nrmessage_type *nr = nrtypes[i];
nrtypes[i] = nr->next;
free(nr->vars);
free(nr);
}
}
}

View file

@ -58,6 +58,7 @@ void init_tokens_ex(const char *initstr, void *data, void (*dtor)(void *))
{ {
if (states == NULL) { if (states == NULL) {
states = calloc(1, sizeof(parser_state)); states = calloc(1, sizeof(parser_state));
if (!states) abort();
} }
else if (states->dtor) { else if (states->dtor) {
states->dtor(states->data); states->dtor(states->data);
@ -74,6 +75,7 @@ void init_tokens_str(const char *initstr) {
void parser_pushstate(void) void parser_pushstate(void)
{ {
parser_state *new_state = calloc(1, sizeof(parser_state)); parser_state *new_state = calloc(1, sizeof(parser_state));
if (!new_state) abort();
new_state->current_token = NULL; new_state->current_token = NULL;
new_state->next = states; new_state->next = states;
states = new_state; states = new_state;

View file

@ -17,7 +17,7 @@ bool password_is_implemented(cryptalgo_t algo) {
} }
const char * password_hash(const char * passwd, cryptalgo_t algo) { const char * password_hash(const char * passwd, cryptalgo_t algo) {
if (algo == PASSWORD_BCRYPT) { if (algo == PASSWORD_BCRYPT && bcrypt_workfactor != 0) {
char salt[BCRYPT_HASHSIZE]; char salt[BCRYPT_HASHSIZE];
static char hash[BCRYPT_HASHSIZE]; static char hash[BCRYPT_HASHSIZE];
int ret; int ret;
@ -32,9 +32,12 @@ const char * password_hash(const char * passwd, cryptalgo_t algo) {
int password_verify(const char * pwhash, const char * passwd) { int password_verify(const char * pwhash, const char * passwd) {
if (pwhash[0] == '$') { if (pwhash[0] == '$') {
if (pwhash[1] == '2') { if (pwhash[1] == '2') {
int ret = bcrypt_checkpw(passwd, pwhash); if (bcrypt_workfactor > 0) {
assert(ret != -1); int ret = bcrypt_checkpw(passwd, pwhash);
return (ret == 0) ? VERIFY_OK : VERIFY_FAIL; assert(ret != -1);
return (ret == 0) ? VERIFY_OK : VERIFY_FAIL;
}
return VERIFY_OK;
} }
} }
return (strcmp(passwd, pwhash) == 0) ? VERIFY_OK : VERIFY_FAIL; return (strcmp(passwd, pwhash) == 0) ? VERIFY_OK : VERIFY_FAIL;

View file

@ -74,7 +74,8 @@ int pofile_read(const char *filename, int (*callback)(const char *msgid, const c
line = read_line(F); line = read_line(F);
while (line) { while (line) {
char token[8]; char token[8];
int err = sscanf(line, "%8s", token); int err = sscanf(line, "%7s", token);
token[7] = 0;
if (err == 1) { if (err == 1) {
char *text = NULL; char *text = NULL;
size_t size = 0, len = strlen(token); size_t size = 0, len = strlen(token);

View file

@ -132,6 +132,7 @@ void random_source_inject_array(double inject[], int size) {
if (values) if (values)
free(values); free(values);
values = malloc(sizeof(double) * size); values = malloc(sizeof(double) * size);
if (!values) abort();
for (i=0; i < size; ++i) { for (i=0; i < size; ++i) {
values[i] = inject[i]; values[i] = inject[i];
} }

View file

@ -80,9 +80,10 @@ size_t str_strlcpy(char *dst, const char *src, size_t len)
/* Not enough room in dst, add NUL and traverse rest of src */ /* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) { if (n == 0) {
if (len != 0) if (len != 0) {
*d = '\0'; /* NUL-terminate dst */ *d = '\0'; /* NUL-terminate dst */
while (*s++); }
return (s - src) + strlen(s); /* count does not include NUL */
} }
return (s - src - 1); /* count does not include NUL */ return (s - src - 1); /* count does not include NUL */
@ -287,8 +288,9 @@ void sbs_strcat(struct sbstring *sbs, const char *str)
size_t len; size_t len;
assert(sbs); assert(sbs);
len = sbs->size - (sbs->end - sbs->begin); len = sbs->size - (sbs->end - sbs->begin);
len = str_strlcpy(sbs->end, str, len); str_strlcpy(sbs->end, str, len);
sbs->end += len; sbs->end += strlen(sbs->end);
assert(sbs->begin + sbs->size >= sbs->end);
} }
void sbs_strcpy(struct sbstring *sbs, const char *str) void sbs_strcpy(struct sbstring *sbs, const char *str)
@ -298,10 +300,32 @@ void sbs_strcpy(struct sbstring *sbs, const char *str)
len = sbs->size - 1; len = sbs->size - 1;
} }
sbs->end = sbs->begin + len; sbs->end = sbs->begin + len;
assert(sbs->begin + sbs->size >= sbs->end);
}
void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len)
{
if (pos > sbs->end - sbs->begin) {
/* starting past end of string, do nothing */
sbs->end = sbs->begin;
}
if (pos >= 0) {
size_t sz = sbs->end - (sbs->begin + pos);
if (len > sz) len = sz;
if (len - pos > 0) {
memmove(sbs->begin, sbs->begin + pos, len);
}
else {
memcpy(sbs->begin, sbs->begin + pos, len);
}
sbs->end = sbs->begin + len;
sbs->end[0] = '\0';
}
} }
size_t sbs_length(const struct sbstring *sbs) size_t sbs_length(const struct sbstring *sbs)
{ {
assert(sbs->begin + sbs->size >= sbs->end);
return sbs->end - sbs->begin; return sbs->end - sbs->begin;
} }

View file

@ -53,6 +53,7 @@ extern "C" {
void sbs_strcat(struct sbstring *sbs, const char *str); void sbs_strcat(struct sbstring *sbs, const char *str);
void sbs_strncat(struct sbstring *sbs, const char *str, size_t size); void sbs_strncat(struct sbstring *sbs, const char *str, size_t size);
void sbs_strcpy(struct sbstring *sbs, const char *str); void sbs_strcpy(struct sbstring *sbs, const char *str);
void sbs_substr(struct sbstring *sbp, ptrdiff_t pos, size_t len);
size_t sbs_length(const struct sbstring *sbs); size_t sbs_length(const struct sbstring *sbs);
/* benchmark for units: /* benchmark for units:

View file

@ -115,16 +115,21 @@ static void test_str_strlcpy(CuTest * tc)
memset(buffer, 0x7f, sizeof(buffer)); memset(buffer, 0x7f, sizeof(buffer));
CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 4)); CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 8));
CuAssertStrEquals(tc, "her", buffer);
CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 8)); /*-V666 */
CuAssertStrEquals(tc, "herp", buffer); CuAssertStrEquals(tc, "herp", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[5]); CuAssertIntEquals(tc, 0x7f, buffer[5]);
CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 4));
CuAssertStrEquals(tc, "her", buffer);
CuAssertIntEquals(tc, 8, (int)str_strlcpy(buffer, "herpderp", 8)); CuAssertIntEquals(tc, 8, (int)str_strlcpy(buffer, "herpderp", 8));
CuAssertStrEquals(tc, "herpder", buffer); CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[8]); 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; errno = 0;
} }
@ -154,6 +159,69 @@ static void test_sbstring(CuTest * tc)
CuAssertStrEquals(tc, "123456789012345", sbs.begin); 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);
}
static void test_sbs_substr(CuTest * tc)
{
char buffer[10];
sbstring sbs;
buffer[9] = 'A';
sbs_init(&sbs, buffer, sizeof(buffer));
sbs_strcpy(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 0, 4);
CuAssertStrEquals(tc, "1234", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 4, 4);
CuAssertStrEquals(tc, "5678", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 2, 4);
CuAssertStrEquals(tc, "3456", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 4, 4);
CuAssertStrEquals(tc, "5678", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 4, 8);
CuAssertStrEquals(tc, "5678", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
CuAssertIntEquals(tc, 'A', buffer[9]);
}
CuSuite *get_strings_suite(void) CuSuite *get_strings_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -166,5 +234,7 @@ CuSuite *get_strings_suite(void)
SUITE_ADD_TEST(suite, test_str_strlcat); SUITE_ADD_TEST(suite, test_str_strlcat);
SUITE_ADD_TEST(suite, test_str_strlcpy); SUITE_ADD_TEST(suite, test_str_strlcpy);
SUITE_ADD_TEST(suite, test_sbstring); SUITE_ADD_TEST(suite, test_sbstring);
SUITE_ADD_TEST(suite, test_sbs_strcat);
SUITE_ADD_TEST(suite, test_sbs_substr);
return suite; return suite;
} }

View file

@ -118,6 +118,7 @@ char * transliterate(char * out, size_t size, const char * in)
tnode * mknode(void) { tnode * mknode(void) {
tnode * node = (tnode *)calloc(1, sizeof(tnode)); tnode * node = (tnode *)calloc(1, sizeof(tnode));
if (!node) abort();
node->refcount = 1; node->refcount = 1;
return node; return node;
} }
@ -179,6 +180,7 @@ void addtoken(tnode ** root, const char *str, variant id)
} }
ref = (tref *)malloc(sizeof(tref)); ref = (tref *)malloc(sizeof(tref));
if (!ref) abort();
ref->ucs = ucs; ref->ucs = ucs;
ref->node = node; ref->node = node;
ref->nexthash = tk->next[index]; ref->nexthash = tk->next[index];
@ -221,11 +223,10 @@ void addtoken(tnode ** root, const char *str, variant id)
} }
} }
void freetokens(tnode * root) void freetokens(tnode * node)
{ {
tnode * node = root;
int i; int i;
for (i = 0; node && i != NODEHASHSIZE; ++i) { for (i = 0; i != NODEHASHSIZE; ++i) {
if (node->next[i]) { if (node->next[i]) {
tref * ref = node->next[i]; tref * ref = node->next[i];
while (ref) { while (ref) {
@ -237,6 +238,7 @@ void freetokens(tnode * root)
node->next[i] = 0; node->next[i] = 0;
} }
} }
/* TODO: warning C6011: Dereferencing NULL pointer 'node'. */
if (--node->refcount == 0) { if (--node->refcount == 0) {
free(node); free(node);
} }