From 324b77834b094bbda8abb3352465776a54b23549 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 30 Jul 2017 17:05:06 -0700 Subject: [PATCH] backupfile: new function to validate backup suffix * lib/backupfile.c (set_simple_backup_suffix): New function. Also, reject suffixes containing "/". (backupfile_internal): Use it. --- ChangeLog | 4 ++++ lib/backupfile.c | 19 +++++++++++-------- lib/backupfile.h | 1 + 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 72d5a77232..7aedb71470 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2017-07-30 Paul Eggert + backupfile: new function to validate backup suffix + * lib/backupfile.c (set_simple_backup_suffix): New function. + (backupfile_internal): Use it. + canonicalize: fix EOVERFLOW commentary Problem reported by Bruno Haible in: http://lists.gnu.org/archive/html/bug-gnulib/2017-07/msg00147.html diff --git a/lib/backupfile.c b/lib/backupfile.c index 9773fe2c41..d7115bd61c 100644 --- a/lib/backupfile.c +++ b/lib/backupfile.c @@ -83,6 +83,16 @@ to numbered) backup file name. */ char const *simple_backup_suffix = NULL; +/* Set SIMPLE_BACKUP_SUFFIX to S, or to a default specified by the + environment if S is null. If S or the environment does not specify + a valid backup suffix, use "~". */ +void +set_simple_backup_suffix (char const *s) +{ + if (!s) + s = getenv ("SIMPLE_BACKUP_SUFFIX"); + simple_backup_suffix = s && *s && s == last_component (s) ? s : "~"; +} /* If FILE (which was of length FILELEN before an extension was appended to it) is too long, replace the extension with the single @@ -289,15 +299,8 @@ backupfile_internal (char const *file, enum backup_type backup_type, bool rename ptrdiff_t base_offset = last_component (file) - file; size_t filelen = base_offset + strlen (file + base_offset); - /* Initialize the default simple backup suffix. */ if (! simple_backup_suffix) - { - char const *env_suffix = getenv ("SIMPLE_BACKUP_SUFFIX"); - if (env_suffix && *env_suffix) - simple_backup_suffix = env_suffix; - else - simple_backup_suffix = "~"; - } + set_simple_backup_suffix (NULL); /* Allow room for simple or ".~N~" backups. The guess must be at least sizeof ".~1~", but otherwise will be adjusted as needed. */ diff --git a/lib/backupfile.h b/lib/backupfile.h index d578a6500f..fe4d89d2bf 100644 --- a/lib/backupfile.h +++ b/lib/backupfile.h @@ -46,6 +46,7 @@ enum backup_type extern char const *simple_backup_suffix; +void set_simple_backup_suffix (char const *); char *backup_file_rename (char const *, enum backup_type); char *find_backup_file_name (char const *, enum backup_type); enum backup_type get_version (char const *context, char const *arg); -- 2.39.5