1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

Fix some issues found with -fsanitize=undefined

Leave in issues with calling memcpy with size=0. If these
become a problem, will probably add a janet_memcpy as memcpy
is used so much in the code without 0 checks.
This commit is contained in:
Calvin Rose 2019-03-04 11:17:34 -05:00
parent 624be87c97
commit b07adce2b9
6 changed files with 28 additions and 20 deletions

View File

@ -89,8 +89,9 @@ static void janetc_loadconst(JanetCompiler *c, Janet k, int32_t reg) {
int32_t i = (int32_t) dval;
if (dval != i || !(dval >= INT16_MIN && dval <= INT16_MAX))
goto do_constant;
uint32_t iu = (uint32_t)i;
janetc_emit(c,
(i << 16) |
(iu << 16) |
(reg << 8) |
JOP_LOAD_INTEGER);
break;

View File

@ -529,15 +529,18 @@ static int32_t readint(UnmarshalState *st, const uint8_t **atdata) {
ret = *data++;
} else if (*data < 192) {
MARSH_EOS(st, data + 1);
ret = ((data[0] & 0x3F) << 8) + data[1];
ret = ((ret << 18) >> 18);
uint32_t uret = ((data[0] & 0x3F) << 8) + data[1];
/* Sign extend 18 MSBs */
uret |= (uret >> 13) ? 0xFFFFC000 : 0;
ret = (int32_t)uret;
data += 2;
} else if (*data == LB_INTEGER) {
MARSH_EOS(st, data + 4);
ret = ((int32_t)(data[1]) << 24) |
((int32_t)(data[2]) << 16) |
((int32_t)(data[3]) << 8) |
(int32_t)(data[4]);
uint32_t ui = ((uint32_t)(data[1]) << 24) |
((uint32_t)(data[2]) << 16) |
((uint32_t)(data[3]) << 8) |
(uint32_t)(data[4]);
ret = (int32_t)ui;
data += 5;
} else {
janet_panicf("expected integer, got byte %x at index %d",
@ -970,11 +973,12 @@ static const uint8_t *unmarshal_one(
case LB_INTEGER:
/* Long integer */
MARSH_EOS(st, data + 4);
*out = janet_wrap_integer(
(data[4]) |
(data[3] << 8) |
(data[2] << 16) |
(data[1] << 24));
uint32_t ui = ((uint32_t)(data[4])) |
((uint32_t)(data[3]) << 8) |
((uint32_t)(data[2]) << 16) |
((uint32_t)(data[1]) << 24);
int32_t si = (int32_t)ui;
*out = janet_wrap_integer(si);
return data + 5;
case LB_REAL:
/* Real */

View File

@ -49,7 +49,7 @@ static const uint32_t symchars[8] = {
/* Check if a character is a valid symbol character
* symbol chars are A-Z, a-z, 0-9, or one of !$&*+-./:<=>@\^_~| */
static int is_symbol_char(uint8_t c) {
return symchars[c >> 5] & (1 << (c & 0x1F));
return symchars[c >> 5] & ((uint32_t)1 << (c & 0x1F));
}
/* Validate some utf8. Useful for identifiers. Only validates

View File

@ -66,12 +66,16 @@ void janetc_regalloc_clone(JanetcRegisterAllocator *dest, JanetcRegisterAllocato
dest->capacity = src->capacity;
dest->max = src->max;
size = sizeof(uint32_t) * dest->capacity;
dest->chunks = malloc(size);
dest->regtemps = 0;
if (!dest->chunks) {
JANET_OUT_OF_MEMORY;
if (size) {
dest->chunks = malloc(size);
if (!dest->chunks) {
JANET_OUT_OF_MEMORY;
}
memcpy(dest->chunks, src->chunks, size);
} else {
dest->chunks = NULL;
}
memcpy(dest->chunks, src->chunks, size);
}
/* Allocate one more chunk in chunks */

View File

@ -566,8 +566,8 @@ static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv)
/* Calculate jumps */
labeld = janet_v_count(c->buffer);
if (!infinite) c->buffer[labelc] |= (labeld - labelc) << 16;
c->buffer[labeljt] |= (labelwt - labeljt) << 8;
if (!infinite) c->buffer[labelc] |= (uint32_t)(labeld - labelc) << 16;
c->buffer[labeljt] |= (uint32_t)(labelwt - labeljt) << 8;
/* Pop scope and return nil slot */
janetc_popscope(c);

View File

@ -133,7 +133,6 @@ extern "C" {
#define JANET_TYPED_ARRAY
#endif
/* How to export symbols */
#ifndef JANET_API
#ifdef JANET_WINDOWS