|
From: falcovorbis <fal...@us...> - 2024-06-03 02:34:03
|
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "A pseudo Operating System for the Dreamcast.".
The branch, master has been updated
via 7f261a396099cfd4cbbd771108bf670914568ed6 (commit)
from cfcd40fd57bab6de70c181e48b0eea1db4b263a6 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 7f261a396099cfd4cbbd771108bf670914568ed6
Author: Donald Haase <qu...@ya...>
Date: Sun Jun 2 22:33:50 2024 -0400
Better error/warning handling in some examples (#589)
* Correcting possible null derefs and similar from fanalyzer
* Notify gcc that we are going to be doing the bad things and not worry
* Prevent fanalyzer warning for intentional null deref
* Idiomatic typing
* Prevent null deref
* Prevent null deref
* Static allocate the temp beffer, it was being leaked anyways
* Rollback pointless check
* Resolve conflict due to other changes
* Properly free memory and close stuff
* Make sure to free used allocations
* Protect string printing from buffer overflow and expand the buffer to fit the longer ns text.
* Apply suggestions from code review
Co-authored-by: Falco Girgis <gyr...@gm...>
---------
Co-authored-by: QuzarDC <qu...@co...>
Co-authored-by: Falco Girgis <gyr...@gm...>
-----------------------------------------------------------------------
Summary of changes:
examples/dreamcast/2ndmix/2ndmix.c | 11 +++++++++++
examples/dreamcast/basic/mmu/nullptr/nullptr.c | 12 +++++++++---
examples/dreamcast/basic/mmu/pvrmap/pvrmap.c | 17 +++++++++++++----
.../dreamcast/basic/stackprotector/stackprotector.c | 8 ++++++++
examples/dreamcast/dev/devroot/devroot.c | 4 +++-
examples/dreamcast/network/httpd/httpd.c | 16 ++++++++++++++--
examples/dreamcast/parallax/serpent_dma/perfmeter.c | 2 +-
examples/dreamcast/parallax/serpent_dma/serpent.c | 7 ++++---
examples/dreamcast/png/example.c | 9 +++++----
examples/dreamcast/rumble/rumble.c | 4 ++++
examples/dreamcast/sound/multi-stream/main.c | 3 ++-
examples/dreamcast/sound/sfx/main.c | 3 ++-
examples/dreamcast/vmu/vmu_beep/beep.c | 13 ++++++++++---
13 files changed, 86 insertions(+), 23 deletions(-)
diff --git a/examples/dreamcast/2ndmix/2ndmix.c b/examples/dreamcast/2ndmix/2ndmix.c
index 3c6470c6..4a4089a8 100644
--- a/examples/dreamcast/2ndmix/2ndmix.c
+++ b/examples/dreamcast/2ndmix/2ndmix.c
@@ -398,6 +398,11 @@ int load_pcx(char *pcxdata) {
int num_bytes;
struct pcx_hdr pcxh;
+ if(image == NULL) {
+ printf("Image not allocated\r\n");
+ return 0;
+ }
+
bytes = 0;
memcpy(&pcxh, pcxdata, sizeof(pcxh));
@@ -857,6 +862,12 @@ int main() {
draw_one_frame();
}
+ /* Clean up what we allocated */
+ free(charmap);
+ free(star_x);
+ free(star_y);
+ free(star_z);
+
printf("Done, returning\n");
return 0;
}
diff --git a/examples/dreamcast/basic/mmu/nullptr/nullptr.c b/examples/dreamcast/basic/mmu/nullptr/nullptr.c
index fbb0cf44..861570fd 100644
--- a/examples/dreamcast/basic/mmu/nullptr/nullptr.c
+++ b/examples/dreamcast/basic/mmu/nullptr/nullptr.c
@@ -29,10 +29,18 @@ int main(int argc, char **argv) {
/* Set our handler */
mmu_map_set_callback(catchnull);
+/* Make sure the compiler doesn't complain about the bad thing
+we are doing intentionally */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
+
/* Do something naughty; note that this will actually work
for both reads and writes in this case, but we can only
really effectively test one ;) */
- *((uint16*)NULL) = 0;
+ *((uint16_t *)NULL) = 0;
+
+/* Turn the warning back on */
+#pragma GCC diagnostic pop
/* We shouldn't get here... */
@@ -41,5 +49,3 @@ int main(int argc, char **argv) {
return 0;
}
-
-
diff --git a/examples/dreamcast/basic/mmu/pvrmap/pvrmap.c b/examples/dreamcast/basic/mmu/pvrmap/pvrmap.c
index f24ff5c8..656b0196 100644
--- a/examples/dreamcast/basic/mmu/pvrmap/pvrmap.c
+++ b/examples/dreamcast/basic/mmu/pvrmap/pvrmap.c
@@ -15,8 +15,9 @@ KOS_INIT_FLAGS(INIT_DEFAULT | INIT_MALLOCSTATS);
int main(int argc, char **argv) {
mmucontext_t * cxt;
- uint16 * vr;
- int x, y, done = 0;
+ uint16_t * vr;
+ bool done = false;
+ uint16_t x, y = 0;
/* Initialize MMU support */
mmu_init();
@@ -33,9 +34,14 @@ int main(int argc, char **argv) {
/* Draw a nice pattern to the NULL space */
vr = NULL;
+/* Make sure the compiler doesn't complain about the bad thing
+we are doing intentionally */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
+
for(y = 0; y < 480; y++) {
for(x = 0; x < 640; x++) {
- int v = ((x * x + y * y) & 255);
+ uint8_t v = ((x * x + y * y) & 0xff);
if(v >= 128)
v = 127 - (v - 128);
@@ -46,6 +52,9 @@ int main(int argc, char **argv) {
}
}
+/* Turn the warning back on */
+#pragma GCC diagnostic pop
+
/* Draw some text */
bfont_draw_str(vr + 20 * 640 + 20, 640, 0, "Press START!");
@@ -54,7 +63,7 @@ int main(int argc, char **argv) {
MAPLE_FOREACH_BEGIN(MAPLE_FUNC_CONTROLLER, cont_state_t, st)
if(st->buttons & CONT_START)
- done = 1;
+ done = true;
MAPLE_FOREACH_END()
}
diff --git a/examples/dreamcast/basic/stackprotector/stackprotector.c b/examples/dreamcast/basic/stackprotector/stackprotector.c
index ca89b594..235840fc 100644
--- a/examples/dreamcast/basic/stackprotector/stackprotector.c
+++ b/examples/dreamcast/basic/stackprotector/stackprotector.c
@@ -49,11 +49,19 @@ __used void __stack_chk_fail(void) {
exit(EXIT_SUCCESS);
}
+/* Make sure the compiler doesn't complain about the bad thing
+we are doing intentionally */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wanalyzer-out-of-bounds"
+
__noinline void badfunc(void) {
char buffer[8];
strcpy(buffer, "This string is entirely too long and will overflow.");
}
+/* Turn the warning back on */
+#pragma GCC diagnostic pop
+
int main(int argc, char **argv) {
printf("Stack protector test....\n");
printf("About to call badfunc()...\n");
diff --git a/examples/dreamcast/dev/devroot/devroot.c b/examples/dreamcast/dev/devroot/devroot.c
index 8e4ea5e1..34b00439 100644
--- a/examples/dreamcast/dev/devroot/devroot.c
+++ b/examples/dreamcast/dev/devroot/devroot.c
@@ -2,7 +2,7 @@
devroot.c
Copyright (C) 2024 Donald Haase
-
+
This example demonstrates the expected behavior of reading from the
/ and /dev dirs, as well as attempting to open a non-existant device
for the same.
@@ -43,6 +43,8 @@ void printdir(char* fn) {
}
else
printf("FAIL: Counted %i entries the first time and %i the second.\n", cnt1, cnt2);
+
+ closedir(d);
}
}
diff --git a/examples/dreamcast/network/httpd/httpd.c b/examples/dreamcast/network/httpd/httpd.c
index 6d848afb..311bb984 100644
--- a/examples/dreamcast/network/httpd/httpd.c
+++ b/examples/dreamcast/network/httpd/httpd.c
@@ -104,6 +104,9 @@ int read_headers(http_state_t * hs, char * buffer, int bufsize) {
char fn[256];
int i, j;
+ if(buffer == NULL)
+ return -2;
+
for(i = 0; ; i++) {
if(readline(hs->socket, buffer, bufsize) < 0) {
if(i > 0)
@@ -143,6 +146,9 @@ static const char * errmsg3 = "</h4>\n<hr>\nKOSHttp/1.0 server\n</body></html>";
int send_error(http_state_t * hs, int errcode, const char * str) {
char * buffer = malloc(65536);
+ if(buffer == NULL)
+ return -1;
+
sprintf(buffer, "HTTP/1.0 %d %s\r\nContent-type: text/html\r\n\r\n", errcode, str);
write(hs->socket, buffer, strlen(buffer));
@@ -181,6 +187,9 @@ int do_dirlist(const char * name, http_state_t * hs, file_t f) {
dl = malloc(65536);
dlout = dl;
+
+ if(dl == NULL)
+ return -1;
sprintf(dlout, "<html><head><title>Listing of %s</title></head></html>\n<body bgcolor=\"white\">\n", name);
dlout += strlen(dlout);
@@ -210,8 +219,10 @@ int do_dirlist(const char * name, http_state_t * hs, file_t f) {
while(dlsize > 0) {
r = write(hs->socket, dlout, dlsize);
- if(r <= 0)
+ if(r <= 0) {
+ free(dl);
return -1;
+ }
dlsize -= r;
dlout += r;
@@ -253,7 +264,8 @@ void *client_thread(void *p) {
f = fs_open(buf, O_RDONLY);
if(f < 0) {
- send_error(hs, 404, "File not found or unreadable");
+ if(send_error(hs, 404, "File not found or unreadable") < 0)
+ printf("Error sending 404\n");
goto out;
}
diff --git a/examples/dreamcast/parallax/serpent_dma/perfmeter.c b/examples/dreamcast/parallax/serpent_dma/perfmeter.c
index 8eeca655..8934e7e7 100644
--- a/examples/dreamcast/parallax/serpent_dma/perfmeter.c
+++ b/examples/dreamcast/parallax/serpent_dma/perfmeter.c
@@ -35,7 +35,7 @@ void pm_drawbar(float pct, float posx, float posy, float posz,
}
void pm_draw(void) {
- char str[64];
+ char str[128];
float pct;
float posx = 64, posy = 96, posz = 4500;
diff --git a/examples/dreamcast/parallax/serpent_dma/serpent.c b/examples/dreamcast/parallax/serpent_dma/serpent.c
index 4d3fa65a..8f5cdcfe 100644
--- a/examples/dreamcast/parallax/serpent_dma/serpent.c
+++ b/examples/dreamcast/parallax/serpent_dma/serpent.c
@@ -51,9 +51,10 @@ static void sphere(sphere_t *s) { /* {{{ */
float yaw;
pvr_vertex_t *v;
- v = (pvr_vertex_t *)malloc(s->stacks * (s->slices + 2) * sizeof(pvr_vertex_t) + 32);
- v = (pvr_vertex_t *)(((uint32)v & ~31) + 32); /* align to 32 bytes */
- s->data = v;
+ s->data = (pvr_vertex_t *)memalign(32, s->stacks * (s->slices + 2) * sizeof(pvr_vertex_t));
+ if(s->data == NULL) return;
+
+ v = s->data;
// s->data_trans = (pvr_vertex_t *)malloc(s->stacks * (s->slices+2) * sizeof(pvr_vertex_t));
/* transformed data -- for testing mat_transform */
printf("allocated %d bytes for %d stacks, %d + 2 slices, and %d-byte pvr_vertex_t\n",
diff --git a/examples/dreamcast/png/example.c b/examples/dreamcast/png/example.c
index 130ba422..e9115610 100644
--- a/examples/dreamcast/png/example.c
+++ b/examples/dreamcast/png/example.c
@@ -30,10 +30,9 @@ void back_init(void) {
/* init font */
void font_init(void) {
int i, x, y, c;
- unsigned short * temp_tex;
+ unsigned short temp_tex[256 * 128 * 2];
font_tex = pvr_mem_malloc(256 * 256 * 2);
- temp_tex = (unsigned short *)malloc(256 * 128 * 2);
c = 0;
@@ -53,14 +52,14 @@ void font_init(void) {
c += 16;
}
- pvr_txr_load_ex(temp_tex, font_tex, 256, 256, PVR_TXRLOAD_16BPP);
+ pvr_txr_load_ex(&temp_tex, font_tex, 256, 256, PVR_TXRLOAD_16BPP);
}
void text_init(void) {
int length = zlib_getlength("/rd/text.gz");
gzFile f;
- data = (char *)malloc(length + 1); // I am not currently freeing it
+ data = (char *)malloc(length + 1);
f = gzopen("/rd/text.gz", "r");
gzread(f, data, length);
@@ -234,6 +233,8 @@ int main(int argc, char **argv) {
draw_frame();
}
+ free(data);
+
return 0;
}
diff --git a/examples/dreamcast/rumble/rumble.c b/examples/dreamcast/rumble/rumble.c
index 391e8836..74d6aa32 100644
--- a/examples/dreamcast/rumble/rumble.c
+++ b/examples/dreamcast/rumble/rumble.c
@@ -195,5 +195,9 @@ int main(int argc, char *argv[]) {
/* Stop rumbling before exiting, if it still exists. */
if((purudev != NULL) && (purudev->valid != 0))
purupuru_rumble_raw(purudev, 0x00000000);
+
+ plx_font_destroy(fnt);
+ plx_fcxt_destroy(cxt);
+
return 0;
}
diff --git a/examples/dreamcast/sound/multi-stream/main.c b/examples/dreamcast/sound/multi-stream/main.c
index 2a99c086..474c725f 100644
--- a/examples/dreamcast/sound/multi-stream/main.c
+++ b/examples/dreamcast/sound/multi-stream/main.c
@@ -42,7 +42,8 @@ int main(int argc, char **argv) {
for(;;) {
- cond = get_cont_state();
+ if(!(cond = get_cont_state()))
+ continue;
current_buttons = cond->buttons;
changed_buttons = current_buttons ^ previous_buttons;
previous_buttons = current_buttons;
diff --git a/examples/dreamcast/sound/sfx/main.c b/examples/dreamcast/sound/sfx/main.c
index 043566ee..7a4cf1a8 100644
--- a/examples/dreamcast/sound/sfx/main.c
+++ b/examples/dreamcast/sound/sfx/main.c
@@ -48,7 +48,8 @@ int main(int argc, char **argv) {
uint32_t previous_buttons = 0;
for(;;) {
- cond = get_cont_state();
+ if(!(cond = get_cont_state()))
+ continue;
current_buttons = cond->buttons;
changed_buttons = current_buttons ^ previous_buttons;
previous_buttons = current_buttons;
diff --git a/examples/dreamcast/vmu/vmu_beep/beep.c b/examples/dreamcast/vmu/vmu_beep/beep.c
index 79f50eee..ea600717 100644
--- a/examples/dreamcast/vmu/vmu_beep/beep.c
+++ b/examples/dreamcast/vmu/vmu_beep/beep.c
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdint.h>
+#include <stdlib.h>
#include <kos/init.h>
@@ -37,13 +38,18 @@
#include <plx/font.h>
-KOS_INIT_FLAGS(INIT_DEFAULT);
+
+static plx_font_t *fnt;
+static plx_fcxt_t *cxt;
+
+static void cleanup(void) {
+ plx_font_destroy(fnt);
+ plx_fcxt_destroy(cxt);
+}
int main(int argc, char *argv[]) {
maple_device_t *dev, *vmudev;
cont_state_t *state;
- plx_font_t *fnt;
- plx_fcxt_t *cxt;
point_t w;
int i = 0, count = 0;
uint16_t old_buttons = 0, rel_buttons = 0;
@@ -51,6 +57,7 @@ int main(int argc, char *argv[]) {
uint8_t n[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; //nibbles
char s[8][2] = { "", "", "", "", "", "", "", "" };
+ atexit(cleanup);
/* If the face buttons are all pressed, exit the app */
cont_btn_callback(0, CONT_START | CONT_A | CONT_B | CONT_X | CONT_Y,
(cont_btn_callback_t)arch_exit);
hooks/post-receive
--
A pseudo Operating System for the Dreamcast.
|