QuickJS-NG js_malloc() returns NULL and throws "out of memory" for a
zero size, while QuickJS returns a valid pointer. Empty inputs to hash
update() and fs write and read functions with a string encoding, empty
raw and JWK secret keys in WebCrypto importKey() and zero-length
deriveBits() failed with an out of memory error. Since QuickJS-NG
0.18.0 (
90aa2c4) that error is uncatchable, so such a call terminates
the script.
Now at least one byte is allocated, as QuickJS-NG itself does for
possibly empty allocations.
if (enc->decode_length != NULL) {
content.length = enc->decode_length(cx, &str);
- content.start = js_malloc(cx, content.length);
+ content.start = js_malloc(cx, njs_max(content.length, 1));
if (content.start == NULL) {
JS_FreeCString(cx, (const char *) str.start);
return JS_ThrowOutOfMemory(cx);
if (encoding->decode_length != NULL) {
content.length = encoding->decode_length(cx, &str);
- content.start = js_malloc(cx, content.length);
+ content.start = js_malloc(cx, njs_max(content.length, 1));
if (content.start == NULL) {
JS_FreeCString(cx, (const char *) str.start);
JS_ThrowOutOfMemory(cx);
if (encoding->decode_length != NULL) {
content.length = encoding->decode_length(cx, &str);
- content.start = js_malloc(cx, content.length);
+ content.start = js_malloc(cx, njs_max(content.length, 1));
if (content.start == NULL) {
JS_FreeCString(cx, (const char *) str.start);
JS_ThrowOutOfMemory(cx);
} else if (encoding->encode_length != NULL) {
data.length = encoding->encode_length(cx, str);
- data.start = js_malloc(cx, data.length);
+ data.start = js_malloc(cx, njs_max(data.length, 1));
if (data.start == NULL) {
JS_ThrowOutOfMemory(cx);
return JS_EXCEPTION;
dkey = JS_GetOpaque(dkey_value, QJS_CORE_CLASS_ID_WEBCRYPTO_KEY);
}
- k = js_malloc(cx, length);
+ k = js_malloc(cx, njs_max(length, 1));
if (k == NULL) {
JS_ThrowOutOfMemory(cx);
return JS_EXCEPTION;
size = qjs_base64url_decode_length(cx, &b64);
key->u.s.raw.length = size;
- key->u.s.raw.start = js_malloc(cx, size);
+ key->u.s.raw.start = js_malloc(cx, njs_max(size, 1));
if (key->u.s.raw.start == NULL) {
JS_FreeCString(cx, (char *) b64.start);
JS_ThrowOutOfMemory(cx);
goto fail;
}
- wkey->u.s.raw.start = js_malloc(cx, kd.length);
+ wkey->u.s.raw.start = js_malloc(cx, njs_max(kd.length, 1));
if (wkey->u.s.raw.start == NULL) {
JS_ThrowOutOfMemory(cx);
goto fail;
case QJS_ALGORITHM_PBKDF2:
case QJS_ALGORITHM_HKDF:
default:
- wkey->u.s.raw.start = js_malloc(cx, kd.length);
+ wkey->u.s.raw.start = js_malloc(cx, njs_max(kd.length, 1));
if (wkey->u.s.raw.start == NULL) {
JS_ThrowOutOfMemory(cx);
goto fail;