* src/retr.c(rotate_backups): Simplify logic for handling filename rotation

This commit is contained in:
Darshit Shah 2021-01-03 15:59:49 +01:00
parent 1e89e5f66c
commit 9f3df123bb

View File

@ -1415,9 +1415,10 @@ rotate_backups(const char *fname)
# define SEP "." # define SEP "."
# define AVSL 0 # define AVSL 0
#endif #endif
#define FILE_BUF_SIZE 1024
/* avoid alloca() here */ /* avoid alloca() here */
char from[1024], to[1024]; char from[FILE_BUF_SIZE], to[FILE_BUF_SIZE];
struct stat sb; struct stat sb;
bool overflow; bool overflow;
int i; int i;
@ -1442,20 +1443,24 @@ rotate_backups(const char *fname)
delete (to); delete (to);
} }
#endif #endif
if ((overflow = ((unsigned) snprintf (to, sizeof (to), "%s%s%d", fname, SEP, i)) >= sizeof (to))) overflow = (unsigned) snprintf (to, FILE_BUF_SIZE, "%s%s%d", fname, SEP, i) >= FILE_BUF_SIZE;
errno = ENAMETOOLONG; overflow |= (unsigned) snprintf (from, FILE_BUF_SIZE, "%s%s%d", fname, SEP, i - 1) >= FILE_BUF_SIZE;
else if ((overflow = ((unsigned) snprintf (from, sizeof (from), "%s%s%d", fname, SEP, i - 1)) >= sizeof (from)))
errno = ENAMETOOLONG; if (overflow)
errno = ENAMETOOLONG;
if (overflow || rename (from, to)) if (overflow || rename (from, to))
logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n", logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n",
from, to, errno, strerror (errno)); 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; errno = ENAMETOOLONG;
if (overflow || rename(fname, to)) if (overflow || rename(fname, to))
logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n", logprintf (LOG_NOTQUIET, "Failed to rename %s to %s: (%d) %s\n",
fname, to, errno, strerror (errno)); fname, to, errno, strerror (errno));
#undef FILE_BUF_SIZE
} }
static bool no_proxy_match (const char *, const char **); static bool no_proxy_match (const char *, const char **);