]> wimlib.net Git - wimlib/blobdiff - src/test_support.c
Prevent huge memory allocations from fuzzed header fields
[wimlib] / src / test_support.c
index 38cdc1ea1ba92ae03040aef0e7c6740b6604c8fc..91cb468ce980fe4f3fb715b1a5423a9255a0f6b2 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2015-2018 Eric Biggers
+ * Copyright 2015-2023 Eric Biggers
  *
  * This file is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
@@ -126,7 +126,7 @@ is_valid_windows_filename_char(utf16lechar c)
 static inline bool
 is_valid_filename_char(utf16lechar c)
 {
-#ifdef __WIN32__
+#ifdef _WIN32
        return is_valid_windows_filename_char(c);
 #else
        return c != cpu_to_le16('\0') && c != cpu_to_le16('/');
@@ -168,7 +168,7 @@ retry:
        /* Generate the characters in the name. */
        for (int i = 0; i < len; i++) {
                do {
-                       name[i] = rand16();
+                       name[i] = cpu_to_le16(rand16());
                } while (!is_valid_filename_char(name[i]));
        }
 
@@ -385,7 +385,7 @@ generate_random_security_descriptor(void *_desc, struct generation_context *ctx)
 static bool
 am_root(void)
 {
-#ifdef __WIN32__
+#ifdef _WIN32
        return false;
 #else
        return (getuid() == 0);
@@ -395,7 +395,7 @@ am_root(void)
 static u32
 generate_uid(void)
 {
-#ifdef __WIN32__
+#ifdef _WIN32
        return 0;
 #else
        if (am_root())
@@ -407,7 +407,7 @@ generate_uid(void)
 static u32
 generate_gid(void)
 {
-#ifdef __WIN32__
+#ifdef _WIN32
        return 0;
 #else
        if (am_root())
@@ -416,7 +416,7 @@ generate_gid(void)
 #endif
 }
 
-#ifdef __WIN32__
+#ifdef _WIN32
 #  ifndef S_IFLNK
 #    define S_IFLNK  0120000
 #  endif
@@ -475,7 +475,11 @@ set_random_xattrs(struct wim_inode *inode)
        struct wim_xattr_entry *entry = (void *)entries;
        size_t entries_size;
        struct wimlib_unix_data unix_data;
+#ifdef _WIN32
+       const char *prefix = "";
+#else
        const char *prefix = "user.";
+#endif
        static const char capability_name[] = "security.capability";
        bool generated_capability_xattr = false;
 
@@ -497,6 +501,11 @@ set_random_xattrs(struct wim_inode *inode)
                int value_len = rand32() % 64;
                u8 *p;
 
+       #ifdef _WIN32
+               if (value_len == 0)
+                       value_len++;
+       #endif
+
                entry->value_len = cpu_to_le16(value_len);
                entry->flags = 0;
 
@@ -511,10 +520,14 @@ set_random_xattrs(struct wim_inode *inode)
 
                        entry->name_len = strlen(prefix) + name_len;
                        p = mempcpy(entry->name, prefix, strlen(prefix));
-                       *p++ = 'a' + i;
+                       *p++ = 'A' + i;
                        for (int j = 1; j < name_len; j++) {
                                do {
+                               #ifdef _WIN32
+                                       *p = 'A' + rand8() % 26;
+                               #else
                                        *p = rand8();
+                               #endif
                                } while (*p == '\0');
                                p++;
                        }
@@ -788,7 +801,8 @@ set_random_streams(struct wim_inode *inode, struct generation_context *ctx)
                        ret = add_random_data_stream(inode, ctx, stream_name);
                        if (ret)
                                return ret;
-                       stream_name[0] += cpu_to_le16(1);
+                       stream_name[0] =
+                               cpu_to_le16(le16_to_cpu(stream_name[0]) + 1);
                }
        }
 
@@ -890,7 +904,7 @@ retry:
         * within the same directory.  */
        hash = 0;
        for (const utf16lechar *p = name; *p; p++)
-               hash = (hash * 31) + *p;
+               hash = (hash * 31) + le16_to_cpu(*p);
        FREE(child->d_short_name);
        child->d_short_name = memdup(name, (name_len + 1) * 2);
        child->d_short_name_nbytes = name_len * 2;
@@ -1430,8 +1444,7 @@ cmp_xattrs(const struct wim_inode *inode1, const struct wim_inode *inode2,
        if (!xattrs1 && !xattrs2) {
                return 0;
        } else if (xattrs1 && !xattrs2) {
-               if (cmp_flags & (WIMLIB_CMP_FLAG_NTFS_3G_MODE |
-                                WIMLIB_CMP_FLAG_WINDOWS_MODE))
+               if (cmp_flags & WIMLIB_CMP_FLAG_NTFS_3G_MODE)
                        return 0;
                ERROR("%"TS" unexpectedly lost its xattrs",
                      inode_any_full_path(inode1));