From 6d1fcd1c4848d774b70570fa7c830d45388734de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20R=C3=BChsen?= Date: Fri, 14 Feb 2020 13:15:21 +0100 Subject: [PATCH] * src/convert.c (write_backup_file): Remove use of alloca --- src/convert.c | 59 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/convert.c b/src/convert.c index e2989d26..150fb065 100644 --- a/src/convert.c +++ b/src/convert.c @@ -538,32 +538,6 @@ write_backup_file (const char *file, downloaded_file_t downloaded_file_return) clobber .orig files sitting around from previous invocations. On VMS, use "_orig" instead of ".orig". See "wget.h". */ - /* Construct the backup filename as the original name plus ".orig". */ - size_t filename_len = strlen (file); - char* filename_plus_orig_suffix; - - /* TODO: hack this to work with css files */ - if (downloaded_file_return == FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED) - { - /* Just write "orig" over "html". We need to do it this way - because when we're checking to see if we've downloaded the - file before (to see if we can skip downloading it), we don't - know if it's a text/html file. Therefore we don't know yet - at that stage that -E is going to cause us to tack on - ".html", so we need to compare vs. the original URL plus - ".orig", not the original URL plus ".html.orig". */ - filename_plus_orig_suffix = alloca (filename_len + 1); - strcpy (filename_plus_orig_suffix, file); - strcpy ((filename_plus_orig_suffix + filename_len) - 4, "orig"); - } - else /* downloaded_file_return == FILE_DOWNLOADED_NORMALLY */ - { - /* Append ".orig" to the name. */ - filename_plus_orig_suffix = alloca (filename_len + sizeof (ORIG_SFX)); - strcpy (filename_plus_orig_suffix, file); - strcpy (filename_plus_orig_suffix + filename_len, ORIG_SFX); - } - if (!converted_files) converted_files = make_string_hash_table (0); @@ -574,11 +548,44 @@ write_backup_file (const char *file, downloaded_file_t downloaded_file_return) called on this file. */ if (!string_set_contains (converted_files, file)) { + /* Construct the backup filename as the original name plus ".orig". */ + char buf[1024]; + size_t filename_len = strlen (file); + char *filename_plus_orig_suffix; + + if (filename_len < sizeof (buf) - 5) + filename_plus_orig_suffix = buf; + else + filename_plus_orig_suffix = xmalloc (filename_len + 5 + 1); + + /* TODO: hack this to work with css files */ + if (downloaded_file_return == FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED) + { + /* Just write "orig" over "html". We need to do it this way + because when we're checking to see if we've downloaded the + file before (to see if we can skip downloading it), we don't + know if it's a text/html file. Therefore we don't know yet + at that stage that -E is going to cause us to tack on + ".html", so we need to compare vs. the original URL plus + ".orig", not the original URL plus ".html.orig". */ + memcpy (filename_plus_orig_suffix, file, filename_len - 4); + memcpy (filename_plus_orig_suffix + filename_len - 4, "orig", 5); + } + else /* downloaded_file_return == FILE_DOWNLOADED_NORMALLY */ + { + /* Append ".orig" to the name. */ + memcpy (filename_plus_orig_suffix, file, filename_len); + strcpy (filename_plus_orig_suffix + filename_len, ORIG_SFX); + } + /* Rename to .orig before former gets written over. */ if (rename (file, filename_plus_orig_suffix) != 0) logprintf (LOG_NOTQUIET, _("Cannot back up %s as %s: %s\n"), file, filename_plus_orig_suffix, strerror (errno)); + if (filename_plus_orig_suffix != buf) + xfree (filename_plus_orig_suffix); + /* Remember that we've already written a .orig backup for this file. Note that we never free this memory since we need it till the convert_all_links() call, which is one of the last things the