From 1c8115dbc9179d6c62886613cdadfb64758e010f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 16 Sep 2025 18:13:12 +0900 Subject: [PATCH] fixed store_into_numeric_rcv_slot() which checked the return value of hak_inttooow() in a wrong way --- README.md | 17 +++++++++++++++++ lib/bigint.c | 2 +- lib/exec.c | 8 ++++---- lib/fmt.c | 3 +-- lib/obj.c | 42 ++++++++++++++++++++++++------------------ lib/prim.c | 7 +++---- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index ab5940c..51fe67c 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,25 @@ class[attributes] Name: Superclass (ivars (cvars)) { 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 diff --git a/lib/bigint.c b/lib/bigint.c index 725c89f..591bbf1 100644 --- a/lib/bigint.c +++ b/lib/bigint.c @@ -321,7 +321,7 @@ int hak_inttooow (hak_t* hak, hak_oop_t x, hak_oow_t* w) if (v < 0) { *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 */ } else diff --git a/lib/exec.c b/lib/exec.c index bd80580..fab005a 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -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; 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); switch (rcv_type) { 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; 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; 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; case HAK_OBJ_TYPE_WORD: diff --git a/lib/fmt.c b/lib/fmt.c index b882606..403e3fc 100644 --- a/lib/fmt.c +++ b/lib/fmt.c @@ -65,7 +65,6 @@ #include "hak-prv.h" - #if defined(HAK_ENABLE_FLTFMT) #include /* 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; 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; return p; /* returns the end */ diff --git a/lib/obj.c b/lib/obj.c index 867acab..3604577 100644 --- a/lib/obj.c +++ b/lib/obj.c @@ -182,7 +182,10 @@ static HAK_INLINE hak_oop_t alloc_numeric_array (hak_t* hak, const void* ptr, ha /* allocate a variable object */ 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; /* '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)); /* 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 * HAK_SIZEOF(hak_oop_t) will guarantee the starting address * of the allocated space to be an even number. * see HAK_OOP_IS_NUMERIC() and HAK_OOP_IS_POINTER() */ 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 - hdr = (hak_oop_t)hak_allocbytes(hak, HAK_SIZEOF(hak_obj_t) + nbytes_aligned); - if (HAK_UNLIKELY(!hdr)) return HAK_NULL; + hdr = (hak_oop_t)hak_allocbytes(hak, nbytes_total); - hdr->_flags = HAK_OBJ_MAKE_FLAGS(type, unit, extra, 0, 0, ngc, 0); - hdr->_size = len; - HAK_OBJ_SET_SIZE (hdr, len); - /*HAK_OBJ_SET_CLASS (hdr, hak->_nil);*/ + if (HAK_LIKELY(hdr)) + { + hdr->_flags = HAK_OBJ_MAKE_FLAGS(type, unit, extra, 0, 0, ngc, 0); + hdr->_size = len; + HAK_OBJ_SET_SIZE (hdr, len); + /*HAK_OBJ_SET_CLASS (hdr, hak->_nil);*/ - if (ptr) - { - /* copy data */ - HAK_MEMCPY(hdr + 1, ptr, xbytes); - HAK_MEMSET((hak_uint8_t*)(hdr + 1) + xbytes, 0, nbytes_aligned - xbytes); - } - else - { - /* initialize with zeros when the string pointer is not given */ - HAK_MEMSET(hdr + 1, 0, nbytes_aligned); + if (ptr) + { + /* copy data */ + HAK_MEMCPY(hdr + 1, ptr, xbytes); + HAK_MEMSET((hak_uint8_t*)(hdr + 1) + xbytes, 0, nbytes_aligned - xbytes); + } + else + { + /* initialize with zeros when the string pointer is not given */ + HAK_MEMSET(hdr + 1, 0, nbytes_aligned); + } } return hdr; diff --git a/lib/prim.c b/lib/prim.c index 119e4f5..373d063 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -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); 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 <= -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; sz = HAK_STACK_GETARG(hak, nargs, 1); - n = hak_inttooow(hak, sz, &size); - if (n == 0) return HAK_PF_FAILURE; - if (n <= -1) + n = hak_inttooow_noseterr(hak, sz, &size); + if (n <= 0) { hak_seterrbfmt(hak, HAK_EINVAL, "invalid size - %O", sz); return HAK_PF_FAILURE;