From 79d9be4e71b171cf047bed0f59377dd9ce930d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gomez?= Date: Sun, 9 Jan 2022 20:43:26 +0100 Subject: [PATCH] sanitize: ftell/fseek & len/file_offset type fixes --- src/log.c | 35 +++++++++++++++++++++++++++-------- src/log.h | 5 +++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/log.c b/src/log.c index 0af0a11..52d4b37 100644 --- a/src/log.c +++ b/src/log.c @@ -191,6 +191,7 @@ void log_updatelast(logfile_t *lf) static void log_reset(logstore_t *store) { logfile_t *olf; + long ftell_r; store->skip_advance = 0; @@ -206,14 +207,19 @@ static void log_reset(logstore_t *store) 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) @@ -267,6 +273,7 @@ static int log_add_file(log_t *logdata, const char *destination, 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)) { @@ -296,7 +303,16 @@ static int log_add_file(log_t *logdata, const char *destination, 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); @@ -308,8 +324,9 @@ static int log_add_file(log_t *logdata, const char *destination, 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); } @@ -828,7 +845,9 @@ int log_has_backlog(log_t *logdata, const char *destination) 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; } /* diff --git a/src/log.h b/src/log.h index dbbc4a6..b77ad46 100644 --- a/src/log.h +++ b/src/log.h @@ -2,7 +2,8 @@ * $Id: log.h,v 1.26 2005/04/12 19:34:35 nohar Exp $ * * This file is part of the bip project - * Copyright (C) 2004 Arnaud Cornet and Loïc Gomez + * Copyright (C) 2004 Arnaud Cornet + * Copyright (C) 2004,2022 Loïc Gomez * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,7 +48,7 @@ typedef struct logstore int memc; int track_backlog; list_iterator_t file_it; - size_t file_offset; + long file_offset; } logstore_t; typedef struct log