Skip to content

Commit 44adf4f

Browse files
committed
C backend: fix array out of bounds warning when compiled with -O2 or higher optimizations
- see github # 217 - warning: array subscript -1 is outside array bounds - caused by fbc's optimization of non-zero lower bound arrays - fbc calculates a kind of virtual pointer of where the array(0,..) element would be and then computes element addresses from that - this optimization in fbc saves some computations when accessing array elements but the translation to C is technically undefined behaviour - fixed (for now) by casting the address to the equivalent of intptr_t in C. (FB_DATATYPE_INTEGER).
1 parent d15165a commit 44adf4f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Version 1.08.0
133133
- fix __FB_EVAL__() incorrectly reading past the end of the expression, and report errors in expressions
134134
- C backend: switch to .text section after writing the exports to the C file in the explicit asm block. gcc can move sections around with optimizations and there is a change between 7.x and 8.x that causes issue with where the directive section is located
135135
- sf.net #917: optimize 'm += s' string concatenations to fix the long compile times in the gcc backend (which makes heavy use of string building).
136+
- github #217: C backend, fix gcc array out of bounds warning when compiled with -O2 or higher optimizations and accessing non-zero lower bound fixed length string arrays
136137

137138

138139
Version 1.07.0

src/compiler/ir-hlc.bas

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,8 +2678,23 @@ private function exprNewVREG _
26782678
l = exprNewUOP( AST_OP_ADDROF, l )
26792679
end if
26802680
if( have_offset ) then
2681-
'' Cast to ubyte ptr to work around C's pointer arithmetic
2682-
l = exprNewCAST( typeAddrOf( FB_DATATYPE_UBYTE ), NULL, l )
2681+
if( is_c_array ) then
2682+
'' Cast to intptr_t to work around gcc out side of array bounds
2683+
'' warnings if we are casting from FBSTRING array to pointer
2684+
'' fbc uses a kind of virtual pointer for the an array's (0,..)
2685+
'' index; technically this is undefinded behaviour in C and is
2686+
'' impossible to cast away even when using pointer only casts
2687+
'' in the same expression. Some gcc optimizations cause a
2688+
'' a warning when setting a pointer for the array's virtual
2689+
'' index location. To fix this for compliant C code, would
2690+
'' need to rewrite the array descriptor to contain only the
2691+
'' offset value from actual memory pointer and compute the
2692+
'' array access fully on each array element access.
2693+
l = exprNewCAST( FB_DATATYPE_INTEGER, NULL, l )
2694+
else
2695+
'' Cast to ubyte ptr to work around C's pointer arithmetic
2696+
l = exprNewCAST( typeAddrOf( FB_DATATYPE_UBYTE ), NULL, l )
2697+
end if
26832698
if( vreg->vidx <> NULL ) then
26842699
l = exprNewBOP( AST_OP_ADD, l, exprNewVREG( vreg->vidx ) )
26852700
end if

0 commit comments

Comments
 (0)