From 5ed8f629ec74af29a5bdb7a44decd6dfeff4b7e7 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Tue, 6 May 2025 07:55:07 +0200 Subject: [PATCH 1/3] something I worked on --- src/parser.rs | 114 ++++++++++++--- tests/parse_patch.rs | 1 - tests/parse_samples.rs | 5 +- tests/wild-samples/0001-cross.patch | 52 ------- tests/wild-samples/CVE-2019-12211-13.patch | 162 --------------------- tests/wild-samples/define_byteswap.patch | 27 ---- tests/wild-samples/sample5.patch | 29 ---- tests/wild-samples/usecmake.patch | 143 ------------------ 8 files changed, 95 insertions(+), 438 deletions(-) delete mode 100644 tests/wild-samples/0001-cross.patch delete mode 100644 tests/wild-samples/CVE-2019-12211-13.patch delete mode 100644 tests/wild-samples/define_byteswap.patch delete mode 100644 tests/wild-samples/sample5.patch delete mode 100644 tests/wild-samples/usecmake.patch diff --git a/src/parser.rs b/src/parser.rs index 8c7b00b..28c70a5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,6 +3,7 @@ use std::error::Error; use chrono::DateTime; use nom::combinator::verify; +use nom::complete::take; use nom::*; use nom::{ branch::alt, @@ -265,31 +266,20 @@ fn is_next_header(input: Input<'_>) -> bool { /// to figure out how to count in nom more robustly than many1!(). Maybe using switch!()? ///FIXME: The test_parse_triple_plus_minus_hack test will no longer panic when this is fixed. fn chunk(input: Input<'_>) -> IResult, Hunk> { + // First, parse the chunk header to get range information let (input, ranges) = chunk_header(input)?; + let (old_range, new_range, range_hint) = ranges; - // Parse chunk lines, using the range information to guide parsing - let (input, lines) = many0(verify( - alt(( - // Detect added lines - map( - preceded(tuple((char('+'), not(tag("++ ")))), consume_content_line), - Line::Add, - ), - // Detect removed lines - map( - preceded(tuple((char('-'), not(tag("-- ")))), consume_content_line), - Line::Remove, - ), - // Detect context lines - map(preceded(char(' '), consume_content_line), Line::Context), - // Handle empty lines within the chunk - map(tag("\n"), |_| Line::Context("")), - )), - // Stop parsing when we detect the next header or have parsed the expected number of lines - |_| !is_next_header(input), - ))(input)?; + // Calculate total expected lines + let total_context = old_range.count; + let total_added = new_range.count; + + let (input, lines) = parse_hunk_lines( + input, + old_range.count as usize, + new_range.count as usize, + )?; - let (old_range, new_range, range_hint) = ranges; Ok(( input, Hunk { @@ -301,6 +291,86 @@ fn chunk(input: Input<'_>) -> IResult, Hunk> { )) } +fn parse_hunk_lines<'a>( + mut input: Input<'a>, + old_count: usize, + new_count: usize, +) -> IResult, Vec>> { + use nom::{ + branch::alt, + bytes::complete::tag, + character::complete::{char, line_ending, not_line_ending}, + combinator::{map, opt}, + sequence::preceded, + IResult, + }; + + enum LineKind<'b> { + Add(&'b str), + Remove(&'b str), + Context(&'b str), + EmptyContext, + } + + let mut lines = Vec::new(); + let mut context = 0; + let mut added = 0; + let mut removed = 0; + + while context + removed < old_count || context + added < new_count { + let (rest, kind) = alt(( + // +added line + map(preceded(char('+'), not_line_ending), |s: Input<'a>| LineKind::Add(*s.fragment())), + // -removed line + map(preceded(char('-'), not_line_ending), |s: Input<'a>| LineKind::Remove(*s.fragment())), + // ' ' context line (possibly empty after the space) + map(preceded(char(' '), opt(not_line_ending)), |opt_s: Option>| { + LineKind::Context(opt_s.map(|s| *s.fragment()).unwrap_or("")) + }), + // bare newline (no prefix at all) = empty context line + map(line_ending, |_| LineKind::EmptyContext), + ))(input)?; + + // For all but EmptyContext, consume the line ending + + let (rest, _) = match &kind { + LineKind::EmptyContext => (rest, ()), // already consumed + _ => { + let (rest, _) = line_ending(rest)?; + (rest, ()) + } + }; + // Update counters and build Line + let line = match kind { + LineKind::Add(s) => { + added += 1; + Line::Add(s) + } + LineKind::Remove(s) => { + removed += 1; + Line::Remove(s) + } + LineKind::Context(s) => { + context += 1; + Line::Context(s) + } + LineKind::EmptyContext => { + context += 1; + Line::Context("") + } + }; + + lines.push(line); + input = rest; + + if context + removed == old_count && context + added == new_count { + break; + } + } + + Ok((input, lines)) +} + fn chunk_header(input: Input<'_>) -> IResult, (Range, Range, &'_ str)> { let (input, _) = tag("@@ -")(input)?; let (input, old_range) = range(input)?; diff --git a/tests/parse_patch.rs b/tests/parse_patch.rs index da25ada..982c2f6 100644 --- a/tests/parse_patch.rs +++ b/tests/parse_patch.rs @@ -240,7 +240,6 @@ fn test_parse_triple_plus_minus() -> Result<(), ParseError<'static>> { // actually takes the hunk ranges into account, the #[should_panic] annotation should be removed. // See the FIXME comment on top of the chunk_line parser. #[test] -#[should_panic] fn test_parse_triple_plus_minus_hack() { // Our parser has some hacky rules to make sure that lines starting with +++ or --- aren't // interpreted as regular addition/removal lines that could be part of a hunk. This test diff --git a/tests/parse_samples.rs b/tests/parse_samples.rs index aac580d..765be82 100644 --- a/tests/parse_samples.rs +++ b/tests/parse_samples.rs @@ -45,9 +45,10 @@ fn parse_wild_samples() { } let data = fs::read_to_string(dbg!(&path)).unwrap(); - let patches = Patch::from_multiple(&data) - .unwrap_or_else(|err| panic!("failed to parse {:?}, error: {}", path, err)); + let patches = Patch::from_multiple(&data); + println!("Patches: {:?}", patches); + let patches = patches.unwrap_or_else(|err| panic!("failed to parse {:?}, error: {}", path, err)); // Make sure that the patch file we produce parses to the same information as the original // patch file. let patch_file: String = patches diff --git a/tests/wild-samples/0001-cross.patch b/tests/wild-samples/0001-cross.patch deleted file mode 100644 index 5398fb2..0000000 --- a/tests/wild-samples/0001-cross.patch +++ /dev/null @@ -1,52 +0,0 @@ -diff --git a/Makefile b/Makefile -index 9754ddf..b5512de 100644 ---- a/Makefile -+++ b/Makefile -@@ -15,16 +15,16 @@ - SHELL=/bin/sh - - # To assist in cross-compiling --CC=gcc --AR=ar --RANLIB=ranlib --LDFLAGS= -+CC?=gcc -+AR?=ar -+RANLIB?=ranlib - - BIGFILES=-D_FILE_OFFSET_BITS=64 --CFLAGS=-Wall -Winline -O2 -g $(BIGFILES) -+CFLAGS?=-Wall -Winline -O2 -g -+CFLAGS=$(CFLAGS) $(BIGFILES) - - # Where you want it installed when you do 'make install' --PREFIX=/usr/local -+PREFIX=$PREFIX - - - OBJS= blocksort.o \ -diff --git a/Makefile-libbz2_so b/Makefile-libbz2_so -index e58791b..f4b9fa2 100644 ---- a/Makefile-libbz2_so -+++ b/Makefile-libbz2_so -@@ -22,9 +22,18 @@ - - - SHELL=/bin/sh --CC=gcc -+ -+# To assist in cross-compiling -+CC?=gcc -+AR?=ar -+RANLIB?=ranlib -+ - BIGFILES=-D_FILE_OFFSET_BITS=64 --CFLAGS=-fpic -fPIC -Wall -Winline -O2 -g $(BIGFILES) -+CFLAGS?=-Wall -Winline -O2 -g -+CFLAGS=$(CFLAGS) $(BIGFILES) -+ -+# Where you want it installed when you do 'make install' -+PREFIX=$PREFIX - - OBJS= blocksort.o \ - huffman.o \ diff --git a/tests/wild-samples/CVE-2019-12211-13.patch b/tests/wild-samples/CVE-2019-12211-13.patch deleted file mode 100644 index 1260786..0000000 --- a/tests/wild-samples/CVE-2019-12211-13.patch +++ /dev/null @@ -1,162 +0,0 @@ -Index: freeimage/Source/FreeImage/PluginTIFF.cpp -=================================================================== ---- freeimage.orig/Source/FreeImage/PluginTIFF.cpp -+++ freeimage/Source/FreeImage/PluginTIFF.cpp -@@ -122,9 +122,14 @@ static void ReadThumbnail(FreeImageIO *i - static int s_format_id; - - typedef struct { -+ //! FreeImage IO functions - FreeImageIO *io; -+ //! FreeImage handle - fi_handle handle; -+ //! LibTIFF handle - TIFF *tif; -+ //! Count the number of thumbnails already read (used to avoid recursion on loading) -+ unsigned thumbnailCount; - } fi_TIFFIO; - - // ---------------------------------------------------------- -@@ -184,10 +189,8 @@ Open a TIFF file descriptor for reading - */ - TIFF * - TIFFFdOpen(thandle_t handle, const char *name, const char *mode) { -- TIFF *tif; -- - // Open the file; the callback will set everything up -- tif = TIFFClientOpen(name, mode, handle, -+ TIFF *tif = TIFFClientOpen(name, mode, handle, - _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc, - _tiffSizeProc, _tiffMapProc, _tiffUnmapProc); - -@@ -460,9 +463,9 @@ CreateImageType(BOOL header_only, FREE_I - } - - } -- else { -+ else if (bpp <= 32) { - -- dib = FreeImage_AllocateHeader(header_only, width, height, MIN(bpp, 32), FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); -+ dib = FreeImage_AllocateHeader(header_only, width, height, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); - } - - -@@ -1053,6 +1056,7 @@ Open(FreeImageIO *io, fi_handle handle, - if(!fio) return NULL; - fio->io = io; - fio->handle = handle; -+ fio->thumbnailCount = 0; - - if (read) { - fio->tif = TIFFFdOpen((thandle_t)fio, "", "r"); -@@ -1108,6 +1112,27 @@ check for uncommon bitspersample values - */ - static BOOL - IsValidBitsPerSample(uint16 photometric, uint16 bitspersample, uint16 samplesperpixel) { -+ // get the pixel depth in bits -+ const uint16 pixel_depth = bitspersample * samplesperpixel; -+ -+ // check for a supported pixel depth -+ switch (pixel_depth) { -+ case 1: -+ case 4: -+ case 8: -+ case 16: -+ case 24: -+ case 32: -+ case 48: -+ case 64: -+ case 96: -+ case 128: -+ // OK, go on -+ break; -+ default: -+ // unsupported pixel depth -+ return FALSE; -+ } - - switch(bitspersample) { - case 1: -@@ -1148,6 +1173,8 @@ IsValidBitsPerSample(uint16 photometric, - default: - return FALSE; - } -+ -+ return FALSE; - } - - static TIFFLoadMethod -@@ -1237,16 +1264,32 @@ Read embedded thumbnail - static void - ReadThumbnail(FreeImageIO *io, fi_handle handle, void *data, TIFF *tiff, FIBITMAP *dib) { - FIBITMAP* thumbnail = NULL; -+ -+ fi_TIFFIO *fio = (fi_TIFFIO*)data; -+ -+ /* -+ Thumbnail loading can cause recursions because of the way -+ functions TIFFLastDirectory and TIFFSetSubDirectory are working. -+ We use here a hack to count the number of times the ReadThumbnail function was called. -+ We only allow one call, check for this -+ */ -+ if (fio->thumbnailCount > 0) { -+ return; -+ } -+ else { -+ // update the thumbnail count (used to avoid recursion) -+ fio->thumbnailCount++; -+ } - - // read exif thumbnail (IFD 1) ... - -- /* -- // this code can cause unwanted recursion causing an overflow, it is thus disabled until we have a better solution -- // do we really need to read a thumbnail from the Exif segment ? knowing that TIFF store the thumbnail in the subIFD ... -- // - toff_t exif_offset = 0; - if(TIFFGetField(tiff, TIFFTAG_EXIFIFD, &exif_offset)) { - -+ // this code can cause unwanted recursion causing an overflow, -+ // because of the way TIFFLastDirectory work => this is checked -+ // using -+ - if(!TIFFLastDirectory(tiff)) { - // save current position - const long tell_pos = io->tell_proc(handle); -@@ -1264,7 +1307,6 @@ ReadThumbnail(FreeImageIO *io, fi_handle - TIFFSetDirectory(tiff, cur_dir); - } - } -- */ - - // ... or read the first subIFD - -@@ -1281,6 +1323,10 @@ ReadThumbnail(FreeImageIO *io, fi_handle - const long tell_pos = io->tell_proc(handle); - const uint16 cur_dir = TIFFCurrentDirectory(tiff); - -+ // this code can cause unwanted recursion -+ // causing an overflow, because of the way -+ // TIFFSetSubDirectory work -+ - if(TIFFSetSubDirectory(tiff, subIFD_offsets[0])) { - // load the thumbnail - int page = -1; -@@ -2041,7 +2087,7 @@ Load(FreeImageIO *io, fi_handle handle, - } - - // calculate src line and dst pitch -- int dst_pitch = FreeImage_GetPitch(dib); -+ unsigned dst_pitch = FreeImage_GetPitch(dib); - uint32 tileRowSize = (uint32)TIFFTileRowSize(tif); - uint32 imageRowSize = (uint32)TIFFScanlineSize(tif); - -@@ -2071,7 +2117,7 @@ Load(FreeImageIO *io, fi_handle handle, - BYTE *src_bits = tileBuffer; - BYTE *dst_bits = bits + rowSize; - for(int k = 0; k < nrows; k++) { -- memcpy(dst_bits, src_bits, src_line); -+ memcpy(dst_bits, src_bits, MIN(dst_pitch, src_line)); - src_bits += tileRowSize; - dst_bits -= dst_pitch; - } diff --git a/tests/wild-samples/define_byteswap.patch b/tests/wild-samples/define_byteswap.patch deleted file mode 100644 index e3c0480..0000000 --- a/tests/wild-samples/define_byteswap.patch +++ /dev/null @@ -1,27 +0,0 @@ -diff --git a/image/decode/segdec.c b/image/decode/segdec.c -index fb83f2b..1eb9ae4 100644 ---- a/image/decode/segdec.c -+++ b/image/decode/segdec.c -@@ -52,6 +52,9 @@ static Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInf - //================================================================ - // Memory access functions - //================================================================ -+ -+U32 _byteswap_ulong(U32 bits); -+ - static U32 _FORCEINLINE _load4(void* pv) - { - #ifdef _BIG__ENDIAN_ -diff --git a/jxrgluelib/JXRGlueJxr.c b/jxrgluelib/JXRGlueJxr.c -index 0fde9bb..e6c54e4 100644 ---- a/jxrgluelib/JXRGlueJxr.c -+++ b/jxrgluelib/JXRGlueJxr.c -@@ -28,7 +28,7 @@ - //*@@@---@@@@****************************************************************** - #include - #include -- -+#include - - static const char szHDPhotoFormat[] = "image/vnd.ms-photo"; - const U32 IFDEntryTypeSizes[] = { 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 }; diff --git a/tests/wild-samples/sample5.patch b/tests/wild-samples/sample5.patch deleted file mode 100644 index ec0eeb8..0000000 --- a/tests/wild-samples/sample5.patch +++ /dev/null @@ -1,29 +0,0 @@ -=== modified file 'modified_file1' ---- modified_file1 2013-10-13 23:53:13 +0000 -+++ modified_file1 2013-10-13 23:53:26 +0000 -@@ -1,5 +1,7 @@ - This is the original content. - --This should be updated. -+This is now updated. -+ -+This is a new line. - - This will stay. -\ No newline at end of file - -=== modified file 'modified_file2' ---- modified_file2 2013-10-13 23:53:13 +0000 -+++ modified_file2 2013-10-13 23:53:26 +0000 -@@ -1,5 +1,7 @@ - This is the original content. - --This should be updated. -+This is now updated. -+ -+This is a new line. - - This will stay. -\ No newline at end of file - - diff --git a/tests/wild-samples/usecmake.patch b/tests/wild-samples/usecmake.patch deleted file mode 100644 index 139e0ba..0000000 --- a/tests/wild-samples/usecmake.patch +++ /dev/null @@ -1,143 +0,0 @@ -Description: Prefer a cmake based build system -Author: Mathieu Malaterre -Forwarded: https://jxrlib.codeplex.com/discussions/440294 - -Index: CMakeLists.txt -=================================================================== ---- CMakeLists.txt 1970-01-01 00:00:00.000000000 +0000 -+++ CMakeLists.txt 2014-03-26 17:05:04.429637801 +0100 -@@ -0,0 +1,134 @@ -+# Copyright Mathieu Malaterre -+# BSD (Same as jxrlib) -+cmake_minimum_required(VERSION 2.8) -+project(jxrlib C) -+ -+# Need shared libs for ABI -+set(BUILD_SHARED_LIBS ON) -+ -+# helper macro to preserve original Makefile convention -+macro(JXR_MAKE_OBJ SET_NAME) -+ foreach(src ${SRC_${SET_NAME}}) -+ list(APPEND OBJ_${SET_NAME} ${DIR_${SET_NAME}}/${src}) -+ endforeach() -+endmacro() -+ -+include(TestBigEndian) -+test_big_endian(ISBIGENDIAN) -+if(ISBIGENDIAN) -+ set(DEF_ENDIAN _BIG__ENDIAN_) -+endif() -+ -+set(DIR_SYS image/sys) -+set(DIR_DEC image/decode) -+set(DIR_ENC image/encode) -+ -+set(DIR_GLUE jxrgluelib) -+set(DIR_TEST jxrtestlib) -+set(DIR_EXEC jxrencoderdecoder) -+ -+if(NOT JXRLIB_INSTALL_BIN_DIR) -+ set(JXRLIB_INSTALL_BIN_DIR "bin") -+endif() -+ -+if(NOT JXRLIB_INSTALL_LIB_DIR) -+ set(JXRLIB_INSTALL_LIB_DIR "lib") -+endif() -+ -+if(NOT JXRLIB_INSTALL_INCLUDE_DIR) -+ set(JXRLIB_INSTALL_INCLUDE_DIR "include/jxrlib") -+endif() -+ -+include_directories( -+ common/include -+ ${DIR_SYS} -+ ${DIR_GLUE} -+ ${DIR_TEST} -+) -+ -+# where is strlcpy ? -+include(CheckSymbolExists) -+check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY) -+#set(CMAKE_REQUIRED_LIBRARIES bsd) -+#CHECK_SYMBOL_EXISTS(strlcpy "string.h" HAVE_STRLCPY4) -+# on linux, strlcpy is in -lbsd: -+#if(NOT HAVE_STRLCPY) -+# include(CheckLibraryExists) -+# find_library(BSD_LIBRARY bsd) -+# check_library_exists(bsd "strlcpy" ${BSD_LIBRARY} HAVE_STRLCPY_BSD) -+#endif() -+ -+# JPEG-XR -+set(SRC_SYS adapthuff.c image.c strcodec.c strPredQuant.c strTransform.c perfTimerANSI.c) -+JXR_MAKE_OBJ(SYS) -+set(SRC_DEC decode.c postprocess.c segdec.c strdec.c strInvTransform.c strPredQuantDec.c JXRTranscode.c) -+JXR_MAKE_OBJ(DEC) -+set(SRC_ENC encode.c segenc.c strenc.c strFwdTransform.c strPredQuantEnc.c) -+JXR_MAKE_OBJ(ENC) -+ -+add_library(jpegxr ${OBJ_ENC} ${OBJ_DEC} ${OBJ_SYS}) -+set_property(TARGET jpegxr -+ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} -+) -+set_property(TARGET jpegxr PROPERTY LINK_INTERFACE_LIBRARIES "") -+set_property(TARGET jpegxr PROPERTY COMPILE_FLAGS -w) -+# VERSION/SOVERSION -+set_property(TARGET jpegxr PROPERTY VERSION 1.1) -+set_property(TARGET jpegxr PROPERTY SOVERSION 0) -+install(TARGETS jpegxr -+ EXPORT JXRLibTargets -+ RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR} COMPONENT Applications -+ LIBRARY DESTINATION ${JXRLIB_INSTALL_LIB_DIR} COMPONENT Libraries -+) -+ -+# JXR-GLUE -+set(SRC_GLUE JXRGlue.c JXRMeta.c JXRGluePFC.c JXRGlueJxr.c) -+JXR_MAKE_OBJ(GLUE) -+set(SRC_TEST JXRTest.c JXRTestBmp.c JXRTestHdr.c JXRTestPnm.c JXRTestTif.c JXRTestYUV.c) -+JXR_MAKE_OBJ(TEST) -+ -+add_library(jxrglue ${OBJ_GLUE} ${OBJ_TEST}) -+set_property(TARGET jxrglue -+ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} -+) -+set_property(TARGET jxrglue PROPERTY LINK_INTERFACE_LIBRARIES "") -+set_property(TARGET jxrglue PROPERTY COMPILE_FLAGS -w) -+# VERSION/SOVERSION -+set_property(TARGET jxrglue PROPERTY VERSION 1.1) -+set_property(TARGET jxrglue PROPERTY SOVERSION 0) -+install(TARGETS jxrglue -+ EXPORT JXRLibTargets -+ RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR} COMPONENT Applications -+ LIBRARY DESTINATION ${JXRLIB_INSTALL_LIB_DIR} COMPONENT Libraries -+) -+#if(HAVE_STRLCPY_BSD) -+# target_link_libraries(jxrglue ${BSD_LIBRARY}) -+#endif() -+target_link_libraries(jxrglue jpegxr m) -+# Enc app files -+set(ENCAPP JxrEncApp) -+add_executable(${ENCAPP} ${DIR_EXEC}/${ENCAPP}.c) -+set_property(TARGET ${ENCAPP} -+ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} -+) -+set_property(TARGET ${ENCAPP} PROPERTY COMPILE_FLAGS -w) -+target_link_libraries(${ENCAPP} jxrglue) # jpegxr) -+install(TARGETS ${ENCAPP} RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR}) -+# Dec app files -+set(DECAPP JxrDecApp) -+add_executable(${DECAPP} ${DIR_EXEC}/${DECAPP}.c) -+set_property(TARGET ${DECAPP} -+ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} -+) -+set_property(TARGET ${DECAPP} PROPERTY COMPILE_FLAGS -w) -+target_link_libraries(${DECAPP} jxrglue) # jpegxr) -+install(TARGETS ${DECAPP} RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR}) -+ -+# install rules -+install(FILES jxrgluelib/JXRGlue.h jxrgluelib/JXRMeta.h jxrtestlib/JXRTest.h -+ image/sys/windowsmediaphoto.h -+ DESTINATION ${JXRLIB_INSTALL_INCLUDE_DIR} COMPONENT Headers -+) -+install(DIRECTORY common/include/ DESTINATION ${JXRLIB_INSTALL_INCLUDE_DIR} -+ FILES_MATCHING PATTERN "*.h" -+) From 21ec9f690190e47a73f82aa0aae26f6643f36ae3 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Tue, 6 May 2025 09:32:28 +0200 Subject: [PATCH 2/3] restore files --- tests/wild-samples/0001-cross.patch | 52 +++++++ tests/wild-samples/CVE-2019-12211-13.patch | 162 +++++++++++++++++++++ tests/wild-samples/define_byteswap.patch | 27 ++++ tests/wild-samples/sample5.patch | 29 ++++ tests/wild-samples/usecmake.patch | 143 ++++++++++++++++++ 5 files changed, 413 insertions(+) create mode 100644 tests/wild-samples/0001-cross.patch create mode 100644 tests/wild-samples/CVE-2019-12211-13.patch create mode 100644 tests/wild-samples/define_byteswap.patch create mode 100644 tests/wild-samples/sample5.patch create mode 100644 tests/wild-samples/usecmake.patch diff --git a/tests/wild-samples/0001-cross.patch b/tests/wild-samples/0001-cross.patch new file mode 100644 index 0000000..5398fb2 --- /dev/null +++ b/tests/wild-samples/0001-cross.patch @@ -0,0 +1,52 @@ +diff --git a/Makefile b/Makefile +index 9754ddf..b5512de 100644 +--- a/Makefile ++++ b/Makefile +@@ -15,16 +15,16 @@ + SHELL=/bin/sh + + # To assist in cross-compiling +-CC=gcc +-AR=ar +-RANLIB=ranlib +-LDFLAGS= ++CC?=gcc ++AR?=ar ++RANLIB?=ranlib + + BIGFILES=-D_FILE_OFFSET_BITS=64 +-CFLAGS=-Wall -Winline -O2 -g $(BIGFILES) ++CFLAGS?=-Wall -Winline -O2 -g ++CFLAGS=$(CFLAGS) $(BIGFILES) + + # Where you want it installed when you do 'make install' +-PREFIX=/usr/local ++PREFIX=$PREFIX + + + OBJS= blocksort.o \ +diff --git a/Makefile-libbz2_so b/Makefile-libbz2_so +index e58791b..f4b9fa2 100644 +--- a/Makefile-libbz2_so ++++ b/Makefile-libbz2_so +@@ -22,9 +22,18 @@ + + + SHELL=/bin/sh +-CC=gcc ++ ++# To assist in cross-compiling ++CC?=gcc ++AR?=ar ++RANLIB?=ranlib ++ + BIGFILES=-D_FILE_OFFSET_BITS=64 +-CFLAGS=-fpic -fPIC -Wall -Winline -O2 -g $(BIGFILES) ++CFLAGS?=-Wall -Winline -O2 -g ++CFLAGS=$(CFLAGS) $(BIGFILES) ++ ++# Where you want it installed when you do 'make install' ++PREFIX=$PREFIX + + OBJS= blocksort.o \ + huffman.o \ diff --git a/tests/wild-samples/CVE-2019-12211-13.patch b/tests/wild-samples/CVE-2019-12211-13.patch new file mode 100644 index 0000000..1260786 --- /dev/null +++ b/tests/wild-samples/CVE-2019-12211-13.patch @@ -0,0 +1,162 @@ +Index: freeimage/Source/FreeImage/PluginTIFF.cpp +=================================================================== +--- freeimage.orig/Source/FreeImage/PluginTIFF.cpp ++++ freeimage/Source/FreeImage/PluginTIFF.cpp +@@ -122,9 +122,14 @@ static void ReadThumbnail(FreeImageIO *i + static int s_format_id; + + typedef struct { ++ //! FreeImage IO functions + FreeImageIO *io; ++ //! FreeImage handle + fi_handle handle; ++ //! LibTIFF handle + TIFF *tif; ++ //! Count the number of thumbnails already read (used to avoid recursion on loading) ++ unsigned thumbnailCount; + } fi_TIFFIO; + + // ---------------------------------------------------------- +@@ -184,10 +189,8 @@ Open a TIFF file descriptor for reading + */ + TIFF * + TIFFFdOpen(thandle_t handle, const char *name, const char *mode) { +- TIFF *tif; +- + // Open the file; the callback will set everything up +- tif = TIFFClientOpen(name, mode, handle, ++ TIFF *tif = TIFFClientOpen(name, mode, handle, + _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc, + _tiffSizeProc, _tiffMapProc, _tiffUnmapProc); + +@@ -460,9 +463,9 @@ CreateImageType(BOOL header_only, FREE_I + } + + } +- else { ++ else if (bpp <= 32) { + +- dib = FreeImage_AllocateHeader(header_only, width, height, MIN(bpp, 32), FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); ++ dib = FreeImage_AllocateHeader(header_only, width, height, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); + } + + +@@ -1053,6 +1056,7 @@ Open(FreeImageIO *io, fi_handle handle, + if(!fio) return NULL; + fio->io = io; + fio->handle = handle; ++ fio->thumbnailCount = 0; + + if (read) { + fio->tif = TIFFFdOpen((thandle_t)fio, "", "r"); +@@ -1108,6 +1112,27 @@ check for uncommon bitspersample values + */ + static BOOL + IsValidBitsPerSample(uint16 photometric, uint16 bitspersample, uint16 samplesperpixel) { ++ // get the pixel depth in bits ++ const uint16 pixel_depth = bitspersample * samplesperpixel; ++ ++ // check for a supported pixel depth ++ switch (pixel_depth) { ++ case 1: ++ case 4: ++ case 8: ++ case 16: ++ case 24: ++ case 32: ++ case 48: ++ case 64: ++ case 96: ++ case 128: ++ // OK, go on ++ break; ++ default: ++ // unsupported pixel depth ++ return FALSE; ++ } + + switch(bitspersample) { + case 1: +@@ -1148,6 +1173,8 @@ IsValidBitsPerSample(uint16 photometric, + default: + return FALSE; + } ++ ++ return FALSE; + } + + static TIFFLoadMethod +@@ -1237,16 +1264,32 @@ Read embedded thumbnail + static void + ReadThumbnail(FreeImageIO *io, fi_handle handle, void *data, TIFF *tiff, FIBITMAP *dib) { + FIBITMAP* thumbnail = NULL; ++ ++ fi_TIFFIO *fio = (fi_TIFFIO*)data; ++ ++ /* ++ Thumbnail loading can cause recursions because of the way ++ functions TIFFLastDirectory and TIFFSetSubDirectory are working. ++ We use here a hack to count the number of times the ReadThumbnail function was called. ++ We only allow one call, check for this ++ */ ++ if (fio->thumbnailCount > 0) { ++ return; ++ } ++ else { ++ // update the thumbnail count (used to avoid recursion) ++ fio->thumbnailCount++; ++ } + + // read exif thumbnail (IFD 1) ... + +- /* +- // this code can cause unwanted recursion causing an overflow, it is thus disabled until we have a better solution +- // do we really need to read a thumbnail from the Exif segment ? knowing that TIFF store the thumbnail in the subIFD ... +- // + toff_t exif_offset = 0; + if(TIFFGetField(tiff, TIFFTAG_EXIFIFD, &exif_offset)) { + ++ // this code can cause unwanted recursion causing an overflow, ++ // because of the way TIFFLastDirectory work => this is checked ++ // using ++ + if(!TIFFLastDirectory(tiff)) { + // save current position + const long tell_pos = io->tell_proc(handle); +@@ -1264,7 +1307,6 @@ ReadThumbnail(FreeImageIO *io, fi_handle + TIFFSetDirectory(tiff, cur_dir); + } + } +- */ + + // ... or read the first subIFD + +@@ -1281,6 +1323,10 @@ ReadThumbnail(FreeImageIO *io, fi_handle + const long tell_pos = io->tell_proc(handle); + const uint16 cur_dir = TIFFCurrentDirectory(tiff); + ++ // this code can cause unwanted recursion ++ // causing an overflow, because of the way ++ // TIFFSetSubDirectory work ++ + if(TIFFSetSubDirectory(tiff, subIFD_offsets[0])) { + // load the thumbnail + int page = -1; +@@ -2041,7 +2087,7 @@ Load(FreeImageIO *io, fi_handle handle, + } + + // calculate src line and dst pitch +- int dst_pitch = FreeImage_GetPitch(dib); ++ unsigned dst_pitch = FreeImage_GetPitch(dib); + uint32 tileRowSize = (uint32)TIFFTileRowSize(tif); + uint32 imageRowSize = (uint32)TIFFScanlineSize(tif); + +@@ -2071,7 +2117,7 @@ Load(FreeImageIO *io, fi_handle handle, + BYTE *src_bits = tileBuffer; + BYTE *dst_bits = bits + rowSize; + for(int k = 0; k < nrows; k++) { +- memcpy(dst_bits, src_bits, src_line); ++ memcpy(dst_bits, src_bits, MIN(dst_pitch, src_line)); + src_bits += tileRowSize; + dst_bits -= dst_pitch; + } diff --git a/tests/wild-samples/define_byteswap.patch b/tests/wild-samples/define_byteswap.patch new file mode 100644 index 0000000..e3c0480 --- /dev/null +++ b/tests/wild-samples/define_byteswap.patch @@ -0,0 +1,27 @@ +diff --git a/image/decode/segdec.c b/image/decode/segdec.c +index fb83f2b..1eb9ae4 100644 +--- a/image/decode/segdec.c ++++ b/image/decode/segdec.c +@@ -52,6 +52,9 @@ static Int DecodeSignificantAbsLevel (struct CAdaptiveHuffman *pAHexpt, BitIOInf + //================================================================ + // Memory access functions + //================================================================ ++ ++U32 _byteswap_ulong(U32 bits); ++ + static U32 _FORCEINLINE _load4(void* pv) + { + #ifdef _BIG__ENDIAN_ +diff --git a/jxrgluelib/JXRGlueJxr.c b/jxrgluelib/JXRGlueJxr.c +index 0fde9bb..e6c54e4 100644 +--- a/jxrgluelib/JXRGlueJxr.c ++++ b/jxrgluelib/JXRGlueJxr.c +@@ -28,7 +28,7 @@ + //*@@@---@@@@****************************************************************** + #include + #include +- ++#include + + static const char szHDPhotoFormat[] = "image/vnd.ms-photo"; + const U32 IFDEntryTypeSizes[] = { 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 }; diff --git a/tests/wild-samples/sample5.patch b/tests/wild-samples/sample5.patch new file mode 100644 index 0000000..ec0eeb8 --- /dev/null +++ b/tests/wild-samples/sample5.patch @@ -0,0 +1,29 @@ +=== modified file 'modified_file1' +--- modified_file1 2013-10-13 23:53:13 +0000 ++++ modified_file1 2013-10-13 23:53:26 +0000 +@@ -1,5 +1,7 @@ + This is the original content. + +-This should be updated. ++This is now updated. ++ ++This is a new line. + + This will stay. +\ No newline at end of file + +=== modified file 'modified_file2' +--- modified_file2 2013-10-13 23:53:13 +0000 ++++ modified_file2 2013-10-13 23:53:26 +0000 +@@ -1,5 +1,7 @@ + This is the original content. + +-This should be updated. ++This is now updated. ++ ++This is a new line. + + This will stay. +\ No newline at end of file + + diff --git a/tests/wild-samples/usecmake.patch b/tests/wild-samples/usecmake.patch new file mode 100644 index 0000000..139e0ba --- /dev/null +++ b/tests/wild-samples/usecmake.patch @@ -0,0 +1,143 @@ +Description: Prefer a cmake based build system +Author: Mathieu Malaterre +Forwarded: https://jxrlib.codeplex.com/discussions/440294 + +Index: CMakeLists.txt +=================================================================== +--- CMakeLists.txt 1970-01-01 00:00:00.000000000 +0000 ++++ CMakeLists.txt 2014-03-26 17:05:04.429637801 +0100 +@@ -0,0 +1,134 @@ ++# Copyright Mathieu Malaterre ++# BSD (Same as jxrlib) ++cmake_minimum_required(VERSION 2.8) ++project(jxrlib C) ++ ++# Need shared libs for ABI ++set(BUILD_SHARED_LIBS ON) ++ ++# helper macro to preserve original Makefile convention ++macro(JXR_MAKE_OBJ SET_NAME) ++ foreach(src ${SRC_${SET_NAME}}) ++ list(APPEND OBJ_${SET_NAME} ${DIR_${SET_NAME}}/${src}) ++ endforeach() ++endmacro() ++ ++include(TestBigEndian) ++test_big_endian(ISBIGENDIAN) ++if(ISBIGENDIAN) ++ set(DEF_ENDIAN _BIG__ENDIAN_) ++endif() ++ ++set(DIR_SYS image/sys) ++set(DIR_DEC image/decode) ++set(DIR_ENC image/encode) ++ ++set(DIR_GLUE jxrgluelib) ++set(DIR_TEST jxrtestlib) ++set(DIR_EXEC jxrencoderdecoder) ++ ++if(NOT JXRLIB_INSTALL_BIN_DIR) ++ set(JXRLIB_INSTALL_BIN_DIR "bin") ++endif() ++ ++if(NOT JXRLIB_INSTALL_LIB_DIR) ++ set(JXRLIB_INSTALL_LIB_DIR "lib") ++endif() ++ ++if(NOT JXRLIB_INSTALL_INCLUDE_DIR) ++ set(JXRLIB_INSTALL_INCLUDE_DIR "include/jxrlib") ++endif() ++ ++include_directories( ++ common/include ++ ${DIR_SYS} ++ ${DIR_GLUE} ++ ${DIR_TEST} ++) ++ ++# where is strlcpy ? ++include(CheckSymbolExists) ++check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY) ++#set(CMAKE_REQUIRED_LIBRARIES bsd) ++#CHECK_SYMBOL_EXISTS(strlcpy "string.h" HAVE_STRLCPY4) ++# on linux, strlcpy is in -lbsd: ++#if(NOT HAVE_STRLCPY) ++# include(CheckLibraryExists) ++# find_library(BSD_LIBRARY bsd) ++# check_library_exists(bsd "strlcpy" ${BSD_LIBRARY} HAVE_STRLCPY_BSD) ++#endif() ++ ++# JPEG-XR ++set(SRC_SYS adapthuff.c image.c strcodec.c strPredQuant.c strTransform.c perfTimerANSI.c) ++JXR_MAKE_OBJ(SYS) ++set(SRC_DEC decode.c postprocess.c segdec.c strdec.c strInvTransform.c strPredQuantDec.c JXRTranscode.c) ++JXR_MAKE_OBJ(DEC) ++set(SRC_ENC encode.c segenc.c strenc.c strFwdTransform.c strPredQuantEnc.c) ++JXR_MAKE_OBJ(ENC) ++ ++add_library(jpegxr ${OBJ_ENC} ${OBJ_DEC} ${OBJ_SYS}) ++set_property(TARGET jpegxr ++ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} ++) ++set_property(TARGET jpegxr PROPERTY LINK_INTERFACE_LIBRARIES "") ++set_property(TARGET jpegxr PROPERTY COMPILE_FLAGS -w) ++# VERSION/SOVERSION ++set_property(TARGET jpegxr PROPERTY VERSION 1.1) ++set_property(TARGET jpegxr PROPERTY SOVERSION 0) ++install(TARGETS jpegxr ++ EXPORT JXRLibTargets ++ RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR} COMPONENT Applications ++ LIBRARY DESTINATION ${JXRLIB_INSTALL_LIB_DIR} COMPONENT Libraries ++) ++ ++# JXR-GLUE ++set(SRC_GLUE JXRGlue.c JXRMeta.c JXRGluePFC.c JXRGlueJxr.c) ++JXR_MAKE_OBJ(GLUE) ++set(SRC_TEST JXRTest.c JXRTestBmp.c JXRTestHdr.c JXRTestPnm.c JXRTestTif.c JXRTestYUV.c) ++JXR_MAKE_OBJ(TEST) ++ ++add_library(jxrglue ${OBJ_GLUE} ${OBJ_TEST}) ++set_property(TARGET jxrglue ++ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} ++) ++set_property(TARGET jxrglue PROPERTY LINK_INTERFACE_LIBRARIES "") ++set_property(TARGET jxrglue PROPERTY COMPILE_FLAGS -w) ++# VERSION/SOVERSION ++set_property(TARGET jxrglue PROPERTY VERSION 1.1) ++set_property(TARGET jxrglue PROPERTY SOVERSION 0) ++install(TARGETS jxrglue ++ EXPORT JXRLibTargets ++ RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR} COMPONENT Applications ++ LIBRARY DESTINATION ${JXRLIB_INSTALL_LIB_DIR} COMPONENT Libraries ++) ++#if(HAVE_STRLCPY_BSD) ++# target_link_libraries(jxrglue ${BSD_LIBRARY}) ++#endif() ++target_link_libraries(jxrglue jpegxr m) ++# Enc app files ++set(ENCAPP JxrEncApp) ++add_executable(${ENCAPP} ${DIR_EXEC}/${ENCAPP}.c) ++set_property(TARGET ${ENCAPP} ++ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} ++) ++set_property(TARGET ${ENCAPP} PROPERTY COMPILE_FLAGS -w) ++target_link_libraries(${ENCAPP} jxrglue) # jpegxr) ++install(TARGETS ${ENCAPP} RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR}) ++# Dec app files ++set(DECAPP JxrDecApp) ++add_executable(${DECAPP} ${DIR_EXEC}/${DECAPP}.c) ++set_property(TARGET ${DECAPP} ++ PROPERTY COMPILE_DEFINITIONS __ANSI__ DISABLE_PERF_MEASUREMENT ${DEF_ENDIAN} ++) ++set_property(TARGET ${DECAPP} PROPERTY COMPILE_FLAGS -w) ++target_link_libraries(${DECAPP} jxrglue) # jpegxr) ++install(TARGETS ${DECAPP} RUNTIME DESTINATION ${JXRLIB_INSTALL_BIN_DIR}) ++ ++# install rules ++install(FILES jxrgluelib/JXRGlue.h jxrgluelib/JXRMeta.h jxrtestlib/JXRTest.h ++ image/sys/windowsmediaphoto.h ++ DESTINATION ${JXRLIB_INSTALL_INCLUDE_DIR} COMPONENT Headers ++) ++install(DIRECTORY common/include/ DESTINATION ${JXRLIB_INSTALL_INCLUDE_DIR} ++ FILES_MATCHING PATTERN "*.h" ++) From ac9824d4ff7e2920b019630df04d4f24807cb988 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Tue, 6 May 2025 09:33:10 +0200 Subject: [PATCH 3/3] add another sample --- tests/wild-samples/sample2.patch | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/wild-samples/sample2.patch diff --git a/tests/wild-samples/sample2.patch b/tests/wild-samples/sample2.patch new file mode 100644 index 0000000..857997d --- /dev/null +++ b/tests/wild-samples/sample2.patch @@ -0,0 +1,53 @@ +# HG changeset patch +# Parent 13ba6cbdb304cd251fbc22466cadb21019ee817f +# User Bill McCloskey + +diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp +--- a/content/base/src/nsContentUtils.cpp ++++ b/content/base/src/nsContentUtils.cpp +@@ -6369,17 +6369,17 @@ public: + nsCycleCollectionParticipant* helper) + { + } + + NS_IMETHOD_(void) NoteNextEdgeName(const char* name) + { + } + +- NS_IMETHOD_(void) NoteWeakMapping(void* map, void* key, void* val) ++ NS_IMETHOD_(void) NoteWeakMapping(void* map, void* key, void* kdelegate, void* val) + { + } + + bool mFound; + + private: + void* mWrapper; + }; +diff --git a/js/src/jsfriendapi.cpp b/js/src/jsfriendapi.cpp +--- a/js/src/jsfriendapi.cpp ++++ b/js/src/jsfriendapi.cpp +@@ -527,16 +527,24 @@ js::VisitGrayWrapperTargets(JSCompartmen + { + for (WrapperMap::Enum e(comp->crossCompartmentWrappers); !e.empty(); e.popFront()) { + gc::Cell *thing = e.front().key.wrapped; + if (thing->isMarked(gc::GRAY)) + callback(closure, thing); + } + } + ++JS_FRIEND_API(JSObject *) ++js::GetWeakmapKeyDelegate(JSObject *key) ++{ ++ if (JSWeakmapKeyDelegateOp op = key->getClass()->ext.weakmapKeyDelegateOp) ++ return op(key); ++ return NULL; ++} ++ + JS_FRIEND_API(void) + JS_SetAccumulateTelemetryCallback(JSRuntime *rt, JSAccumulateTelemetryDataCallback callback) + { + rt->telemetryCallback = callback; + } + + JS_FRIEND_API(JSObject *)