Nested IF-Statement bug - General Discussion and Assistance - CHDK Forum

Nested IF-Statement bug

  • 30 Replies
  • 11654 Views
Nested IF-Statement bug
« on: 26 / September / 2009, 06:10:40 »
Advertisements
Hallo,
recent days, rudi form german Forum (http://www.wirklemms.de/chdk/forum/viewtopic.php?t=1437) has created a patch, which works fine.
I have wrote an entry (http://chdk.kernreaktor.org/mantis/view.php?id=317) in the Bugtracker, but I can't upload anything.
Please update trunk.


Bug description:
It is a Bug in extended (nested) multiline IF-THEN-statement and ELSE-statement. The singleline IF-THEN-ELSE-statement is not affected.
If the interpreter have to skip script-lines between THEN and ELSE or ESLE and ENDIF, the following points are not considered:
1. the next TOKEN after THEN/ELSE don't process, it means: If a child multiline-IF-Statement direct after THEN/ELSE, the parent IF were closed by childs ENDIF
2. singleline-IF-Statements inside a IF are not identified, only multiline-IF-Statements

current Implementation
- fault processed multiline IF-THEN-ELSE-ENDIF statements for The next statement after THEN or ELSE
- fault processed multiline IF-THEN-[ELSE]-ENDIF statements for singleline IF-statements
- deficient handling for parse errors
- no handling for stackoverflow errors

proposed Implementation
- using singleline IF-statements in multiline IF-statements are possible
- handling of all skipped script-lines are correct
- better handle for parse errors
- add handle stackoverflow errors, stacksize is unchanged

corect IF-syntax:
- singleline statement IF-THEN-ELSE; no ENDIF expected
- multiline statement IF-THEN-[ELSE]-ENDIF; IF-THEN, ELSE, and ENDIF needs an own line


Important note for both implementations:
Don't use GOTO-statement in multiline IF-statements, which jump inside/outside of IF-ENDIF-block (get a stackoverflow).

examples for tests:

@title IF-bug test1
rem the valid output in this simple test is only "n<>1"
rem but in current implementation output: "n<>0", "n=1", "n<>1"
n=2
if n=1 then
  if n=0 then
        print "n=0"
  else
        print "n<>0"
  endif
  print "n=1"
else
        print "n<>1"  
endif
print "test1 finished"
print "press a key to end"
wait_click
end

-----------------------------------------------------

@title IF-bug test2
rem singleline IF in multiline IF-statement
rem in current implementation
rem the ELSE-ENDIF-section in multiline IF isn't found
rem and the script ends in the first run from "tryif"-section
rem without any error
a=0
b=0
gosub "tryif"
a=0
b=1
gosub "tryif"
a=1
b=0
gosub "tryif"
a=1
b=1
gosub "tryif"
print "test2 finished"
print "press a key to end"
wait_click
end

:tryif
print "tryif: a=", a ,"b=", b
if a=0 then
  print "[IF]"    
  if b=0 then print "a=0; b=0" else print "a=0; b=1"
else
  print "[ELSE]"
  if b=0 then print "a=1; b=0" else print "a=1; b=1"
endif
print "gosub end"
print "press a key to continue"
wait_click
return

-----------------------------------------------------

Thanks, CHDKLover
« Last Edit: 24 / October / 2009, 10:57:29 by CHDKLover »

*

Offline msl

  • *****
  • 1263
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: Nested IF-Statement bug
« Reply #1 on: 27 / September / 2009, 07:22:24 »
This is a very important patch!

The patch finally put an end indefinable pars errors in ubasic scripts by using bigger "if" constructions. As soon as possible the patch should be inserted in the trunk.
CHDK-DE:  CHDK-DE links

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: Nested IF-Statement bug
« Reply #2 on: 27 / September / 2009, 15:02:55 »
Thank you for the patch. uBasic code is over my head so I'll just have to take your word for it unless someone more capable wishes to look into this more closely...

Have you tested this code with a variety of existing scripts?

Does this in any way affect existing scripts (except for the odd, unlikely buggy script that may have worked by accident thanks to the buggy implementation)?

And would someone mind translating "besser neuer Fehler UBASIC_E_IF_STACK_EXHAUSTED" to English from this patch?

*

Offline whim

  • ******
  • 2013
  • A495/590/620/630 ixus70/115/220/230/300/870 S95
Re: Nested IF-Statement bug
« Reply #3 on: 27 / September / 2009, 15:22:00 »
@fudgey

i guess this is a suggestion to introduce a new error "UBASIC_E_IF_STACK_EXHAUSTED"
which is more helpfull to the user than merely reporting "UBASIC_E_UNKNOWN_ERROR"

FWIW, i agree with msl that this is a very important fix, it's difficult to see how previous
behaviour could have been 'exploited' by script writers.


cheers,

wim


Re: Nested IF-Statement bug
« Reply #4 on: 28 / September / 2009, 04:32:40 »
Hallo fudgey
Have you tested this code with a variety of existing scripts?
Does this in any way affect existing scripts (except for the odd, unlikely buggy script that may have worked by accident thanks to the buggy implementation)?
Yes, I have tested some existing scripts, but some need a little modification. Because, they violate one of the following conditions:
- singleline statement IF-THEN-ELSE; non ENDIF expected
- multiline statement IF-THEN-[ELSE]-ENDIF; IF-THEN, ELSE, and ENDIF needs an own line
AND
If scripts use the bug, they are behave differently.

CHDKLover

*

Offline msl

  • *****
  • 1263
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: Nested IF-Statement bug
« Reply #5 on: 29 / September / 2009, 06:23:36 »
Maybe we need to change some scripts.

But it is easier to change a few lines in a script, as looking for a mysterious pars error.

This patch is for safe working uBasic scripts absolutely necessary!
CHDK-DE:  CHDK-DE links

Re: Nested IF-Statement bug
« Reply #6 on: 01 / October / 2009, 12:07:09 »
Hallo,
have anybody test the patch? Why only few members advance an opinion?

CHDKLover
« Last Edit: 01 / October / 2009, 16:35:16 by CHDKLover »

*

Offline PhyrePhoX

  • *****
  • 2254
  • make RAW not WAR
    • PhyreWorX
Re: Nested IF-Statement bug
« Reply #7 on: 01 / October / 2009, 20:42:14 »
well if i dont see complaints here i will check in the patch during the next 7 days :)


*

Offline fe50

  • ******
  • 3107
  • IXUS50 & 860, SX10 Star WARs-Star RAWs
    • fe50
Re: Nested IF-Statement bug
« Reply #8 on: 02 / October / 2009, 06:08:06 »
well if i dont see complaints here i will check in the patch during the next 7 days :)
Yeah, will save us much work since there were lots of support requests based upon this issue...

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: Nested IF-Statement bug
« Reply #9 on: 03 / October / 2009, 10:48:55 »
Okay, I took a quick look at what our ubasic manual http://chdk.wikia.com/wiki/UBASIC/TutorialScratchpad says about if-then-else:

1) A single line if doesn't have an endif
2) It clearly states that multiline if statements are buggy (in the Fingalo extension section). The given example structure has if-then, else, endif all three on separate lines.

Breaking backwards compatibility is of course troublesome, but given the facts I agree that it should be done.


Please post here links faulty scripts you find, it's better to fix them before we break ubasic for them... Also, IMO this change calls for a louder than usual announcement when it hits the autobuilds.

 

Related Topics