Recent

Author Topic: [Solved] Need warning about missing bool result  (Read 425 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
[Solved] Need warning about missing bool result
« on: November 26, 2022, 10:31:02 am »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. program test_bool_res;
  3.  
  4. function AskTest: boolean;
  5. begin
  6.   if Random(2)=0 then exit;
  7.  
  8.   writeln('body');
  9.  
  10.   Result:= true;
  11. end;
  12.  
  13. begin
  14.   if AskTest then writeln('ok') else writeln('no');
  15. end.
  16.  
Can FPC show the warning about missing bool result when I do "exit" and not "exit(false)" at line 6?
« Last Edit: November 26, 2022, 10:08:19 pm by AlexTP »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1028
Re: Need warning about missing bool result
« Reply #1 on: November 26, 2022, 10:44:15 am »
If you compile with -Oodfa (enabled data flow analysis), the compiler will print a warning that the function result is not initialised at that point.

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: Need warning about missing bool result
« Reply #2 on: November 26, 2022, 10:57:02 am »
Great!
This test shows the DFA fails here, false warning.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. program test_bool_res;
  3.  
  4. function GetItem(out S: UnicodeString): boolean;
  5. const
  6.   cnt: integer = 0;
  7. begin
  8.   inc(cnt);
  9.   Result:= cnt<2;
  10.   S:= 'aa';
  11. end;
  12.  
  13. var
  14.   S: UnicodeString;
  15. begin
  16.   while GetItem(S) do
  17.     writeln('item: ', S);
  18. end.
  19.  

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1028
Re: Need warning about missing bool result
« Reply #3 on: November 26, 2022, 11:02:06 am »
That bug has been fixed in trunk already.

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: Need warning about missing bool result
« Reply #4 on: November 26, 2022, 11:44:11 am »
Great.
I am fighting with these 2 false positives of DFA (variable result not inited).
Did not yet make simple example.

1. function api_ed_attr
2. function api_msg_box_ex

Here:
https://github.com/Alexey-T/CudaText/blob/master/app/formmain_py_api.inc


« Last Edit: November 26, 2022, 11:46:13 am by AlexTP »

Thaddy

  • Hero Member
  • *****
  • Posts: 12543
Re: Need warning about missing bool result
« Reply #5 on: November 26, 2022, 12:11:18 pm »
https://www.freepascal.org/docs-html/rtl/system/exit.html
Why should you want a warning? Exit is not a good idea anyway.
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: Need warning about missing bool result
« Reply #6 on: November 26, 2022, 12:19:14 pm »
@Thaddy
I don't want a warning, bug DFA analysis gives the warning that 'variable result not initialized'. It is false positive as  I see.

For these cases
1. function api_ed_attr
2. function api_msg_box_ex

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: Need warning about missing bool result
« Reply #7 on: November 26, 2022, 01:06:51 pm »
@Jonas,
can you tell why DFA gives warning here?

Quote
test_bool_res.pas(11,3) Hint: Local variable "res" of a managed type does not seem to be initialized

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1028
Re: Need warning about missing bool result
« Reply #8 on: November 26, 2022, 01:18:06 pm »
That's also fixed on trunk.

Unrelated: using fillchar on managed types (as you do in proc_appvariant) is always very dangerous, and wrong in your program. The reason is that with the result variable this can result in memory leaks in case the compiler passes a temp that was already used before and still contains valid data as result (because the Items field won't be finalized in that case). If you want to intiialize it in a safe way, use "result := default(TAppVariant);".

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: Need warning about missing bool result
« Reply #9 on: November 26, 2022, 01:57:18 pm »
Fixed, avoided FillChar here, thanks.

 

TinyPortal © 2005-2018