Skip to content

Commit e118aae

Browse files
committed
Fix a few debug/assert warnings
1 parent ffae400 commit e118aae

10 files changed

Lines changed: 35 additions & 30 deletions

src-input/duk_api_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ DUK_INTERNAL_DECL void duk_require_constructable(duk_context *ctx, duk_idx_t idx
218218
* Both 'ctx' and 'idx' are evaluted multiple times, but only for asserts.
219219
*/
220220
#define DUK_ASSERT_VALID_NEGIDX(ctx,idx) \
221-
(DUK_ASSERT_EXPR((idx) < 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx))))
221+
(DUK_ASSERT_EXPR((duk_int_t) (idx) < 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx))))
222222
#define DUK_ASSERT_VALID_POSIDX(ctx,idx) \
223-
(DUK_ASSERT_EXPR((idx) >= 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx))))
223+
(DUK_ASSERT_EXPR((duk_int_t) (idx) >= 0), DUK_ASSERT_EXPR(duk_is_valid_index((ctx), (idx))))
224224
#define DUK_GET_TVAL_NEGIDX(ctx,idx) \
225225
(DUK_ASSERT_VALID_NEGIDX((ctx),(idx)), ((duk_hthread *) (ctx))->valstack_top + (idx))
226226
#define DUK_GET_TVAL_POSIDX(ctx,idx) \

src-input/duk_api_object.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_idx,
6565
duk_hthread *thr = (duk_hthread *) ctx;
6666

6767
DUK_ASSERT_CTX_VALID(ctx);
68-
DUK_ASSERT_DISABLE(stridx >= 0);
69-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
68+
DUK_ASSERT_STRIDX_VALID(stridx);
7069
DUK_UNREF(thr);
7170

