Project

General

Profile

Enhancement #750 » 0019-sanitize-ftell-fseek-and-logfile-len-logstore-file_o.patch

Loïc Gomez, 2022-01-10 19:28

View differences:

src/log.c
static void log_reset(logstore_t *store)
{
logfile_t *olf;
long ftell_r;
store->skip_advance = 0;
......
list_remove_first(&store->file_group);
}
assert(olf);
assert(olf->file);
if (!olf || !olf->file)
return;
list_it_init_last(&store->file_group, &store->file_it);
fseek(olf->file, 0, SEEK_END);
olf->len = ftell(olf->file);
store->file_offset = olf->len;
fseek(olf->file, (long)0, SEEK_END);
ftell_r = ftell(olf->file);
if (ftell_r < 0) {
mylog(LOG_ERROR, "log_reset: ftell error %s", strerror(errno));
return;
}
store->file_offset = ftell_r;
olf->len = (size_t)ftell_r;
}
void log_reinit(logstore_t *store)
......
char *uniq_fname;
char *canonical_fname = NULL;
logfile_t *lf = NULL;
long ftell_r;
if (logdata->log_to_file) {
if (log_has_file(logdata, filename)) {
......
lf = bip_malloc(sizeof(logfile_t));
lf->file = f;
lf->len = ftell(f);
ftell_r = ftell(f);
lf->len = (size_t)ftell_r;
if (ftell_r < 0) {
mylog(LOG_ERROR, "log_add_file: ftell error %s",
strerror(errno));
free(uniq_fname);
free(canonical_fname);
fclose(f);
return 0;
}
lf->filename = uniq_fname;
lf->canonical_filename = canonical_fname;
log_updatelast(lf);
......
list_init(&store->file_group, NULL);
store->name = bip_strdup(destination);
store->skip_advance = 0;
// should be safe to cast as lf->len comes from ftell()
if (lf)
store->file_offset = lf->len;
store->file_offset = (long)lf->len;
hash_insert(&logdata->logfgs, destination, store);
}
......
if (lf != list_get_last(&store->file_group))
return 1;
return store->file_offset != lf->len;
// should be safe to cast to unsigned as we check ftell
// when setting file_offset and only ++ since then
return (size_t)store->file_offset != lf->len;
}
/*
src/log.h
int memc;
int track_backlog;
list_iterator_t file_it;
size_t file_offset;
long file_offset;
} logstore_t;
typedef struct log
(18-18/24)