Skip to content

Commit f4fd03f

Browse files
committed
Increase buffer sizes for safer string handling
Update getFileLength for cross-platform compatibility Use larger static buffers in describe.c to prevent overflows. Refactor getFileLength in helper.c to handle file length retrieval safely on both Windows and POSIX systems.
1 parent 644aeca commit f4fd03f

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

‎describe.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,12 @@ void extract_placeholder(const char *descriptionTemplate, char *placeholder) {
375375
const char* describe(unsigned long pos, const char* input, const char *translationTemplate) {
376376
regex_t regex;
377377
int result;
378-
static char lookupValue[100];
379-
static char explanation[200]; // Increased size to accommodate additional text
380-
static char tempExplanation[200];
381-
char mainInput[100];
382-
char offsetPart[100];
383-
char tempInput[100];
378+
static char lookupValue[512];
379+
static char explanation[512];
380+
static char tempExplanation[512];
381+
char mainInput[256];
382+
char offsetPart[256];
383+
char tempInput[256];
384384

385385
// Copy input to tempInput for manipulation
386386
strncpy(tempInput, input, sizeof(tempInput));
@@ -412,7 +412,7 @@ const char* describe(unsigned long pos, const char* input, const char *translati
412412
int internal = dec_memory >= patch_address_start && dec_memory <= patch_address_end;
413413
int jump_size = dec_memory - pos;
414414

415-
static char internal_jump_details[100];
415+
static char internal_jump_details[256];
416416
if (internal)
417417
snprintf(internal_jump_details, sizeof(internal_jump_details), " (patch internal by %i bytes)", jump_size);
418418
snprintf(lookupValue, sizeof(lookupValue), "0x%s%s", hex_memory, internal_jump_details);
@@ -434,7 +434,7 @@ const char* describe(unsigned long pos, const char* input, const char *translati
434434
const char* template = translationTable[i].description;
435435
// Now, if offsetPart is not empty, append explanation
436436
if (offsetPart[0] != '\0') {
437-
static char extendedLookup[200];
437+
static char extendedLookup[256];
438438
char* offsetTrimmed = offsetPart;
439439
while (isspace((unsigned char)*offsetTrimmed)) {
440440
offsetTrimmed++;

‎helper.c‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
#include <stdbool.h>
1212
#include <string.h>
1313
#include <ctype.h>
14+
#include <sys/stat.h>
15+
16+
#ifdef _WIN32
17+
#include <io.h>
18+
#endif
1419

1520
#include "dispel.h"
1621

@@ -116,14 +121,23 @@ int AllASCII(unsigned char *b, int size)
116121
}
117122

118123
unsigned long getFileLength(FILE **fin) {
119-
#if defined(__APPLE__) || defined(__linux__)
120-
fseek(*fin, 0L, SEEK_END);
121-
unsigned long len = ftell(*fin);
122-
fseek(*fin, 0L, SEEK_SET);
123-
#else
124-
unsigned long len = filelength(fileno(*fin));
125-
#endif
126-
return len;
124+
if (!fin || !*fin) return 0;
125+
126+
#ifdef _WIN32
127+
int fd = _fileno(*fin);
128+
if (fd == -1) return 0;
129+
long len = _filelength(fd);
130+
if (len < 0) return 0;
131+
return (unsigned long)len;
132+
#else
133+
long pos = ftell(*fin);
134+
if (pos == -1L) pos = 0;
135+
if (fseek(*fin, 0L, SEEK_END) != 0) return 0;
136+
long len = ftell(*fin);
137+
(void)fseek(*fin, pos, SEEK_SET);
138+
if (len < 0) return 0;
139+
return (unsigned long)len;
140+
#endif
127141
}
128142

129143
unsigned char *allocateMemory(unsigned long len) {

0 commit comments

Comments
 (0)