Merge pull request #369 from ennorehling/coverity-leaks

make it easier for coverity to understand this code
This commit is contained in:
Enno Rehling 2015-11-06 10:37:48 +01:00
commit d29429f309
7 changed files with 41 additions and 43 deletions

@ -1 +1 @@
Subproject commit 77130e660e2227ae740e06f38e85cd18ff728599 Subproject commit dfe57a077222c6b572da61a79dc0687f81c10055

View file

@ -1743,10 +1743,8 @@ int writegame(const char *filename)
sprintf(path, "%s/%s", datapath(), filename); sprintf(path, "%s/%s", datapath(), filename);
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
if (access(path, R_OK) == 0) { /* make sure we don't overwrite an existing file (hard links) */
/* make sure we don't overwrite some hardlinkedfile */ unlink(path);
unlink(path);
}
#endif #endif
F = fopen(path, "wb"); F = fopen(path, "wb");
if (!F) { if (!F) {

View file

@ -38,6 +38,7 @@ static void test_readwrite_unit(CuTest * tc)
f = test_create_faction(0); f = test_create_faction(0);
fno = f->no; fno = f->no;
u = test_create_unit(f, r); u = test_create_unit(f, r);
_mkdir(datapath());
sprintf(path, "%s/%s", datapath(), filename); sprintf(path, "%s/%s", datapath(), filename);
data = gamedata_open(path, "wb"); data = gamedata_open(path, "wb");

View file

@ -73,7 +73,7 @@ spell * create_spell(const char * name, unsigned int id)
} }
sp = (spell *)calloc(1, sizeof(spell)); sp = (spell *)calloc(1, sizeof(spell));
len = cb_new_kv(name, len, &sp, sizeof(sp), buffer); len = cb_new_kv(name, len, &sp, sizeof(sp), buffer);
if (cb_insert(&cb_spells, buffer, len)) { if (cb_insert(&cb_spells, buffer, len) == CB_SUCCESS) {
sp->id = id ? id : hashstring(name); sp->id = id ? id : hashstring(name);
sp->sname = _strdup(name); sp->sname = _strdup(name);
add_spell(&spells, sp); add_spell(&spells, sp);

View file

@ -2981,7 +2981,10 @@ spellbook * get_spellbook(const char * name)
result = create_spellbook(name); result = create_spellbook(name);
assert(strlen(name) + sizeof(result) < sizeof(buffer)); assert(strlen(name) + sizeof(result) < sizeof(buffer));
len = cb_new_kv(name, len, &result, sizeof(result), buffer); len = cb_new_kv(name, len, &result, sizeof(result), buffer);
cb_insert(&cb_spellbooks, buffer, len); if (cb_insert(&cb_spellbooks, buffer, len) == CB_EXISTS) {
log_error("cb_insert failed although cb_find returned nothing for spellbook=%s", name);
assert(!"should not happen");
}
} }
return result; return result;
} }

View file

@ -1540,6 +1540,15 @@ static void prepare_report(struct report_context *ctx, faction *f)
ctx->last = lastregion(f); ctx->last = lastregion(f);
} }
static void mkreportdir(const char *rpath) {
if (_access(rpath, 0) < 0) {
if (_mkdir(rpath) != 0) {
log_error("could not create reports directory %s: %s", rpath, strerror(errno));
abort();
}
}
}
int write_reports(faction * f, time_t ltime) int write_reports(faction * f, time_t ltime)
{ {
unsigned int backup = 1, maxbackup = 128 * 1000; unsigned int backup = 1, maxbackup = 128 * 1000;
@ -1547,19 +1556,14 @@ int write_reports(faction * f, time_t ltime)
struct report_context ctx; struct report_context ctx;
const char *encoding = "UTF-8"; const char *encoding = "UTF-8";
report_type *rtype; report_type *rtype;
const char *path = reportpath();; const char *path = reportpath();
if (noreports) { if (noreports) {
return false; return false;
} }
prepare_report(&ctx, f); prepare_report(&ctx, f);
get_addresses(&ctx); get_addresses(&ctx);
if (_access(path, 0) < 0) { mkreportdir(path);
if (_mkdir(path) != 0) {
log_error("could not create reports directory %s: %s", path, strerror(errno));
abort();
}
}
if (errno) { if (errno) {
log_warning("errno was %d before writing reports", errno); log_warning("errno was %d before writing reports", errno);
errno = 0; errno = 0;
@ -1641,12 +1645,7 @@ int init_reports(void)
return 0; return 0;
} }
} }
if (_mkdir(reportpath()) != 0) { mkreportdir(reportpath());
if (errno != EEXIST) {
perror("could not create reportpath");
return -1;
}
}
return 0; return 0;
} }
@ -1663,13 +1662,8 @@ int reports(void)
report_donations(); report_donations();
remove_empty_units(); remove_empty_units();
if (_access(rpath, 0) < 0) { mkreportdir(rpath);
if (_mkdir(rpath) != 0) { sprintf(path, "%s/reports.txt", rpath);
log_error("could not create reports directory %s: %s", rpath, strerror(errno));
abort();
}
}
sprintf(path, "%s/reports.txt", reportpath());
mailit = fopen(path, "w"); mailit = fopen(path, "w");
if (mailit == NULL) { if (mailit == NULL) {
log_error("%s could not be opened!\n", path); log_error("%s could not be opened!\n", path);

View file

@ -77,24 +77,26 @@ cp_convert(const char *format, char *buffer, size_t length, int codepage)
void log_rotate(const char *filename, int maxindex) void log_rotate(const char *filename, int maxindex)
{ {
if (_access(filename, 4) == 0) { char buffer[2][MAX_PATH];
char buffer[2][MAX_PATH]; int dst = 1;
int dst = 1; assert(strlen(filename) < sizeof(buffer[0]) - 4);
assert(strlen(filename) < sizeof(buffer[0]) - 4);
sprintf(buffer[dst], "%s.%d", filename, maxindex); sprintf(buffer[dst], "%s.%d", filename, maxindex);
while (maxindex > 0) { #ifdef HAVE_UNISTD_H
int err, src = 1 - dst; /* make sure we don't overwrite an existing file (hard links) */
sprintf(buffer[src], "%s.%d", filename, --maxindex); unlink(buffer[dst]);
err = rename(buffer[src], buffer[dst]); #endif
if (err != 0) { while (maxindex > 0) {
log_error("log rotate %s: %s", buffer[dst], strerror(errno)); int err, src = 1 - dst;
} sprintf(buffer[src], "%s.%d", filename, --maxindex);
dst = src; err = rename(buffer[src], buffer[dst]);
} if (err != 0) {
if (rename(filename, buffer[dst]) != 0) { log_debug("log rotate %s: %s", buffer[dst], strerror(errno));
log_error("log rotate %s: %s", buffer[dst], strerror(errno));
} }
dst = src;
}
if (rename(filename, buffer[dst]) != 0) {
log_debug("log rotate %s: %s", buffer[dst], strerror(errno));
} }
} }