snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

commit 4b355fb2c96af2565c48bd47b87f51ab72192085
parent b9e43acf21c0c7d7e2521e168316494b5afcf6c6
Author: Santtu Lakkala <santtu.lakkala@unikie.com>
Date:   Thu,  6 Feb 2025 13:34:22 +0200

Fix warnings

Diffstat:
MMakefile | 2+-
Mdata.c | 55+++++++++++++++++++++++++++++--------------------------
Mhttpd.c | 14++++++++------
Mxs_fcgi.h | 7+++++--
Mxs_time.h | 17+++++++++++++++++
5 files changed, 60 insertions(+), 35 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ .POSIX: PREFIX?=/usr/local PREFIX_MAN=$(PREFIX)/man -CFLAGS=-std=c99 -Os -Wall -Wextra -pedantic -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 +CFLAGS=-std=c99 -Os -Wall -Wextra -pedantic -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 -DWITHOUT_TIMEGM all: snac diff --git a/data.c b/data.c @@ -560,8 +560,9 @@ int index_gc(const char *fn) xs *ofn = xs_fmt("%s.bak", fn); unlink(ofn); - link(fn, ofn); - rename(nfn, fn); + if (link(fn, ofn) == -1 || rename(nfn, fn) == -1) { + fprintf(stderr, "Updating link during garbage collection failed: %s.\n", strerror(errno)); + } } fclose(i); @@ -1670,7 +1671,9 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg) /* increase its reference count */ fn = xs_replace_i(fn, ".json", "_a.json"); - link(actor_fn, fn); + if (link(actor_fn, fn) == -1) { + fprintf(stderr, "Failed to create actor link: %s\n", strerror(errno)); + } } else ret = HTTP_STATUS_INTERNAL_SERVER_ERROR; @@ -1768,7 +1771,9 @@ xs_list *following_list(snac *snac) if (mtime(v2) == 0.0) { /* no; add a link to it */ xs *actor_fn = _object_fn(actor); - link(actor_fn, v2); + if (link(actor_fn, v2) == -1) { + fprintf(stderr, "Actor link creation failed: %s\n", strerror(errno)); + } } } } @@ -3268,7 +3273,9 @@ void notify_clear(snac *snac) if (mtime(idx) != 0.0) { pthread_mutex_lock(&data_mutex); - truncate(idx, 0); + if (truncate(idx, 0)) { + fprintf(stderr, "Notification index truncation failed\n"); + } pthread_mutex_unlock(&data_mutex); } } @@ -4084,34 +4091,30 @@ t_announcement *announcement(const double after) FILE *f; if ((f = fopen(fn, "r")) != NULL) { - fseek (f, 0, SEEK_END); - const long int length = ftell(f); + char buffer[MAX_SIZE]; + int r = fread(buffer, 1, MAX_SIZE, f); - if (length > MAX_SIZE) { + if (r == 0) { + /* an empty file means no announcement */ + free(a.text); + a.text = NULL; + a.timestamp = 0.0; + } + else + if (r <= 0 || !xs_is_string(buffer)) { /* this is probably unintentional */ - srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE)); + srv_log(xs_fmt("announcement.txt reading failed")); } else - if (length > 0) { - fseek (f, 0, SEEK_SET); - char *buffer = malloc(length + 1); - if (buffer) { - fread(buffer, 1, length, f); - buffer[length] = '\0'; - - free(a.text); - a.text = buffer; - a.timestamp = ts; - } - else { - srv_log("Error allocating memory for announcement"); - } + if (r > MAX_SIZE) { + /* this is probably unintentional */ + srv_log(xs_fmt("announcement.txt too big: max is %ld, ignoring.", MAX_SIZE)); } else { - /* an empty file means no announcement */ + buffer[r] = '\0'; free(a.text); - a.text = NULL; - a.timestamp = 0.0; + a.text = xs_dup(buffer); + a.timestamp = ts; } fclose (f); diff --git a/httpd.c b/httpd.c @@ -826,10 +826,9 @@ srv_state *srv_state_op(xs_str **fname, int op) #else if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) { - ftruncate(fd, sizeof(*ss)); - - if ((ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0)) == MAP_FAILED) + if (ftruncate(fd, sizeof(*ss)) == -1 || + (ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0)) == MAP_FAILED) ss = NULL; close(fd); @@ -926,10 +925,13 @@ void httpd(void) return; } - ftruncate(pidfd, 0); + (void)(ftruncate(pidfd, 0) + 1); xs *s = xs_fmt("%d\n", (int)getpid()); - write(pidfd, s, strlen(s)); + if (write(pidfd, s, strlen(s)) != (ssize_t)strlen(s)) { + srv_log(xs_fmt("Wrting to pidfile %s failed -- cannot continue", pidfile)); + return; + } } address = xs_dict_get(srv_config, "address"); diff --git a/xs_fcgi.h b/xs_fcgi.h @@ -105,8 +105,11 @@ xs_dict *xs_fcgi_request(FILE *f, xs_str **payload, int *p_size, int *fcgi_id) } /* read (and drop) the padding */ - if (hdr.padding_len > 0) - fread(p_buf + psz, 1, hdr.padding_len, f); + if (hdr.padding_len > 0) { + if (fread(p_buf + psz, 1, hdr.padding_len, f) != hdr.padding_len) { + break; + } + } switch (hdr.type) { case FCGI_BEGIN_REQUEST: diff --git a/xs_time.h b/xs_time.h @@ -51,6 +51,23 @@ xs_str *xs_str_time_diff(time_t time_diff) return xs_fmt("%d:%02d:%02d:%02d", days, hours, mins, secs); } +#ifndef WITHOUT_STRPTIME +#ifdef WITHOUT_TIMEGM +time_t timegm_shim(struct tm *tm) +{ + time_t years_since_70 = tm->tm_year - 70; + time_t yeardays = years_since_70 * 365; + time_t leaps = (years_since_70 + 1) / 4 - (years_since_70 + 69) / 100 + (years_since_70 + 369) / 400; + time_t hours = (yeardays + leaps + tm->tm_yday) * 24 + tm->tm_hour; + time_t minutes = hours * 60 + tm->tm_min; + + return minutes * 60 + tm->tm_sec; +} + +#define timegm timegm_shim +#endif +#endif + time_t xs_parse_time(const char *str, const char *fmt, int local) { time_t t = 0;