#include <njs.h>
+#include <limits.h>
#include <string.h>
#include <zlib.h>
if (njs_value_is_object(options)) {
value = njs_vm_object_prop(vm, options, &chunk_size_key, &lvalue);
if (value != NULL) {
- chunk_size = njs_value_number(value);
+ chunk_size = njs_number_to_length(njs_value_number(value));
- if (njs_slow_path(chunk_size < 64)) {
+ if (njs_slow_path(chunk_size < 64 || chunk_size > UINT_MAX)) {
njs_vm_range_error(vm, "chunkSize must be >= 64");
return NJS_ERROR;
}
if (njs_value_is_object(options)) {
value = njs_vm_object_prop(vm, options, &chunk_size_key, &lvalue);
if (value != NULL) {
- chunk_size = njs_value_number(value);
+ chunk_size = njs_number_to_length(njs_value_number(value));
- if (njs_slow_path(chunk_size < 64)) {
+ if (njs_slow_path(chunk_size < 64 || chunk_size > UINT_MAX)) {
njs_vm_range_error(vm, "chunkSize must be >= 64");
return NJS_ERROR;
}
{
/* Do nothing. */
}
-
*/
#include <qjs.h>
+#include <limits.h>
#include <zlib.h>
#define NJS_ZLIB_CHUNK_SIZE 1024
qjs_zlib_ext_deflate(JSContext *ctx, JSValueConst this_val, int argc,
JSValueConst *argv, int raw)
{
- int rc, chunk_size, level, mem_level, strategy, window_bits;
+ int rc, level, mem_level, strategy, window_bits;
+ size_t chunk_size;
+ uint64_t chunk_size_value;
JSValue ret, options;
z_stream stream;
njs_chb_t chain;
}
if (!JS_IsUndefined(ret)) {
- rc = JS_ToInt32(ctx, &chunk_size, ret);
+ rc = qjs_to_length(ctx, ret, &chunk_size_value);
JS_FreeValue(ctx, ret);
if (rc != 0) {
return JS_EXCEPTION;
}
- if (chunk_size < 64) {
+ if (chunk_size_value < 64 || chunk_size_value > UINT_MAX) {
JS_ThrowRangeError(ctx, "chunkSize must be >= 64");
return JS_EXCEPTION;
}
+
+ chunk_size = chunk_size_value;
}
ret = JS_GetPropertyStr(ctx, options, "level");
qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc,
JSValueConst *argv, int raw)
{
- int rc, chunk_size, window_bits;
+ int rc, window_bits;
+ size_t chunk_size;
+ uint64_t chunk_size_value;
JSValue ret, options;
z_stream stream;
njs_chb_t chain;
}
if (!JS_IsUndefined(ret)) {
- rc = JS_ToInt32(ctx, &chunk_size, ret);
+ rc = qjs_to_length(ctx, ret, &chunk_size_value);
JS_FreeValue(ctx, ret);
if (rc != 0) {
return JS_EXCEPTION;
}
- if (chunk_size < 64) {
+ if (chunk_size_value < 64 || chunk_size_value > UINT_MAX) {
JS_ThrowRangeError(ctx, "chunkSize must be >= 64");
return JS_EXCEPTION;
}
+
+ chunk_size = chunk_size_value;
}
ret = JS_GetPropertyStr(ctx, options, "windowBits");
NJS_EXPORT uint8_t njs_value_bool(const njs_value_t *value);
NJS_EXPORT double njs_value_number(const njs_value_t *value);
+NJS_EXPORT uint64_t njs_number_to_length(double num);
NJS_EXPORT njs_function_t *njs_value_function(const njs_value_t *value);
NJS_EXPORT njs_function_native_t njs_value_native_function(
const njs_value_t *value);
double number, uint32_t radix);
+uint64_t
+njs_number_to_length(double num)
+{
+ if (isnan(num)) {
+ return 0;
+ }
+
+ if (num > NJS_MAX_LENGTH) {
+ return NJS_MAX_LENGTH;
+
+ } else if (num < 0.0) {
+ return 0;
+ }
+
+ return (uint64_t) num;
+}
+
+
double
njs_key_to_index(const njs_value_t *value)
{
}
-njs_inline uint64_t
-njs_number_to_length(double num)
-{
- if (isnan(num)) {
- return 0;
- }
-
- if (num > NJS_MAX_LENGTH) {
- return NJS_MAX_LENGTH;
-
- } else if (num < 0.0) {
- return 0;
- }
-
- return (uint64_t) num;
-}
-
-
njs_inline njs_int_t
njs_char_to_hex(u_char c)
{
{ value: 'αβγ', raw: false, expected: 'eJw7t/HcpnObAQ/sBIE=' },
{ value: 'WAKA', options: {level: 10}, exception: 'RangeError: level must be in the range -1..9' },
+ { value: 'WAKA', options: {chunkSize: Infinity},
+ exception: 'RangeError: chunkSize must be >= 64' },
+ { value: 'WAKA', options: {chunkSize: NaN},
+ exception: 'RangeError: chunkSize must be >= 64' },
{ value: 'WAKA', options: {strategy: 10}, exception: 'RangeError: unknown strategy: 10' },
{ value: 'WAKA', options: {memLevel: 10}, exception: 'RangeError: memLevel must be in the range 1..9' },
{ value: 'WAKA', options: {windowBits: 99}, exception: 'RangeError: windowBits must be in the range -15..-9' },
{ value: Buffer.from('C3f0dgQA', 'base64'), options: {chunkSize: 0},
exception: 'RangeError: chunkSize must be >= 64' },
+ { value: Buffer.from('C3f0dgQA', 'base64'), options: {chunkSize: Infinity},
+ exception: 'RangeError: chunkSize must be >= 64' },
+ { value: Buffer.from('C3f0dgQA', 'base64'), options: {chunkSize: NaN},
+ exception: 'RangeError: chunkSize must be >= 64' },
{ value: Buffer.from('C3f0dgQA', 'base64'), options: {windowBits: 0},
exception: 'RangeError: windowBits must be in the range -15..-8' },