fixed store_into_numeric_rcv_slot() which checked the return value of hak_inttooow() in a wrong way

This commit is contained in:
2025-09-16 18:13:12 +09:00
parent 3faca8ae98
commit 1c8115dbc9
6 changed files with 50 additions and 29 deletions

View File

@ -114,8 +114,25 @@ class[attributes] Name: Superclass (ivars (cvars)) {
function body function body
} }
} }
```
``` ```
class[#b] B (a b) {
fun[#ci] new() {
self.a := 88
self.b := 99
}
fun print() {
printf "%d %d\n" self.a self.b
}
}
x := (B:new)
x:print
```
## Redefining a primitive function ## Redefining a primitive function

View File

@ -321,7 +321,7 @@ int hak_inttooow (hak_t* hak, hak_oop_t x, hak_oow_t* w)
if (v < 0) if (v < 0)
{ {
*w = -v; *w = -v;
hak_seterrnum(hak, HAK_ERANGE); hak_seterrbfmt(hak, HAK_ERANGE, "negative number - %O", x);
return -1; /* negative number negated - kind of an error */ return -1; /* negative number negated - kind of an error */
} }
else else

View File

@ -3386,21 +3386,21 @@ static int store_into_numeric_rcv_slot (hak_t* hak, hak_oop_t rcv, hak_oow_t b1,
hak_obj_type_t rcv_type; hak_obj_type_t rcv_type;
if (HAK_OOP_IS_CHAR(v)) w = HAK_OOP_TO_CHAR(v); if (HAK_OOP_IS_CHAR(v)) w = HAK_OOP_TO_CHAR(v);
else if (hak_inttooow(hak, v, &w) <= -1) return -1; else if (hak_inttooow(hak, v, &w) <= 0) return -1;
rcv_type = (hak_obj_type_t)HAK_OBJ_GET_FLAGS_TYPE(rcv); rcv_type = (hak_obj_type_t)HAK_OBJ_GET_FLAGS_TYPE(rcv);
switch (rcv_type) switch (rcv_type)
{ {
case HAK_OBJ_TYPE_CHAR: case HAK_OBJ_TYPE_CHAR:
((hak_oop_char_t)rcv)->slot[b1] = w; ((hak_oop_char_t)rcv)->slot[b1] = (hak_ooch_t)(hak_oochu_t)w;
break; break;
case HAK_OBJ_TYPE_BYTE: case HAK_OBJ_TYPE_BYTE:
((hak_oop_byte_t)rcv)->slot[b1] = w; ((hak_oop_byte_t)rcv)->slot[b1] = (hak_oob_t)w;
break; break;
case HAK_OBJ_TYPE_HALFWORD: case HAK_OBJ_TYPE_HALFWORD:
((hak_oop_halfword_t)rcv)->slot[b1] = w; ((hak_oop_halfword_t)rcv)->slot[b1] = (hak_oohw_t)w;
break; break;
case HAK_OBJ_TYPE_WORD: case HAK_OBJ_TYPE_WORD:

View File

@ -65,7 +65,6 @@
#include "hak-prv.h" #include "hak-prv.h"
#if defined(HAK_ENABLE_FLTFMT) #if defined(HAK_ENABLE_FLTFMT)
#include <stdio.h> /* for snrintf(). used for floating-point number formatting */ #include <stdio.h> /* for snrintf(). used for floating-point number formatting */
@ -318,7 +317,7 @@ static hak_bch_t* sprintn_lower (hak_bch_t* nbuf, hak_uintmax_t num, int base, h
hak_bch_t* p; hak_bch_t* p;
p = nbuf; *p = '\0'; p = nbuf; *p = '\0';
do {*++p = hex2ascii_lower[num % base]; } while (num /= base); do { *++p = hex2ascii_lower[num % base]; } while (num /= base);
if (lenp) *lenp = p - nbuf; if (lenp) *lenp = p - nbuf;
return p; /* returns the end */ return p; /* returns the end */

View File

@ -182,7 +182,10 @@ static HAK_INLINE hak_oop_t alloc_numeric_array (hak_t* hak, const void* ptr, ha
/* allocate a variable object */ /* allocate a variable object */
hak_oop_t hdr; hak_oop_t hdr;
hak_oow_t xbytes, nbytes, nbytes_aligned; hak_oow_t xbytes;
hak_oow_t nbytes;
hak_oow_t nbytes_aligned;
hak_oow_t nbytes_total;
xbytes = len * unit; xbytes = len * unit;
/* 'extra' indicates an extra unit to append at the end. /* 'extra' indicates an extra unit to append at the end.
@ -191,31 +194,34 @@ static HAK_INLINE hak_oop_t alloc_numeric_array (hak_t* hak, const void* ptr, ha
nbytes_aligned = HAK_ALIGN(nbytes, HAK_SIZEOF(hak_oop_t)); nbytes_aligned = HAK_ALIGN(nbytes, HAK_SIZEOF(hak_oop_t));
/* TODO: check overflow in size calculation*/ /* TODO: check overflow in size calculation*/
nbytes_total = HAK_SIZEOF(hak_obj_t) + nbytes_aligned;
/* making the number of bytes to allocate a multiple of /* making the number of bytes to allocate a multiple of
* HAK_SIZEOF(hak_oop_t) will guarantee the starting address * HAK_SIZEOF(hak_oop_t) will guarantee the starting address
* of the allocated space to be an even number. * of the allocated space to be an even number.
* see HAK_OOP_IS_NUMERIC() and HAK_OOP_IS_POINTER() */ * see HAK_OOP_IS_NUMERIC() and HAK_OOP_IS_POINTER() */
if (HAK_UNLIKELY(ngc)) if (HAK_UNLIKELY(ngc))
hdr = (hak_oop_t)hak_callocmem(hak, HAK_SIZEOF(hak_obj_t) + nbytes_aligned); hdr = (hak_oop_t)hak_callocmem(hak, nbytes_total);
else else
hdr = (hak_oop_t)hak_allocbytes(hak, HAK_SIZEOF(hak_obj_t) + nbytes_aligned); hdr = (hak_oop_t)hak_allocbytes(hak, nbytes_total);
if (HAK_UNLIKELY(!hdr)) return HAK_NULL;
hdr->_flags = HAK_OBJ_MAKE_FLAGS(type, unit, extra, 0, 0, ngc, 0); if (HAK_LIKELY(hdr))
hdr->_size = len; {
HAK_OBJ_SET_SIZE (hdr, len); hdr->_flags = HAK_OBJ_MAKE_FLAGS(type, unit, extra, 0, 0, ngc, 0);
/*HAK_OBJ_SET_CLASS (hdr, hak->_nil);*/ hdr->_size = len;
HAK_OBJ_SET_SIZE (hdr, len);
/*HAK_OBJ_SET_CLASS (hdr, hak->_nil);*/
if (ptr) if (ptr)
{ {
/* copy data */ /* copy data */
HAK_MEMCPY(hdr + 1, ptr, xbytes); HAK_MEMCPY(hdr + 1, ptr, xbytes);
HAK_MEMSET((hak_uint8_t*)(hdr + 1) + xbytes, 0, nbytes_aligned - xbytes); HAK_MEMSET((hak_uint8_t*)(hdr + 1) + xbytes, 0, nbytes_aligned - xbytes);
} }
else else
{ {
/* initialize with zeros when the string pointer is not given */ /* initialize with zeros when the string pointer is not given */
HAK_MEMSET(hdr + 1, 0, nbytes_aligned); HAK_MEMSET(hdr + 1, 0, nbytes_aligned);
}
} }
return hdr; return hdr;

View File

@ -1166,7 +1166,7 @@ static hak_pfrc_t pf_va_get (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
nlvars = GET_BLK_MASK_NLVARS(attr_mask); nlvars = GET_BLK_MASK_NLVARS(attr_mask);
idx = HAK_STACK_GETARG(hak, nargs, 0); idx = HAK_STACK_GETARG(hak, nargs, 0);
n = hak_inttooow(hak, idx, &index); n = hak_inttooow_noseterr(hak, idx, &index);
if (n <= 0) if (n <= 0)
{ {
if (n <= -1) hak_seterrbfmt(hak, HAK_EINVAL, "invalid index - %O", idx); if (n <= -1) hak_seterrbfmt(hak, HAK_EINVAL, "invalid index - %O", idx);
@ -1209,9 +1209,8 @@ static hak_pfrc_t pf_object_new (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
hak_oop_t sz; hak_oop_t sz;
sz = HAK_STACK_GETARG(hak, nargs, 1); sz = HAK_STACK_GETARG(hak, nargs, 1);
n = hak_inttooow(hak, sz, &size); n = hak_inttooow_noseterr(hak, sz, &size);
if (n == 0) return HAK_PF_FAILURE; if (n <= 0)
if (n <= -1)
{ {
hak_seterrbfmt(hak, HAK_EINVAL, "invalid size - %O", sz); hak_seterrbfmt(hak, HAK_EINVAL, "invalid size - %O", sz);
return HAK_PF_FAILURE; return HAK_PF_FAILURE;