From 316de2e60c60455710f2937d379236372e911a36 Mon Sep 17 00:00:00 2001 From: cancel Date: Mon, 26 Nov 2018 04:51:20 +0900 Subject: [PATCH] Add narrowing conversion warning, fix warnings --- Makefile | 2 +- base.h | 2 ++ cli_main.c | 3 +++ field.c | 14 +++++++++++--- field.h | 5 +++-- sim.c | 6 +++--- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index a0f0f0c..f7c0c5f 100644 --- 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 diff --git a/base.h b/base.h index 7289043..fb3ab69 100644 --- a/base.h +++ b/base.h @@ -9,6 +9,8 @@ #include typedef char Term; +typedef uint16_t U16; +typedef int16_t I16; typedef uint32_t U32; typedef int32_t I32; typedef uint64_t U64; diff --git a/cli_main.c b/cli_main.c index b898602..1b26072 100644 --- a/cli_main.c +++ b/cli_main.c @@ -80,6 +80,9 @@ int main(int argc, char** argv) { 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; diff --git a/field.c b/field.c index 4c2afeb..bc8fdf1 100644 --- a/field.c +++ b/field.c @@ -172,12 +172,16 @@ Field_load_error field_load_file(char const* filepath, Field* field) { } 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 @@ Field_load_error field_load_file(char const* filepath, Field* field) { } 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 @@ Field_load_error field_load_file(char const* filepath, Field* field) { 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]; diff --git a/field.h b/field.h index 0944e28..89c8da4 100644 --- a/field.h +++ b/field.h @@ -21,8 +21,9 @@ typedef enum { 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); diff --git a/sim.c b/sim.c index fea51e0..7f92d35 100644 --- a/sim.c +++ b/sim.c @@ -18,7 +18,7 @@ static inline size_t index_of_term(Term c) { } 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 @@ void orca_run(Field* f) { 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; } }