]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Prevent usage of uninitialized variable in `__archive_mkstemp` (#2121)
authorTobias Stoeckmann <stoeckmann@users.noreply.github.com>
Sat, 13 Apr 2024 05:14:59 +0000 (05:14 +0000)
committerGitHub <noreply@github.com>
Sat, 13 Apr 2024 05:14:59 +0000 (22:14 -0700)
Calling `__archive_mkstemp` can lead to access of an uninitialized
variable in `__archive_mktempx`, because `temp_name` is only initialized
if supplied `template` argument is `NULL`.

If `template` is not `NULL`, it is eventually compared with
`temp_name.s` anyway.

The fix is simple: Always initialize `temp_name`, which merely sets
values in the struct. No memory allocation occurs and the check leads to
the expected result.

How to reproduce:

1. Compile libarchive with Visual Studio 2022 and CMake's Debug profile
2. Run test `bsdtar_test_option_safe_writes`
3. A popup (Microsoft Visual C++ Runtime Library) appears, stating that
variable temp_name is being used without being initialized

libarchive/archive_util.c

index 32d4bd40988c3b58f1b62a4d5f75179243b1a34f..7b918fef04b81935097d07a6e12917637f816e0a 100644 (file)
@@ -255,10 +255,9 @@ __archive_mktempx(const char *tmpdir, wchar_t *template)
 #endif
        fd = -1;
        ws = NULL;
+       archive_string_init(&temp_name);
 
        if (template == NULL) {
-               archive_string_init(&temp_name);
-
                /* Get a temporary directory. */
                if (tmpdir == NULL) {
                        size_t l;