From 9a76e77981bc96aab3f6fc36f654661d38b8c04e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 24 Mar 2023 18:49:21 -0500 Subject: [PATCH] Update for undefined behavior sanitizer. --- src/core/parse.c | 8 +++++--- src/core/pp.c | 2 +- src/core/specials.c | 5 +++-- src/core/util.c | 1 + 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/core/parse.c b/src/core/parse.c index 4d98490f..8358a996 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -1194,7 +1194,8 @@ static Janet parser_state_delimiters(const JanetParser *_p) { } } } - str = janet_string(p->buf + oldcount, (int32_t)(p->bufcount - oldcount)); + /* avoid ptr arithmetic on NULL */ + str = janet_string(oldcount ? p->buf + oldcount : p->buf, (int32_t)(p->bufcount - oldcount)); p->bufcount = oldcount; return janet_wrap_string(str); } @@ -1205,10 +1206,11 @@ static Janet parser_state_frames(const JanetParser *p) { states->count = count; uint8_t *buf = p->buf; /* Iterate arg stack backwards */ - Janet *args = p->args + p->argcount; + Janet *args = p->argcount ? p->args + p->argcount : p->args; /* avoid ptr arithmetic on NULL */ for (int32_t i = count - 1; i >= 0; --i) { JanetParseState *s = p->states + i; - if (s->flags & PFLAG_CONTAINER) { + /* avoid ptr arithmetic on args if NULL */ + if ((s->flags & PFLAG_CONTAINER) && s->argn) { args -= s->argn; } states->data[i] = janet_wrap_parse_state(s, args, buf, (uint32_t) p->bufcount); diff --git a/src/core/pp.c b/src/core/pp.c index 1e424096..cd545ad1 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -637,7 +637,7 @@ static void janet_pretty_one(struct pretty *S, Janet x, int is_dict_value) { } } - janet_sorted_keys(kvs, cap, S->keysort_buffer + ks_start); + janet_sorted_keys(kvs, cap, S->keysort_buffer == NULL ? NULL : S->keysort_buffer + ks_start); S->keysort_start += len; if (!(S->flags & JANET_PRETTY_NOTRUNC) && (len > JANET_PRETTY_DICT_LIMIT)) { len = JANET_PRETTY_DICT_LIMIT; diff --git a/src/core/specials.c b/src/core/specials.c index b8772218..a69d9627 100644 --- a/src/core/specials.c +++ b/src/core/specials.c @@ -203,8 +203,9 @@ static int destructure(JanetCompiler *c, janetc_emit(c, JOP_JUMP); int32_t label_loop_exit = janet_v_count(c->buffer); - c->buffer[label_loop_cond_jump] |= (label_loop_exit - label_loop_cond_jump) << 16; - c->buffer[label_loop_loop] |= (label_loop_start - label_loop_loop) << 8; + /* avoid shifting negative numbers */ + c->buffer[label_loop_cond_jump] |= (uint32_t)(label_loop_exit - label_loop_cond_jump) << 16; + c->buffer[label_loop_loop] |= (uint32_t)(label_loop_start - label_loop_loop) << 8; janetc_freeslot(c, argi); janetc_freeslot(c, arg); diff --git a/src/core/util.c b/src/core/util.c index 4fb4c4ca..4d24dca7 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -118,6 +118,7 @@ const char *const janet_status_names[16] = { #ifndef JANET_PRF int32_t janet_string_calchash(const uint8_t *str, int32_t len) { + if (NULL == str) return 5381; const uint8_t *end = str + len; uint32_t hash = 5381; while (str < end)