Browse Source

Add narrowing conversion warning, fix warnings

master
cancel 6 years ago
parent
commit
316de2e60c
  1. 2
      Makefile
  2. 2
      base.h
  3. 3
      cli_main.c
  4. 14
      field.c
  5. 5
      field.h
  6. 6
      sim.c

2
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 debug_flags := -DDEBUG -ggdb
sanitize_flags := -fsanitize=address -fsanitize=undefined sanitize_flags := -fsanitize=address -fsanitize=undefined
# note: -fsanitize=leak not available on at least Mac 10.12 # note: -fsanitize=leak not available on at least Mac 10.12

2
base.h

@ -9,6 +9,8 @@
#include <unistd.h> #include <unistd.h>
typedef char Term; typedef char Term;
typedef uint16_t U16;
typedef int16_t I16;
typedef uint32_t U32; typedef uint32_t U32;
typedef int32_t I32; typedef int32_t I32;
typedef uint64_t U64; typedef uint64_t U64;

3
cli_main.c

@ -80,6 +80,9 @@ int main(int argc, char** argv) {
case Field_load_error_too_many_columns: case Field_load_error_too_many_columns:
errstr = "Grid file has too many columns"; errstr = "Grid file has too many columns";
break; break;
case Field_load_error_too_many_rows:
errstr = "Grid file has too many rows";
break;
case Field_load_error_no_rows_read: case Field_load_error_no_rows_read:
errstr = "Grid file has no rows"; errstr = "Grid file has no rows";
break; break;

14
field.c

@ -172,12 +172,16 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
} }
enum { Bufsize = 4096 }; enum { Bufsize = 4096 };
char buf[Bufsize]; char buf[Bufsize];
U32 first_row_columns = 0; size_t first_row_columns = 0;
U32 rows = 0; size_t rows = 0;
for (;;) { for (;;) {
char* s = fgets(buf, Bufsize, file); char* s = fgets(buf, Bufsize, file);
if (s == NULL) if (s == NULL)
break; break;
if (rows == UINT16_MAX) {
fclose(file);
return Field_load_error_too_many_rows;
}
size_t len = strlen(buf); size_t len = strlen(buf);
if (len == Bufsize - 1 && buf[len - 1] != '\n' && !feof(file)) { if (len == Bufsize - 1 && buf[len - 1] != '\n' && !feof(file)) {
fclose(file); fclose(file);
@ -192,6 +196,10 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
} }
if (len == 0) if (len == 0)
continue; continue;
if (len > UINT16_MAX) {
fclose(file);
return Field_load_error_too_many_columns;
}
// quick hack until we use a proper scanner // quick hack until we use a proper scanner
if (rows == 0) { if (rows == 0) {
first_row_columns = len; first_row_columns = len;
@ -199,7 +207,7 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
fclose(file); fclose(file);
return Field_load_error_not_a_rectangle; 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; Term* rowbuff = field->buffer + first_row_columns * rows;
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
char c = buf[i]; char c = buf[i];

5
field.h

@ -21,8 +21,9 @@ typedef enum {
Field_load_error_ok = 0, Field_load_error_ok = 0,
Field_load_error_cant_open_file = 1, Field_load_error_cant_open_file = 1,
Field_load_error_too_many_columns = 2, Field_load_error_too_many_columns = 2,
Field_load_error_no_rows_read = 3, Field_load_error_too_many_rows = 3,
Field_load_error_not_a_rectangle = 4, Field_load_error_no_rows_read = 4,
Field_load_error_not_a_rectangle = 5,
} Field_load_error; } Field_load_error;
Field_load_error field_load_file(char const* filepath, Field* field); Field_load_error field_load_file(char const* filepath, Field* field);

6
sim.c

@ -18,7 +18,7 @@ static inline size_t index_of_term(Term c) {
} }
static inline Term term_lowered(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 // Always returns 0 through (sizeof indexed_terms) - 1, and works on
@ -73,10 +73,10 @@ void orca_run(Field* f) {
Term c = row[ix]; Term c = row[ix];
switch (c) { switch (c) {
case 'a': case 'a':
act_a(f, iy, ix); act_a(f, (U32)iy, (U32)ix);
break; break;
case 'm': case 'm':
act_m(f, iy, ix); act_m(f, (U32)iy, (U32)ix);
break; break;
} }
} }

Loading…
Cancel
Save