1 /*-
2 * Copyright (c) 2026 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "test.h"
27
28 /*
29 * An empty pathname must be rejected (ARCHIVE_FAILED) by the ZIP writer.
30 *
31 * write_path() used bitwise & instead of logical && when checking whether
32 * to append a trailing slash for directory entries. Because & does not
33 * short-circuit, path[strlen(path)-1] was evaluated even when the entry
34 * was not a directory — and when the path is empty, strlen(path)-1 wraps
35 * to SIZE_MAX, producing a one-byte OOB read before the heap buffer.
36 */
DEFINE_TEST(test_write_format_zip_empty_pathname)37 DEFINE_TEST(test_write_format_zip_empty_pathname)
38 {
39 struct archive *a;
40 struct archive_entry *ae;
41 char buf[4096];
42 size_t used;
43
44 /* Regular file with empty pathname */
45 assert((a = archive_write_new()) != NULL);
46 assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a));
47 assertEqualInt(ARCHIVE_OK,
48 archive_write_open_memory(a, buf, sizeof(buf), &used));
49
50 assert((ae = archive_entry_new()) != NULL);
51 archive_entry_copy_pathname(ae, "");
52 archive_entry_set_mode(ae, AE_IFREG | 0644);
53 archive_entry_set_size(ae, 0);
54 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
55 archive_entry_free(ae);
56
57 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
58 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
59
60 /* Directory entry with empty pathname */
61 assert((a = archive_write_new()) != NULL);
62 assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a));
63 assertEqualInt(ARCHIVE_OK,
64 archive_write_open_memory(a, buf, sizeof(buf), &used));
65
66 assert((ae = archive_entry_new()) != NULL);
67 archive_entry_copy_pathname(ae, "");
68 archive_entry_set_mode(ae, AE_IFDIR | 0755);
69 archive_entry_set_size(ae, 0);
70 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae));
71 archive_entry_free(ae);
72
73 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
74 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
75 }
76