From 5b3111cac05098271f0cc6dd7baac5f09012f1f5 Mon Sep 17 00:00:00 2001 From: Matthew Fearnley Date: Thu, 8 Apr 2021 22:35:44 +0100 Subject: [PATCH 1/2] Show warnings when counter variable in FOR loop cannot exceed the constant limit value e.g. 'for i as ubyte = 0 to 255' --- src/compiler/error.bas | 3 ++- src/compiler/error.bi | 1 + src/compiler/parser-compound-for.bas | 40 ++++++++++++++++++++++++++++ tests/warnings/for-limit-too-big.bas | 16 +++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/warnings/for-limit-too-big.bas diff --git a/src/compiler/error.bas b/src/compiler/error.bas index 9fcfaa6956..0e8d0c4d84 100644 --- a/src/compiler/error.bas +++ b/src/compiler/error.bas @@ -86,7 +86,8 @@ declare function hMakeParamDesc _ ( /'FB_WARNINGMSG_RETURNTYPEMISMATCH '/ 0, @"Return type mismatch" ), _ ( /'FB_WARNINGMSG_CALLINGCONVMISMATCH '/ 0, @"Calling convention mismatch" ), _ ( /'FB_WARNINGMSG_ARGCNTMISMATCH '/ 0, @"Argument count mismatch" ), _ - ( /'FB_WARNINGMSG_SUFFIXIGNORED '/ 1, @"Suffix ignored" ) _ + ( /'FB_WARNINGMSG_SUFFIXIGNORED '/ 1, @"Suffix ignored" ), _ + ( /'FB_WARNINGMSG_FORENDTOOBIG '/ 1, @"FOR counter variable is unable to exceed limit value" ) _ } dim shared errorMsgs( 1 to FB_ERRMSGS-1 ) as const zstring ptr => _ diff --git a/src/compiler/error.bi b/src/compiler/error.bi index dd41f638aa..4b8556e1b1 100644 --- a/src/compiler/error.bi +++ b/src/compiler/error.bi @@ -377,6 +377,7 @@ enum FB_WARNINGMSG FB_WARNINGMSG_CALLINGCONVMISMATCH FB_WARNINGMSG_ARGCNTMISMATCH FB_WARNINGMSG_SUFFIXIGNORED + FB_WARNINGMSG_FORENDTOOBIG FB_WARNINGMSGS end enum diff --git a/src/compiler/parser-compound-for.bas b/src/compiler/parser-compound-for.bas index c16086c0b1..bf32ee261a 100644 --- a/src/compiler/parser-compound-for.bas +++ b/src/compiler/parser-compound-for.bas @@ -834,6 +834,46 @@ sub cForStmtBegin( ) hUdtFor( stk ) end if + if( stk->for.end.sym = NULL and stk->for.ispos.sym = NULL ) then + dim as integer toobig = 0 + if ( stk->for.ispos.value.i ) then + select case as const typeGetSizeType( stk->for.cnt.dtype ) + case FB_SIZETYPE_INT8 + toobig = (stk->for.end.value.i >= 127) + case FB_SIZETYPE_UINT8 + toobig = (stk->for.end.value.i >= 255) + case FB_SIZETYPE_INT16 + toobig = (stk->for.end.value.i >= 32767) + case FB_SIZETYPE_UINT16 + toobig = (stk->for.end.value.i >= 65535) + case FB_SIZETYPE_INT32 + toobig = (stk->for.end.value.i >= 2147483647) + case FB_SIZETYPE_UINT32 + toobig = (stk->for.end.value.i >= 4294967295) + case FB_SIZETYPE_INT64 + toobig = (culngint(stk->for.end.value.i) >= 9223372036854775807) + case FB_SIZETYPE_UINT64 + toobig = (culngint(stk->for.end.value.i) >= 18446744073709551615) + end select + else + select case as const typeGetSizeType( stk->for.cnt.dtype ) + case FB_SIZETYPE_UINT8, FB_SIZETYPE_UINT16, FB_SIZETYPE_UINT32, FB_SIZETYPE_UINT64 + toobig = (stk->for.end.value.i <= 0) + case FB_SIZETYPE_INT8 + toobig = (stk->for.end.value.i <= -128) + case FB_SIZETYPE_INT16 + toobig = (stk->for.end.value.i >= -32768) + case FB_SIZETYPE_INT32 + toobig = (stk->for.end.value.i >= -2147483648) + case FB_SIZETYPE_INT64 + toobig = (stk->for.end.value.i >= -9223372036854775808) + end select + end if + if( toobig ) then + errReportWarn( FB_WARNINGMSG_FORENDTOOBIG ) + end if + end if + '' if inic, endc and stepc are all constants, '' check if this branch is needed if( isconst = 3 ) then diff --git a/tests/warnings/for-limit-too-big.bas b/tests/warnings/for-limit-too-big.bas new file mode 100644 index 0000000000..2e35d0d22c --- /dev/null +++ b/tests/warnings/for-limit-too-big.bas @@ -0,0 +1,16 @@ +#macro testForLimits(T, L, U) +for i as T = 0 to U +next i +for i as T = 0 to L step -1 +next i +#endmacro + +testForLimits(ubyte, 0, 255) +testForLimits(ushort, 0, 65535) +testForLimits(ulong, 0, 4294967295) +testForLimits(ulongint, 0, 18446744073709551615) + +testForLimits(byte, -128, 127) +testForLimits(short, -32768, 32767) +testForLimits(long, -2147483648, 2147483647) +testForLimits(longint, -9223372036854775808, 9223372036854775807) From 6656e1af299a496c1b28f63f15e4a2ce7331b9bb Mon Sep 17 00:00:00 2001 From: coderJeff Date: Sat, 10 Apr 2021 20:03:07 -0400 Subject: [PATCH 2/2] tests/warnings: add the warning output files for the new tests on for loop limits --- tests/warnings/r/dos/for-limit-too-big.txt | 16 ++++++++++++++++ tests/warnings/r/linux-x86/for-limit-too-big.txt | 16 ++++++++++++++++ .../r/linux-x86_64/for-limit-too-big.txt | 16 ++++++++++++++++ tests/warnings/r/win32/for-limit-too-big.txt | 16 ++++++++++++++++ tests/warnings/r/win64/for-limit-too-big.txt | 16 ++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 tests/warnings/r/dos/for-limit-too-big.txt create mode 100644 tests/warnings/r/linux-x86/for-limit-too-big.txt create mode 100644 tests/warnings/r/linux-x86_64/for-limit-too-big.txt create mode 100644 tests/warnings/r/win32/for-limit-too-big.txt create mode 100644 tests/warnings/r/win64/for-limit-too-big.txt diff --git a/tests/warnings/r/dos/for-limit-too-big.txt b/tests/warnings/r/dos/for-limit-too-big.txt new file mode 100644 index 0000000000..e01826f7dd --- /dev/null +++ b/tests/warnings/r/dos/for-limit-too-big.txt @@ -0,0 +1,16 @@ + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value diff --git a/tests/warnings/r/linux-x86/for-limit-too-big.txt b/tests/warnings/r/linux-x86/for-limit-too-big.txt new file mode 100644 index 0000000000..e01826f7dd --- /dev/null +++ b/tests/warnings/r/linux-x86/for-limit-too-big.txt @@ -0,0 +1,16 @@ + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value diff --git a/tests/warnings/r/linux-x86_64/for-limit-too-big.txt b/tests/warnings/r/linux-x86_64/for-limit-too-big.txt new file mode 100644 index 0000000000..e01826f7dd --- /dev/null +++ b/tests/warnings/r/linux-x86_64/for-limit-too-big.txt @@ -0,0 +1,16 @@ + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value diff --git a/tests/warnings/r/win32/for-limit-too-big.txt b/tests/warnings/r/win32/for-limit-too-big.txt new file mode 100644 index 0000000000..e01826f7dd --- /dev/null +++ b/tests/warnings/r/win32/for-limit-too-big.txt @@ -0,0 +1,16 @@ + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value diff --git a/tests/warnings/r/win64/for-limit-too-big.txt b/tests/warnings/r/win64/for-limit-too-big.txt new file mode 100644 index 0000000000..e01826f7dd --- /dev/null +++ b/tests/warnings/r/win64/for-limit-too-big.txt @@ -0,0 +1,16 @@ + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value + FOR counter variable is unable to exceed limit value