diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-06-12 06:52:58 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-06-12 06:52:58 -0700 |
commit | f1ccbf903ec1473531815809424e701e5af88154 (patch) | |
tree | 33866a5f68be66d86c1058d46a5ec95b82853c7b | |
parent | ff0366ac3249b1a25813921e1af9346f345fd4a3 (diff) | |
download | txr-f1ccbf903ec1473531815809424e701e5af88154.tar.gz txr-f1ccbf903ec1473531815809424e701e5af88154.tar.bz2 txr-f1ccbf903ec1473531815809424e701e5af88154.zip |
lib: new chk_xalloc, with mult overflow check.
This will simplify code that allocates an array-like
object whose size is the product of two numbers.
* lib.c (chk_xalloc): New function.
* lib.h (chk_xalloc): Declared.
-rw-r--r-- | lib.c | 12 | ||||
-rw-r--r-- | lib.h | 1 |
2 files changed, 13 insertions, 0 deletions
@@ -2699,6 +2699,18 @@ mem_t *chk_copy_obj(mem_t *orig, size_t size) return copy; } +mem_t *chk_xalloc(ucnum m, ucnum n, val self) +{ + ucnum mn = m * n; + size_t size = mn; + + if ((m > 0 && mn / m != n) || (ucnum) size != mn) + uw_throwf(error_s, lit("~s: memory allocation size overflow"), + self, nao); + + return chk_malloc(size); +} + val cons(val car, val cdr) { val obj; @@ -609,6 +609,7 @@ wchar_t *chk_strdup(const wchar_t *str); char *chk_strdup_utf8(const char *str); unsigned char *chk_strdup_8bit(const wchar_t *str); mem_t *chk_copy_obj(mem_t *orig, size_t size); +mem_t *chk_xalloc(ucnum m, ucnum n, val self); val cons(val car, val cdr); val make_lazy_cons(val func); val make_half_lazy_cons(val func, val car); |