shithub: orca

Download patch

ref: 316de2e60c60455710f2937d379236372e911a36
parent: 6b2bc499af6e8f8fbc5ea12aec15561dcbd46446
author: cancel <cancel@cancel.fm>
date: Sun Nov 25 23:51:20 EST 2018

Add narrowing conversion warning, fix warnings

--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-basic_flags := -std=c99 -pipe -Wall -Wpedantic -Wextra -Werror=implicit-function-declaration -D_XOPEN_SOURCE_EXTENDED=1
+basic_flags := -std=c99 -pipe -Wall -Wpedantic -Wextra -Werror=implicit-function-declaration -Wconversion -D_XOPEN_SOURCE_EXTENDED=1
 debug_flags := -DDEBUG -ggdb
 sanitize_flags := -fsanitize=address -fsanitize=undefined
 # note: -fsanitize=leak not available on at least Mac 10.12
--- a/base.h
+++ b/base.h
@@ -9,6 +9,8 @@
 #include <unistd.h>
 
 typedef char Term;
+typedef uint16_t U16;
+typedef int16_t I16;
 typedef uint32_t U32;
 typedef int32_t I32;
 typedef uint64_t U64;
--- a/cli_main.c
+++ b/cli_main.c
@@ -80,6 +80,9 @@
     case Field_load_error_too_many_columns:
       errstr = "Grid file has too many columns";
       break;
+    case Field_load_error_too_many_rows:
+      errstr = "Grid file has too many rows";
+      break;
     case Field_load_error_no_rows_read:
       errstr = "Grid file has no rows";
       break;
--- a/field.c
+++ b/field.c
@@ -172,12 +172,16 @@
   }
   enum { Bufsize = 4096 };
   char buf[Bufsize];
-  U32 first_row_columns = 0;
-  U32 rows = 0;
+  size_t first_row_columns = 0;
+  size_t rows = 0;
   for (;;) {
     char* s = fgets(buf, Bufsize, file);
     if (s == NULL)
       break;
+    if (rows == UINT16_MAX) {
+      fclose(file);
+      return Field_load_error_too_many_rows;
+    }
     size_t len = strlen(buf);
     if (len == Bufsize - 1 && buf[len - 1] != '\n' && !feof(file)) {
       fclose(file);
@@ -192,6 +196,10 @@
     }
     if (len == 0)
       continue;
+    if (len > UINT16_MAX) {
+      fclose(file);
+      return Field_load_error_too_many_columns;
+    }
     // quick hack until we use a proper scanner
     if (rows == 0) {
       first_row_columns = len;
@@ -199,7 +207,7 @@
       fclose(file);
       return Field_load_error_not_a_rectangle;
     }
-    field_resize_raw(field, rows + 1, first_row_columns);
+    field_resize_raw(field, (U32)(rows + 1), (U32)first_row_columns);
     Term* rowbuff = field->buffer + first_row_columns * rows;
     for (size_t i = 0; i < len; ++i) {
       char c = buf[i];
--- a/field.h
+++ b/field.h
@@ -21,8 +21,9 @@
   Field_load_error_ok = 0,
   Field_load_error_cant_open_file = 1,
   Field_load_error_too_many_columns = 2,
-  Field_load_error_no_rows_read = 3,
-  Field_load_error_not_a_rectangle = 4,
+  Field_load_error_too_many_rows = 3,
+  Field_load_error_no_rows_read = 4,
+  Field_load_error_not_a_rectangle = 5,
 } Field_load_error;
 
 Field_load_error field_load_file(char const* filepath, Field* field);
--- a/sim.c
+++ b/sim.c
@@ -18,7 +18,7 @@
 }
 
 static inline Term term_lowered(Term c) {
-  return (c >= 'A' && c <= 'Z') ? c - ('a' - 'A') : c;
+  return (c >= 'A' && c <= 'Z') ? (char)(c - ('a' - 'A')) : c;
 }
 
 // Always returns 0 through (sizeof indexed_terms) - 1, and works on
@@ -73,10 +73,10 @@
       Term c = row[ix];
       switch (c) {
       case 'a':
-        act_a(f, iy, ix);
+        act_a(f, (U32)iy, (U32)ix);
         break;
       case 'm':
-        act_m(f, iy, ix);
+        act_m(f, (U32)iy, (U32)ix);
         break;
       }
     }