From 9f3df123bbe1cbf5873cea25169da4568122f026 Mon Sep 17 00:00:00 2001 From: Darshit Shah Date: Sun, 3 Jan 2021 15:59:49 +0100 Subject: [PATCH] * src/retr.c(rotate_backups): Simplify logic for handling filename rotation --- src/retr.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/retr.c b/src/retr.c index c30597e6..73f8c717 100644 --- a/src/retr.c +++ b/src/retr.c @@ -1415,9 +1415,10 @@ rotate_backups(const char *fname) # define SEP "." # define AVSL 0 #endif +#define FILE_BUF_SIZE 1024 /* avoid alloca() here */ - char from[1024], to[1024]; + char from[FILE_BUF_SIZE], to[FILE_BUF_SIZE]; struct stat sb; bool overflow; int i; @@ -1442,20 +1443,24 @@ rotate_backups(const char *fname) delete (to); } #endif - if ((overflow = ((unsigned) snprintf (to, sizeof (to), "%s%s%d", fname, SEP, i)) >= sizeof (to))) - errno = ENAMETOOLONG; - else if ((overflow = ((unsigned) snprintf (from, sizeof (from), "%s%s%d", fname, SEP, i - 1)) >= sizeof (from))) - errno = ENAMETOOLONG; + overflow = (unsigned) snprintf (to, FILE_BUF_SIZE, "%s%s%d", fname, SEP, i) >= FILE_BUF_SIZE; + overflow |= (unsigned) snprintf (from, FILE_BUF_SIZE, "%s%s%d", fname, SEP, i - 1) >= FILE_BUF_SIZE; + + if (overflow) + errno = ENAMETOOLONG; if (overflow || rename (from, to)) logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n", from, to, errno, strerror (errno)); } - if ((overflow = ((unsigned) snprintf (to, sizeof (to), "%s%s%d", fname, SEP, 1)) >= sizeof (to))) + overflow = (unsigned) snprintf (to, FILE_BUF_SIZE, "%s%s%d", fname, SEP, 1) >= FILE_BUF_SIZE; + if (overflow) errno = ENAMETOOLONG; if (overflow || rename(fname, to)) logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n", fname, to, errno, strerror (errno)); + +#undef FILE_BUF_SIZE } static bool no_proxy_match (const char *, const char **);