From 62a3d33243eb92a382e65b062c14ef64feb99b70 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 2 Aug 2025 08:07:56 +0000 Subject: [PATCH] [autofix.ci] apply automated fixes --- src/pe.zig | 22 ++++++------ test/bundler/windows-pe-checksum.test.ts | 45 +++++++++++++----------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/pe.zig b/src/pe.zig index 2e21fd461a..9b36e1f22d 100644 --- a/src/pe.zig +++ b/src/pe.zig @@ -371,7 +371,7 @@ pub const PEFile = struct { const updated_optional_header = self.getOptionalHeader(); updated_optional_header.size_of_image = alignSize(new_section.virtual_address + new_section.virtual_size, updated_optional_header.section_alignment); updated_optional_header.size_of_initialized_data += new_section.size_of_raw_data; - + // Update PE checksum after adding section self.updateChecksum(); } @@ -432,13 +432,13 @@ pub const PEFile = struct { pub fn calculateChecksum(self: *const PEFile) u32 { const data = self.data.items; const file_size = data.len; - + // Find checksum field offset const checksum_offset = self.optional_header_offset + @offsetOf(OptionalHeader64, "checksum"); - + var checksum: u64 = 0; var i: usize = 0; - + // Process file as 16-bit words while (i + 1 < file_size) : (i += 2) { // Skip the checksum field itself (4 bytes) @@ -446,17 +446,17 @@ pub const PEFile = struct { i += 2; // Skip 4 bytes total continue; } - + // Add 16-bit word to checksum const word = std.mem.readInt(u16, data[i..][0..2], .little); checksum += word; - + // Handle overflow - fold back the carry if (checksum > 0xFFFF) { checksum = (checksum & 0xFFFF) + (checksum >> 16); } } - + // If file size is odd, last byte is treated as if followed by 0x00 if (file_size & 1 != 0) { checksum += data[file_size - 1]; @@ -464,14 +464,14 @@ pub const PEFile = struct { checksum = (checksum & 0xFFFF) + (checksum >> 16); } } - + // Final fold checksum = (checksum & 0xFFFF) + (checksum >> 16); checksum = (checksum + (checksum >> 16)) & 0xFFFF; - + // Add file size to checksum checksum += file_size; - + return @intCast(checksum); } @@ -651,7 +651,7 @@ pub const PEFile = struct { // Update the resource section try self.updateResourceSection(rsrc_section.?, resource_data); - + // Update PE checksum after all modifications self.updateChecksum(); } diff --git a/test/bundler/windows-pe-checksum.test.ts b/test/bundler/windows-pe-checksum.test.ts index 2fcab5b25a..68867e59d2 100644 --- a/test/bundler/windows-pe-checksum.test.ts +++ b/test/bundler/windows-pe-checksum.test.ts @@ -28,10 +28,7 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => { stderr: "pipe", }); - const [buildStderr, buildExitCode] = await Promise.all([ - new Response(buildProc.stderr).text(), - buildProc.exited, - ]); + const [buildStderr, buildExitCode] = await Promise.all([new Response(buildProc.stderr).text(), buildProc.exited]); expect(buildExitCode).toBe(0); expect(buildStderr).toBe(""); @@ -53,10 +50,10 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => { // Extract checksum from objdump output const checksumMatch = objdumpStdout.match(/CheckSum\s+([0-9a-fA-F]+)/); expect(checksumMatch).not.toBeNull(); - + const checksum = checksumMatch![1]; console.log("PE checksum:", checksum); - + // Checksum should not be 0 after our implementation expect(checksum).not.toBe("00000000"); }); @@ -89,10 +86,7 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => { stderr: "pipe", }); - const [buildStderr, buildExitCode] = await Promise.all([ - new Response(buildProc.stderr).text(), - buildProc.exited, - ]); + const [buildStderr, buildExitCode] = await Promise.all([new Response(buildProc.stderr).text(), buildProc.exited]); expect(buildExitCode).toBe(0); expect(buildStderr).toBe(""); @@ -113,10 +107,10 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => { const checksumMatch = objdumpStdout.match(/CheckSum\s+([0-9a-fA-F]+)/); expect(checksumMatch).not.toBeNull(); - + const checksum = checksumMatch![1]; console.log("PE checksum with resources:", checksum); - + // Checksum should not be 0 expect(checksum).not.toBe("00000000"); }); @@ -126,9 +120,12 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => { function createTestIcon() { // ICO header (6 bytes) const header = Buffer.from([ - 0x00, 0x00, // Reserved - 0x01, 0x00, // Type (1 = ICO) - 0x01, 0x00, // Count (1 icon) + 0x00, + 0x00, // Reserved + 0x01, + 0x00, // Type (1 = ICO) + 0x01, + 0x00, // Count (1 icon) ]); // Directory entry (16 bytes) @@ -137,10 +134,18 @@ function createTestIcon() { 0x10, // Height (16) 0x00, // Color count (0 = 256 colors) 0x00, // Reserved - 0x01, 0x00, // Planes - 0x08, 0x00, // Bit count - 0x28, 0x01, 0x00, 0x00, // Bytes in resource (296) - 0x16, 0x00, 0x00, 0x00, // Image offset (22) + 0x01, + 0x00, // Planes + 0x08, + 0x00, // Bit count + 0x28, + 0x01, + 0x00, + 0x00, // Bytes in resource (296) + 0x16, + 0x00, + 0x00, + 0x00, // Image offset (22) ]); // Minimal BMP data @@ -156,4 +161,4 @@ function createTestIcon() { const imageData = Buffer.alloc(256); // 16x16x8bpp return Buffer.concat([header, dirEntry, bmpHeader, imageData]); -} \ No newline at end of file +}