Skip to content

Commit

Permalink
Fix incorrect usage of EOF and opts dependency on JSON5 module's methods
Browse files Browse the repository at this point in the history
Based on: https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_71.html, we've incorrectly made use of EOF in our code. Opts module depended on zpl_json_skip, which was a specialised version of zpl_str_skip, such method is now available as zpl_str_control_skip without being dependant on the JSON5 module itself.
  • Loading branch information
zpl-zak committed Oct 17, 2019
1 parent 082d7d9 commit f577ded
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 42 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: continuous-integration
on:
push:
# paths:
# - 'Code/*'
# - 'Vendors/*'

jobs:
build-win:
name: Build Windows
runs-on: windows-latest
steps:
- uses: actions/checkout@v1

- name: Run build on windows
run: razzle amd64 && build_tests.sh.bat

build-lin:
name: Build Linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Run build on Linux
run: build_tests.sh.bat
46 changes: 25 additions & 21 deletions code/zpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,7 @@ ZPL_DEF void zpl_str_to_upper(char *str);

ZPL_DEF char *zpl_str_trim(char *str, zpl_b32 skip_newline);
ZPL_DEF char *zpl_str_skip(char *str, char c);
ZPL_DEF char *zpl_str_control_skip(char *str, char c);

ZPL_DEF zpl_isize zpl_strlen(const char *str);
ZPL_DEF zpl_isize zpl_strnlen(const char *str, zpl_isize max_len);
Expand All @@ -1688,7 +1689,7 @@ ZPL_DEF const char *zpl_strtok(char *output, const char *src, const char *delimi

// NOTE: This edits *source* string.
// Returns: zpl_array
ZPL_DEF char **zpl_str_split_lines(zpl_allocator alloc, char *source, zpl_b32 strip_whitespace);
ZPL_DEF char **zpl_str_split_lines(zpl_allocator alloc, char *source, zpl_b32 strip_whitespace);

#define zpl_str_expand(str) str, zpl_strlen(str)

Expand Down Expand Up @@ -3338,9 +3339,6 @@ ZPL_DEF char *zpl__json_parse_object(zpl_json_object *obj, char *base, zpl_alloc
ZPL_DEF char *zpl__json_parse_value(zpl_json_object *obj, char *base, zpl_allocator a, zpl_u8 *err_code);
ZPL_DEF char *zpl__json_parse_array(zpl_json_object *obj, char *base, zpl_allocator a, zpl_u8 *err_code);

#define zpl__trim zpl_str_trim
#define zpl__skip zpl__json_skip
ZPL_DEF char *zpl__json_skip(char *str, char c);
ZPL_DEF zpl_b32 zpl__json_validate_name(char *str, char *err);

//! @}
Expand Down Expand Up @@ -7172,7 +7170,7 @@ void zpl_affinity_init(zpl_affinity *a) {
for (;;) {
// The 'temporary char'. Everything goes into this char,
// so that we can check against EOF at the end of this loop.
char c;
int c;

#define AF__CHECK(letter) ((c = getc(cpu_info)) == letter)
if (AF__CHECK('c') && AF__CHECK('p') && AF__CHECK('u') && AF__CHECK(' ') &&
Expand Down Expand Up @@ -7682,6 +7680,22 @@ zpl_inline char **zpl_str_split_lines(zpl_allocator alloc, char *source, zpl_b32
return lines;
}

zpl_inline zpl_b32 zpl__is_control_char(char c) {
return !!zpl_strchr("\"\\/bfnrt", c);
}

zpl_inline zpl_b32 zpl__is_special_char(char c) { return !!zpl_strchr("<>:/", c); }
zpl_inline zpl_b32 zpl__is_assign_char(char c) { return !!zpl_strchr(":=|", c); }
zpl_inline zpl_b32 zpl__is_delim_char(char c) { return !!zpl_strchr(",|\n", c); }


zpl_inline char *zpl_str_control_skip(char *str, char c) {
while ((*str && *str != c) || (*(str - 1) == '\\' && *str == c && zpl__is_control_char(c))) { ++str; }

return str;
}


zpl_inline zpl_b32 zpl_str_has_prefix(const char *str, const char *prefix) {
while (*prefix) {
if (*str++ != *prefix++) return false;
Expand Down Expand Up @@ -11676,7 +11690,7 @@ zpl_inline zpl_u32 zpl_system_command(const char *command, zpl_usize buffer_len,

if(!handle) return 0;

char c;
int c;
zpl_usize i=0;
while ((c = getc(handle)) != EOF && i++ < buffer_len) {
*buffer++ = c;
Expand Down Expand Up @@ -11706,7 +11720,7 @@ zpl_inline zpl_string zpl_system_command_str(const char *command, zpl_allocator

zpl_string output = zpl_string_make_reserve(backing, 4);

char c;
int c;
while ((c = getc(handle)) != EOF) {
char ins[2] = {c,0};
output = zpl_string_appendc(output, ins);
Expand Down Expand Up @@ -12247,7 +12261,7 @@ char *zpl__json_parse_object(zpl_json_object *obj, char *base, zpl_allocator a,

char c = *p;
b = ++p;
e = zpl__json_skip(b, c);
e = zpl_str_control_skip(b, c);
node.name = b;
*e = '\0';

Expand Down Expand Up @@ -12463,18 +12477,14 @@ zpl_json_object *zpl_json_add(zpl_json_object *obj, char const *name, zpl_u8 typ
return zpl_json_add_at(obj, zpl_array_count(obj->nodes), name, type);
}

zpl_inline zpl_b32 zpl__json_is_control_char(char c) {
return !!zpl_strchr("\"\\/bfnrt", c);
}

zpl_inline zpl_b32 zpl__json_is_special_char(char c) { return !!zpl_strchr("<>:/", c); }
zpl_inline zpl_b32 zpl__json_is_assign_char(char c) { return !!zpl_strchr(":=|", c); }
zpl_inline zpl_b32 zpl__json_is_delim_char(char c) { return !!zpl_strchr(",|\n", c); }

#define jx(x) !zpl_char_is_hex_digit(str[x])
zpl_inline zpl_b32 zpl__json_validate_name(char *str, char *err) {
while (*str) {
if ((str[0] == '\\' && !zpl__json_is_control_char(str[1])) &&
if ((str[0] == '\\' && !zpl__is_control_char(str[1])) &&
(str[0] == '\\' && jx(1) && jx(2) && jx(3) && jx(4))) {
*err = *str;
return false;
Expand All @@ -12487,12 +12497,6 @@ zpl_inline zpl_b32 zpl__json_validate_name(char *str, char *err) {
}
#undef jx

zpl_inline char *zpl__json_skip(char *str, char c) {
while ((*str && *str != c) || (*(str - 1) == '\\' && *str == c && zpl__json_is_control_char(c))) { ++str; }

return str;
}


////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -12664,7 +12668,7 @@ zpl_b32 zpl_opts_compile(zpl_opts *opts, int argc, char **argv) {
char *p = argv[i];

if (*p) {
p = zpl__trim(p, false);
p = zpl_str_trim(p, false);
if (*p == '-') {
zpl_opts_entry *t = 0;
zpl_b32 checkln = false;
Expand Down Expand Up @@ -12716,7 +12720,7 @@ zpl_b32 zpl_opts_compile(zpl_opts *opts, int argc, char **argv) {
}
}

e = zpl__skip(e, '\0');
e = zpl_str_control_skip(e, '\0');
zpl__opts_set_value(opts, t, b);
} else {
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION);
Expand Down
1 change: 1 addition & 0 deletions code/zpl/__main__.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
https://github.com/zpl-c/zpl
Version History:
9.8.5 - Fix incorrect usage of EOF and opts dependency on JSON5 module's methods
9.8.4 - Fix MSVC ZPL_NO_MATH_H code branch using incorrect methods internally
9.8.3 - Fix MinGW GCC related issue with zpl_printf %lld format
9.8.2 - Fix VS C4190 issue
Expand Down
17 changes: 2 additions & 15 deletions code/zpl/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ ZPL_DEF char *zpl__json_parse_object(zpl_json_object *obj, char *base, zpl_alloc
ZPL_DEF char *zpl__json_parse_value(zpl_json_object *obj, char *base, zpl_allocator a, zpl_u8 *err_code);
ZPL_DEF char *zpl__json_parse_array(zpl_json_object *obj, char *base, zpl_allocator a, zpl_u8 *err_code);

#define zpl__trim zpl_str_trim
#define zpl__skip zpl__json_skip
ZPL_DEF char *zpl__json_skip(char *str, char c);
ZPL_DEF zpl_b32 zpl__json_validate_name(char *str, char *err);

//! @}
Expand Down Expand Up @@ -706,7 +703,7 @@ char *zpl__json_parse_object(zpl_json_object *obj, char *base, zpl_allocator a,

char c = *p;
b = ++p;
e = zpl__json_skip(b, c);
e = zpl_str_control_skip(b, c);
node.name = b;
*e = '\0';

Expand Down Expand Up @@ -922,18 +919,14 @@ zpl_json_object *zpl_json_add(zpl_json_object *obj, char const *name, zpl_u8 typ
return zpl_json_add_at(obj, zpl_array_count(obj->nodes), name, type);
}

zpl_inline zpl_b32 zpl__json_is_control_char(char c) {
return !!zpl_strchr("\"\\/bfnrt", c);
}

zpl_inline zpl_b32 zpl__json_is_special_char(char c) { return !!zpl_strchr("<>:/", c); }
zpl_inline zpl_b32 zpl__json_is_assign_char(char c) { return !!zpl_strchr(":=|", c); }
zpl_inline zpl_b32 zpl__json_is_delim_char(char c) { return !!zpl_strchr(",|\n", c); }

#define jx(x) !zpl_char_is_hex_digit(str[x])
zpl_inline zpl_b32 zpl__json_validate_name(char *str, char *err) {
while (*str) {
if ((str[0] == '\\' && !zpl__json_is_control_char(str[1])) &&
if ((str[0] == '\\' && !zpl__is_control_char(str[1])) &&
(str[0] == '\\' && jx(1) && jx(2) && jx(3) && jx(4))) {
*err = *str;
return false;
Expand All @@ -945,9 +938,3 @@ zpl_inline zpl_b32 zpl__json_validate_name(char *str, char *err) {
return true;
}
#undef jx

zpl_inline char *zpl__json_skip(char *str, char c) {
while ((*str && *str != c) || (*(str - 1) == '\\' && *str == c && zpl__json_is_control_char(c))) { ++str; }

return str;
}
4 changes: 2 additions & 2 deletions code/zpl/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ zpl_inline zpl_u32 zpl_system_command(const char *command, zpl_usize buffer_len,

if(!handle) return 0;

char c;
int c;
zpl_usize i=0;
while ((c = getc(handle)) != EOF && i++ < buffer_len) {
*buffer++ = c;
Expand Down Expand Up @@ -340,7 +340,7 @@ zpl_inline zpl_string zpl_system_command_str(const char *command, zpl_allocator

zpl_string output = zpl_string_make_reserve(backing, 4);

char c;
int c;
while ((c = getc(handle)) != EOF) {
char ins[2] = {c,0};
output = zpl_string_appendc(output, ins);
Expand Down
4 changes: 2 additions & 2 deletions code/zpl/opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ zpl_b32 zpl_opts_compile(zpl_opts *opts, int argc, char **argv) {
char *p = argv[i];

if (*p) {
p = zpl__trim(p, false);
p = zpl_str_trim(p, false);
if (*p == '-') {
zpl_opts_entry *t = 0;
zpl_b32 checkln = false;
Expand Down Expand Up @@ -350,7 +350,7 @@ zpl_b32 zpl_opts_compile(zpl_opts *opts, int argc, char **argv) {
}
}

e = zpl__skip(e, '\0');
e = zpl_str_control_skip(e, '\0');
zpl__opts_set_value(opts, t, b);
} else {
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION);
Expand Down
19 changes: 18 additions & 1 deletion code/zpl/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ZPL_DEF void zpl_str_to_upper(char *str);

ZPL_DEF char *zpl_str_trim(char *str, zpl_b32 skip_newline);
ZPL_DEF char *zpl_str_skip(char *str, char c);
ZPL_DEF char *zpl_str_control_skip(char *str, char c);

ZPL_DEF zpl_isize zpl_strlen(const char *str);
ZPL_DEF zpl_isize zpl_strnlen(const char *str, zpl_isize max_len);
Expand All @@ -44,7 +45,7 @@ ZPL_DEF const char *zpl_strtok(char *output, const char *src, const char *delimi

// NOTE: This edits *source* string.
// Returns: zpl_array
ZPL_DEF char **zpl_str_split_lines(zpl_allocator alloc, char *source, zpl_b32 strip_whitespace);
ZPL_DEF char **zpl_str_split_lines(zpl_allocator alloc, char *source, zpl_b32 strip_whitespace);

#define zpl_str_expand(str) str, zpl_strlen(str)

Expand Down Expand Up @@ -471,6 +472,22 @@ zpl_inline char **zpl_str_split_lines(zpl_allocator alloc, char *source, zpl_b32
return lines;
}

zpl_inline zpl_b32 zpl__is_control_char(char c) {
return !!zpl_strchr("\"\\/bfnrt", c);
}

zpl_inline zpl_b32 zpl__is_special_char(char c) { return !!zpl_strchr("<>:/", c); }
zpl_inline zpl_b32 zpl__is_assign_char(char c) { return !!zpl_strchr(":=|", c); }
zpl_inline zpl_b32 zpl__is_delim_char(char c) { return !!zpl_strchr(",|\n", c); }


zpl_inline char *zpl_str_control_skip(char *str, char c) {
while ((*str && *str != c) || (*(str - 1) == '\\' && *str == c && zpl__is_control_char(c))) { ++str; }

return str;
}


zpl_inline zpl_b32 zpl_str_has_prefix(const char *str, const char *prefix) {
while (*prefix) {
if (*str++ != *prefix++) return false;
Expand Down
2 changes: 1 addition & 1 deletion code/zpl/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ void zpl_affinity_init(zpl_affinity *a) {
for (;;) {
// The 'temporary char'. Everything goes into this char,
// so that we can check against EOF at the end of this loop.
char c;
int c;

#define AF__CHECK(letter) ((c = getc(cpu_info)) == letter)
if (AF__CHECK('c') && AF__CHECK('p') && AF__CHECK('u') && AF__CHECK(' ') &&
Expand Down

0 comments on commit f577ded

Please sign in to comment.