7271
obj_idx = duk_require_normalize_index(ctx, obj_idx);
@@ -78,8 +77,7 @@ DUK_INTERNAL duk_bool_t duk_get_prop_stridx_boolean(duk_context *ctx, duk_idx_t
7877
duk_bool_t rc;
7978

8079
DUK_ASSERT_CTX_VALID(ctx);
81-
DUK_ASSERT_DISABLE(stridx >= 0);
82-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
80+
DUK_ASSERT_STRIDX_VALID(stridx);
8381

8482
rc = duk_get_prop_stridx(ctx, obj_idx, stridx);
8583
if (out_has_prop) {
@@ -160,8 +158,7 @@ DUK_INTERNAL duk_bool_t duk_put_prop_stridx(duk_context *ctx, duk_idx_t obj_idx,
160158
duk_hthread *thr = (duk_hthread *) ctx;
161159

162160
DUK_ASSERT_CTX_VALID(ctx);
163-
DUK_ASSERT_DISABLE(stridx >= 0);
164-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
161+
DUK_ASSERT_STRIDX_VALID(stridx);
165162
DUK_UNREF(thr);
166163

167164
obj_idx = duk_require_normalize_index(ctx, obj_idx);
@@ -223,8 +220,7 @@ DUK_INTERNAL duk_bool_t duk_del_prop_stridx(duk_context *ctx, duk_idx_t obj_idx,
223220
duk_hthread *thr = (duk_hthread *) ctx;
224221

225222
DUK_ASSERT_CTX_VALID(ctx);
226-
DUK_ASSERT_DISABLE(stridx >= 0);
227-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
223+
DUK_ASSERT_STRIDX_VALID(stridx);
228224
DUK_UNREF(thr);
229225

230226
obj_idx = duk_require_normalize_index(ctx, obj_idx);
@@ -284,8 +280,7 @@ DUK_INTERNAL duk_bool_t duk_has_prop_stridx(duk_context *ctx, duk_idx_t obj_idx,
284280
duk_hthread *thr = (duk_hthread *) ctx;
285281

286282
DUK_ASSERT_CTX_VALID(ctx);
287-
DUK_ASSERT_DISABLE(stridx >= 0);
288-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
283+
DUK_ASSERT_STRIDX_VALID(stridx);
289284
DUK_UNREF(thr);
290285

291286
obj_idx = duk_require_normalize_index(ctx, obj_idx);
@@ -335,8 +330,7 @@ DUK_INTERNAL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_idx, duk_
335330
duk_hstring *key;
336331

337332
DUK_ASSERT_CTX_VALID(ctx);
338-
DUK_ASSERT_DISABLE(stridx >= 0);
339-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
333+
DUK_ASSERT_STRIDX_VALID(stridx);
340334

341335
obj = duk_require_hobject(ctx, obj_idx);
342336
DUK_ASSERT(obj != NULL);
@@ -354,10 +348,8 @@ DUK_INTERNAL void duk_xdef_prop_stridx_builtin(duk_context *ctx, duk_idx_t obj_i
354348
duk_hstring *key;
355349

356350
DUK_ASSERT_CTX_VALID(ctx);
357-
DUK_ASSERT_DISABLE(stridx >= 0);
358-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
359-
DUK_ASSERT_DISABLE(builtin_idx >= 0);
360-
DUK_ASSERT(builtin_idx < DUK_NUM_BUILTINS);
351+
DUK_ASSERT_STRIDX_VALID(stridx);
352+
DUK_ASSERT_BIDX_VALID(builtin_idx);
361353

362354
obj = duk_require_hobject(ctx, obj_idx);
363355
DUK_ASSERT(obj != NULL);

src-input/duk_api_stack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,7 @@ DUK_INTERNAL duk_int_t duk_get_type_tval(duk_tval *tv) {
28062806
return DUK_TYPE_NUMBER;
28072807
}
28082808
#else /* DUK_USE_PACKED_TVAL */
2809-
DUK_ASSERT(DUK_TVAL_GET_TAG(tv) >= DUK_TAG_MIN && DUK_TVAL_GET_TAG(tv) <= DUK_TAG_MAX);
2809+
DUK_ASSERT(DUK_TVAL_IS_VALID_TAG(tv));
28102810
DUK_ASSERT(sizeof(duk__type_from_tag) / sizeof(duk_uint_t) == DUK_TAG_MAX - DUK_TAG_MIN + 1);
28112811
return (duk_int_t) duk__type_from_tag[DUK_TVAL_GET_TAG(tv) - DUK_TAG_MIN];
28122812
#endif /* DUK_USE_PACKED_TVAL */
@@ -2911,7 +2911,7 @@ DUK_INTERNAL duk_uint_t duk_get_type_mask_tval(duk_tval *tv) {
29112911
return DUK_TYPE_MASK_NUMBER;
29122912
}
29132913
#else /* DUK_USE_PACKED_TVAL */
2914-
DUK_ASSERT(DUK_TVAL_GET_TAG(tv) >= DUK_TAG_MIN && DUK_TVAL_GET_TAG(tv) <= DUK_TAG_MAX);
2914+
DUK_ASSERT(DUK_TVAL_IS_VALID_TAG(tv));
29152915
DUK_ASSERT(sizeof(duk__type_mask_from_tag) / sizeof(duk_uint_t) == DUK_TAG_MAX - DUK_TAG_MIN + 1);
29162916
return (duk_int_t) duk__type_mask_from_tag[DUK_TVAL_GET_TAG(tv) - DUK_TAG_MIN];
29172917
#endif /* DUK_USE_PACKED_TVAL */

src-input/duk_bi_json.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ DUK_LOCAL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t st
304304
* have internal NULs.
305305
*/
306306

307-
DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */
308-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
307+
DUK_ASSERT_STRIDX_VALID(stridx);
309308
h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
310309
DUK_ASSERT(h != NULL);
311310

@@ -1078,8 +1077,7 @@ DUK_LOCAL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *str) {
10781077
DUK_LOCAL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx) {
10791078
duk_hstring *h;
10801079

1081-
DUK_ASSERT_DISABLE(stridx >= 0); /* unsigned */
1082-
DUK_ASSERT(stridx < DUK_HEAP_NUM_STRINGS);
1080+
DUK_ASSERT_STRIDX_VALID(stridx);
10831081
h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
10841082
DUK_ASSERT(h != NULL);
10851083

src-input/duk_bi_string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ DUK_LOCAL duk_ret_t duk__construct_from_codepoints(duk_context *ctx, duk_bool_t
8282
i32 < 0 || i32 > 0x10ffffL) {
8383
DUK_DCERROR_RANGE_INVALID_ARGS((duk_hthread *) ctx);
8484
}
85+
DUK_ASSERT(i32 >= 0 && i32 <= 0x10ffffL);
8586
cp = (duk_ucodepoint_t) i32;
86-
DUK_ASSERT(cp >= 0 && cp <= 0x10ffffL);
8787
DUK_BW_WRITE_ENSURE_CESU8(thr, bw, cp);
8888
} else {
8989
#if defined(DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT)

src-input/duk_error_augment.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ DUK_LOCAL void duk__err_augment_user(duk_hthread *thr, duk_small_uint_t stridx_c
6767

6868
DUK_ASSERT(thr != NULL);
6969
DUK_ASSERT(thr->heap != NULL);
70-
DUK_ASSERT_DISABLE(stridx_cb >= 0); /* unsigned */
71-
DUK_ASSERT(stridx_cb < DUK_HEAP_NUM_STRINGS);
70+
DUK_ASSERT_STRIDX_VALID(stridx_cb);
7271

7372
if (DUK_HEAP_HAS_ERRHANDLER_RUNNING(thr->heap)) {
7473
DUK_DD(DUK_DDPRINT("recursive call to error handler, ignore"));

src-input/duk_hthread.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@
181181
DUK_ASSERT_CTX_VSSIZE((ctx)); \
182182
} while (0)
183183

184+
/*
185+
* Assertion helpers.
186+
*/
187+
188+
#define DUK_ASSERT_STRIDX_VALID(val) \
189+
DUK_ASSERT((duk_uint_t) (val) < DUK_HEAP_NUM_STRINGS)
190+
191+
#define DUK_ASSERT_BIDX_VALID(val) \
192+
DUK_ASSERT((duk_uint_t) (val) < DUK_NUM_BUILTINS)
193+
184194
/*
185195
* Misc
186196
*/

src-input/duk_js_executor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,7 @@ DUK_LOCAL DUK_NOINLINE void duk__js_execute_bytecode_inner(duk_hthread *entry_th
28782878

28792879
tv = DUK__REGP_BC(ins);
28802880
stridx = duk_js_typeof_stridx(tv);
2881-
DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
2881+
DUK_ASSERT_STRIDX_VALID(stridx);
28822882
h_str = DUK_HTHREAD_GET_STRING(thr, stridx);
28832883
tv = DUK__REGP_A(ins);
28842884
DUK_TVAL_SET_STRING_UPDREF(thr, tv, h_str);
@@ -2915,7 +2915,7 @@ DUK_LOCAL DUK_NOINLINE void duk__js_execute_bytecode_inner(duk_hthread *entry_th
29152915
/* unresolvable, no stack changes */
29162916
stridx = DUK_STRIDX_LC_UNDEFINED;
29172917
}
2918-
DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
2918+
DUK_ASSERT_STRIDX_VALID(stridx);
29192919
#if defined(DUK_USE_EXEC_PREFER_SIZE)
29202920
duk_push_hstring_stridx(ctx, stridx);
29212921
DUK__REPLACE_TOP_A_BREAK();

src-input/duk_js_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ DUK_INTERNAL duk_small_uint_t duk_js_typeof_stridx(duk_tval *tv_x) {
12721272
}
12731273
}
12741274

1275-
DUK_ASSERT(stridx >= 0 && stridx < DUK_HEAP_NUM_STRINGS);
1275+
DUK_ASSERT_STRIDX_VALID(stridx);
12761276
return stridx;
12771277
}
12781278

src-input/duk_tval.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ typedef struct {
6363
#define DUK_XTAG_BOOLEAN_FALSE 0xfff50000UL
6464
#define DUK_XTAG_BOOLEAN_TRUE 0xfff50001UL
6565

66+
#define DUK_TVAL_IS_VALID_TAG(tv) \
67+
(DUK_TVAL_GET_TAG((tv)) - DUK_TAG_MIN <= DUK_TAG_MAX - DUK_TAG_MIN)
68+
6669
/* DUK_TVAL_UNUSED initializer for duk_tval_unused, works for any endianness. */
6770
#define DUK_TVAL_UNUSED_INITIALIZER() \
6871
{ DUK_TAG_UNUSED, DUK_TAG_UNUSED, DUK_TAG_UNUSED, DUK_TAG_UNUSED }
@@ -347,6 +350,9 @@ typedef struct {
347350
#define DUK_TAG_BUFFER 10
348351
#define DUK_TAG_MAX 10
349352

353+
#define DUK_TVAL_IS_VALID_TAG(tv) \
354+
(DUK_TVAL_GET_TAG((tv)) - DUK_TAG_MIN <= DUK_TAG_MAX - DUK_TAG_MIN)
355+
350356
/* DUK_TAG_NUMBER is intentionally first, as it is the default clause in code
351357
* to support the 8-byte representation. Further, it is a non-heap-allocated
352358
* type so it should come before DUK_TAG_STRING. Finally, it should not break

0 commit comments

Comments
 (0)