From 9d4984371b09cc9c9001d817ecfedc5e407b1683 Mon Sep 17 00:00:00 2001 From: rgimad Date: Fri, 18 Apr 2025 16:38:04 +0300 Subject: [PATCH] fix some mistakes but still the same segfault --- Makefile | 2 +- kpack_c.c | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index b47bd12..68b9ab5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-Wall -Wextra +CFLAGS=-Wall -Wextra -g LZMA_CFLAGS=-Wall -Wextra -O3 all: kpack_c kpack_c: kpack_c.o LZMAEncoder.o MatchFinder.o RangeCoder.o diff --git a/kpack_c.c b/kpack_c.c index 1126dc0..e0a296f 100644 --- a/kpack_c.c +++ b/kpack_c.c @@ -50,13 +50,16 @@ unsigned preprocess_calltrick(unsigned char *infile, unsigned insize, unsigned c if (buf == NULL) { goto pack_calltrick_fail; } + *ctn = 0; + *cti = 0; memset(ct1, 0, 256); unsigned char *infile_ptr = infile; unsigned count = insize; unsigned char *buf_ptr = buf; do { - unsigned al = *infile_ptr++; + //printf("infile_ptr = %p, count = %u\n", infile_ptr, count); + unsigned al = *(infile_ptr++); al -= 0x0E8; if (al > 1) { continue; @@ -76,8 +79,8 @@ unsigned preprocess_calltrick(unsigned char *infile, unsigned insize, unsigned c num = byteswap(num); memcpy(infile_ptr - 4, &num, 4); (*ctn)++; - memcpy(buf_ptr, &infile_ptr, 4); - buf_ptr += 8; + memcpy(buf_ptr, &infile_ptr, sizeof(buf_ptr)); + buf_ptr += sizeof(buf_ptr); } } while(count > 0); @@ -91,11 +94,11 @@ unsigned preprocess_calltrick(unsigned char *infile, unsigned insize, unsigned c } *cti = ~(unsigned char)256; - unsigned tmp; + unsigned char *tmp; while (buf_ptr != buf) { - buf_ptr -= 8; - memcpy(&tmp, buf_ptr, 4); - memcpy((unsigned char*)(size_t)(tmp - 4), cti, 1); + buf_ptr -= sizeof(tmp); + memcpy(&tmp, buf_ptr, sizeof(tmp)); + memcpy(tmp - 4, cti, 1); } free(buf); return tmp;