Browse Source

Fix (mostly) gcc -O4 related compile warnings

Unused variables in some option combinations.  Some of these were caused by
having -DDUK_OPT_DEBUG but no debug print options.

The GCC -O4 array subscript warning seems spurious and is probably caused by
GCC not being 100% sure 'count' is always valid.  The call sites for
duk__advance_chars() have a variety of argument values, including those
computed using shifts etc.  Add an explicit check for the valid range which
silences the GCC error.  The check also protects against regressions that
would cause an invalid count to be used.  With the check in place,
potentially memory unsafe behavior will now cause an internal error instead.

If the form DUK_ASSERT(x >= 0 && x < y) is used, gcc will cause a spurious
warning:

    duk_api_stack.c:149:33: error: assuming signed overflow does not occur when assuming that (X - c) > X is always false [-Werror=strict-overflow]
    duk_error.h:131:8: note: in definition of macro ‘DUK_ASSERT’

By breaking down the assert into two, i.e. DUK_ASSERT(x >= 0) and
DUK_ASSERT(x < y) the warning disappears.
v1.0-maintenance
Sami Vaarala 10 years ago
parent
commit
633d194476
  1. 15
      src/duk_api_stack.c
  2. 5
      src/duk_debug_heap.c
  3. 1
      src/duk_debug_hobject.c
  4. 4
      src/duk_heap_markandsweep.c
  5. 14
      src/duk_lexer.c

15
src/duk_api_stack.c

@ -120,7 +120,8 @@ DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index) {
}
}
DUK_ASSERT(index >= 0 && index < vs_size);
DUK_ASSERT(index >= 0);
DUK_ASSERT(index < vs_size);
return index;
}
@ -146,7 +147,8 @@ DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t i
}
}
DUK_ASSERT(index >= 0 && index < vs_size);
DUK_ASSERT(index >= 0);
DUK_ASSERT(index < vs_size);
return index;
invalid_index:
@ -176,7 +178,8 @@ DUK_INTERNAL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index) {
}
}
DUK_ASSERT(index >= 0 && index < vs_size);
DUK_ASSERT(index >= 0);
DUK_ASSERT(index < vs_size);
return thr->valstack_bottom + index;
}
@ -202,7 +205,8 @@ DUK_INTERNAL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index) {
}
}
DUK_ASSERT(index >= 0 && index < vs_size);
DUK_ASSERT(index >= 0);
DUK_ASSERT(index < vs_size);
return thr->valstack_bottom + index;
invalid_index:
@ -272,7 +276,8 @@ DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index) {
goto invalid_index;
}
}
DUK_ASSERT(index >= 0 && index <= vs_limit);
DUK_ASSERT(index >= 0);
DUK_ASSERT(index <= vs_limit);
if (index >= vs_size) {
/* Stack size increases or stays the same. Fill the new

5
src/duk_debug_heap.c

@ -40,6 +40,10 @@ DUK_LOCAL const char *duk__get_heap_type_string(duk_heaphdr *hdr) {
}
DUK_LOCAL void duk__dump_indented(duk_heaphdr *obj, int index) {
DUK_UNREF(obj);
DUK_UNREF(index);
DUK_UNREF(duk__get_heap_type_string);
#ifdef DUK_USE_REFERENCE_COUNTING
DUK_D(DUK_DPRINT(" [%ld]: %p %s (flags: 0x%08lx, ref: %ld) -> %!O",
(long) index,
@ -63,6 +67,7 @@ DUK_LOCAL void duk__dump_heaphdr_list(duk_heap *heap, duk_heaphdr *root, const c
duk_heaphdr *curr;
DUK_UNREF(heap);
DUK_UNREF(name);
count = 0;
curr = root;

1
src/duk_debug_hobject.c

@ -135,6 +135,7 @@ DUK_INTERNAL void duk_debug_dump_hobject(duk_hobject *obj) {
DUK_UNREF(str_empty);
DUK_UNREF(str_excl);
DUK_UNREF(duk__class_names);
DUK_D(DUK_DPRINT("=== hobject %p ===", (void *) obj));
if (!obj) {

4
src/duk_heap_markandsweep.c

@ -310,7 +310,6 @@ DUK_LOCAL void duk__mark_finalizable(duk_heap *heap) {
*/
DUK_LOCAL void duk__mark_finalize_list(duk_heap *heap) {
duk_hthread *thr;
duk_heaphdr *hdr;
#ifdef DUK_USE_DEBUG
duk_size_t count_finalize_list = 0;
@ -318,9 +317,6 @@ DUK_LOCAL void duk__mark_finalize_list(duk_heap *heap) {
DUK_DD(DUK_DDPRINT("duk__mark_finalize_list: %p", (void *) heap));
thr = duk__get_temp_hthread(heap);
DUK_ASSERT(thr != NULL);
hdr = heap->finalize_list;
while (hdr) {
duk__mark_heaphdr(heap, hdr);

14
src/duk_lexer.c

@ -291,6 +291,20 @@ DUK_LOCAL void duk__advance_chars(duk_lexer_ctx *lex_ctx, duk_small_int_t count)
DUK_ASSERT(count >= 0 && count <= DUK_LEXER_WINDOW_SIZE);
/* Without this check, gcc -O4 will complain the following for the
* first for-loop below:
*
* duk_lexer.c:301:19: error: array subscript is above array bounds [-Werror=array-bounds]
*
* Check for range explicitly; this also protects against legitimate
* internal errors and avoids memory unsafe behavior in such cases.
*/
if (DUK_UNLIKELY(!(count >= 0 && count <= DUK_LEXER_WINDOW_SIZE))) {
DUK_D(DUK_DPRINT("invalid count: %ld, should not happen", (long) count));
DUK_ERROR(lex_ctx->thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
return; /* never here */
}
if (count == 0) {
/* allowing zero count makes some special caller flows easier */
return;

Loading…
Cancel
Save