Skip to content

Commit dd4cbd5

Browse files
Merge pull request #74 from NathanNeurotic/codex/fix-codacy-issues-in-specified-files
Harden debug logging and fix unsafe/partial reads
2 parents 5ae8a87 + a7af8a1 commit dd4cbd5

6 files changed

Lines changed: 44 additions & 35 deletions

File tree

include/debugprintf.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ static inline void debugprintf(const char *fmt, ...)
5252
#define NO_DPRINTF
5353
#endif
5454

55-
#define DPRINTF(...) debugprintf(__VA_ARGS__)
55+
#ifdef NO_DPRINTF
56+
#define DPRINTF(...) ((void)0)
57+
#define DPRINTS(s) ((void)0)
58+
#else
59+
#define DPRINTF(fmt, ...) debugprintf("" fmt "", ##__VA_ARGS__)
60+
#define DPRINTS(s) debugprintf("%s", (s))
61+
#endif
5662

5763
#endif //DEBUG_PRINTF

mk_kelf.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ cp LICENSE kelf/LICENSE.TXT
4646
cp README.md kelf/README.md
4747
mv kelf/ $TARGET
4848
7z a -t7z PS2BBL_KELF.7z "$TARGET/*"
49+
if [ "$TARGET" = "/" ] || [ "$TARGET" = "." ]; then
50+
echo "Refusing to remove unsafe TARGET: '$TARGET'" >&2
51+
exit 1
52+
fi
4953
rm -rf -- "$TARGET/"
5054
echo "done!"

src/OSDHistory.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ static int HasTooManyHistoryRecords;
4242
static struct HistoryEntry OldHistoryEntry;
4343
struct HistoryEntry HistoryEntries[MAX_HISTORY_ENTRIES];
4444

45+
static int read_full(int fd, void *buf, size_t nbytes)
46+
{
47+
unsigned char *p = buf;
48+
size_t got = 0;
49+
50+
while (got < nbytes) {
51+
ssize_t r = read(fd, p + got, nbytes - got);
52+
if (r <= 0)
53+
return -1;
54+
got += (size_t)r;
55+
}
56+
57+
return 0;
58+
}
59+
4560
#ifdef F_WriteHistoryFile
4661
static int WriteHistoryFile(int port, const char *path, const void *buffer, int len, int append)
4762
{
@@ -130,14 +145,7 @@ int LoadHistoryFile(int port)
130145
result = 0;
131146
if (fd >= 0) {
132147
size_t expected = MAX_HISTORY_ENTRIES * sizeof(struct HistoryEntry);
133-
size_t total = 0;
134-
while (total < expected) {
135-
ssize_t chunk = read(fd, (u8 *)HistoryEntries + total, expected - total);
136-
if (chunk <= 0)
137-
break;
138-
total += (size_t)chunk;
139-
}
140-
if (total != expected)
148+
if (read_full(fd, HistoryEntries, expected) < 0)
141149
result = -EIO;
142150

143151
close(fd);

src/OSDInit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,10 @@ int OSDInitROMVER(void)
562562
memset(ConsoleROMVER, 0, ROMVER_MAX_LEN);
563563
if ((fd = open("rom0:ROMVER", O_RDONLY)) >= 0) {
564564
ssize_t len = read(fd, ConsoleROMVER, ROMVER_MAX_LEN - 1);
565-
if (len > 0 && len < ROMVER_MAX_LEN)
566-
ConsoleROMVER[len] = '\0';
567-
else
565+
if (len < 0)
568566
ConsoleROMVER[0] = '\0';
567+
else
568+
ConsoleROMVER[len] = '\0';
569569
close(fd);
570570
}
571571

src/dvdplayer.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ int DVDPlayerInit(void)
308308
return 0;
309309
}
310310

311-
int bytes = read(fd, id, sizeof(id) - 1);
312-
if (bytes > 0 && bytes < (int)sizeof(id))
313-
id[bytes] = '\0';
314-
else
311+
ssize_t bytes = read(fd, id, sizeof(id) - 1);
312+
if (bytes < 0)
315313
id[0] = '\0';
314+
else
315+
id[bytes] = '\0';
316316
close(fd);
317317

318318
ROMDVDPlayer.major = atoi(id);
@@ -340,11 +340,11 @@ int DVDPlayerInit(void)
340340
fd = open("rom1:DVDVER", O_RDONLY);
341341

342342
if (fd >= 0) {
343-
result = read(fd, ROMDVDPlayer.ver, sizeof(ROMDVDPlayer.ver) - 1);
344-
if (result > 0 && result < (int)sizeof(ROMDVDPlayer.ver))
345-
ROMDVDPlayer.ver[result] = '\0';
346-
else
343+
ssize_t version_len = read(fd, ROMDVDPlayer.ver, sizeof(ROMDVDPlayer.ver) - 1);
344+
if (version_len < 0)
347345
ROMDVDPlayer.ver[0] = '\0';
346+
else
347+
ROMDVDPlayer.ver[version_len] = '\0';
348348
close(fd);
349349
}
350350
}

src/main.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,10 @@ int main(int argc, char *argv[])
519519

520520
if ((fd = open("rom0:ROMVER", O_RDONLY)) >= 0) {
521521
ssize_t len = read(fd, ROMVER, sizeof(ROMVER) - 1);
522-
if (len > 0 && len < (ssize_t)sizeof(ROMVER))
523-
ROMVER[len] = '\0';
524-
else
522+
if (len < 0)
525523
ROMVER[0] = '\0';
524+
else
525+
ROMVER[len] = '\0';
526526
close(fd);
527527
}
528528
j = SifLoadModule("rom0:ADDDRV", 0, NULL); // Load ADDDRV. The OSD has it listed in rom0:OSDCNF/IOPBTCONF, but it is otherwise not loaded automatically.
@@ -1028,22 +1028,13 @@ static int __attribute__((unused)) LocateExternalIRXPath(const char *filename, c
10281028

10291029
for (i = 0; i < (sizeof(search_templates) / sizeof(search_templates[0])); i++) {
10301030
const char *tmpl = search_templates[i];
1031-
const char *placeholder = strstr(tmpl, "%s");
1032-
if (placeholder == NULL)
1031+
if (strstr(tmpl, "%s") == NULL)
10331032
continue;
10341033

1035-
size_t prefix_len = (size_t)(placeholder - tmpl);
1036-
size_t suffix_len = strlen(placeholder + 2);
1037-
size_t filename_len = strlen(filename);
1038-
size_t required = prefix_len + filename_len + suffix_len + 1;
1039-
if (required > resolved_size)
1034+
int ret = util_snprintf(resolved_path, resolved_size, tmpl, filename);
1035+
if (ret < 0 || (size_t)ret >= resolved_size)
10401036
continue;
10411037

1042-
memcpy(resolved_path, tmpl, prefix_len);
1043-
memcpy(resolved_path + prefix_len, filename, filename_len);
1044-
memcpy(resolved_path + prefix_len + filename_len, placeholder + 2, suffix_len);
1045-
resolved_path[required - 1] = '\0';
1046-
10471038
const char *checked_path = CheckPath(resolved_path);
10481039
if (checked_path != NULL && exist(checked_path)) {
10491040
return 0;

0 commit comments

Comments
 (0)