shithub: m8c

Download patch

ref: a002bab6efaa60464f70f33b991eca2a7e1dc49c
parent: 3ff89dc09dc6dceadfb231e756188125ec67afcb
author: Bloop Click <103341015+bloopclick@users.noreply.github.com>
date: Mon May 23 19:45:55 EDT 2022

Bug fixes

- Fix buffer overflow in INI write
- Add assert to guard similar errors
- Fix bug that failed to write last line of INI file
- Fix segfault on reading empty INI file

--- a/config.c
+++ b/config.c
@@ -4,6 +4,7 @@
 #include "config.h"
 #include "ini.h"
 #include <SDL.h>
+#include <assert.h>
 
 /* Case insensitive string compare from ini.h library */
 static int strcmpci(const char *a, const char *b) {
@@ -71,8 +72,10 @@
 
   SDL_Log("Writing config file to %s", config_path);
 
+  const unsigned int INI_LINE_COUNT = 36;
+
   // Entries for the config file
-  char ini_values[35][50];
+  char ini_values[INI_LINE_COUNT][50];
   int initPointer = 0;
   sprintf(ini_values[initPointer++], "[graphics]\n");
   sprintf(ini_values[initPointer++], "fullscreen=%s\n",
@@ -121,9 +124,12 @@
   sprintf(ini_values[initPointer++], "gamepad_analog_axis_edit=%d\n",
           conf->gamepad_analog_axis_edit);
 
+  // Ensure we aren't writing off the end of the array
+  assert(initPointer == INI_LINE_COUNT);
+
   if (rw != NULL) {
     // Write ini_values array to config file
-    for (int i = 0; i < 34; i++) {
+    for (int i = 0; i < INI_LINE_COUNT; i++) {
       size_t len = SDL_strlen(ini_values[i]);
       if (SDL_RWwrite(rw, ini_values[i], 1, len) != len) {
         SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM,
--- a/ini.c
+++ b/ini.c
@@ -194,6 +194,9 @@
   /* Get file size */
   fseek(fp, 0, SEEK_END);
   sz = ftell(fp);
+  if (sz==0) {
+    goto fail;
+  }
   rewind(fp);
 
   /* Load file content into memory, null terminate, init end var */
--- a/main.c
+++ b/main.c
@@ -35,8 +35,6 @@
   // TODO: take cli parameter to override default configfile location
   read_config(&conf);
 
-  SDL_Log("idle_ms=%d", conf.idle_ms);
-
   // allocate memory for serial buffer
   uint8_t *serial_buf = malloc(serial_read_size);
 
@@ -117,7 +115,6 @@
 
     while (1) {
       // read serial port
-      //int bytes_read = sp_blocking_read(port, serial_buf, serial_read_size, 1);
       int bytes_read = sp_nonblocking_read(port, serial_buf, serial_read_size);
       if (bytes_read < 0) {
         SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n",
--