9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] Bug in rc
@ 2023-04-26 18:20 Yury Chumak
  2023-04-27  0:44 ` ori
  0 siblings, 1 reply; 5+ messages in thread
From: Yury Chumak @ 2023-04-26 18:20 UTC (permalink / raw)
  To: 9front

Hello all..

Noticed that rc returns error 'stack overflow' if code consists
'switch' construction with big amount of 'case' blocks. Steps to
reproduce:

1.Generate test script by next code:
#!/bin/rc
echo '#!/bin/rc' >test.rc
echo 'switch($*){' >>test.rc
for(i in `{seq 1 250}){
    echo ' case ' $i>>test.rc
    echo '    echo number ' $i>>test.rc
}
echo ' case *' >>test.rc
echo '    echo number none'>>test.rc
echo '}'>>test.rc

2. Run result script:
% chmod +x test.rc
% test.rc
./test.rc:497: token 248: yacc stack overflow

The 'switch' construction works normaly with number 'case' block less than 247.
In comparison linux's bash has no limitation to number elements in
'case' block (at least 500k tested).


-- 
Sphynkx

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9front] Bug in rc
  2023-04-26 18:20 [9front] Bug in rc Yury Chumak
@ 2023-04-27  0:44 ` ori
  2023-04-27  2:05   ` Yury Chumak
  2023-04-27  2:15   ` Anthony Martin
  0 siblings, 2 replies; 5+ messages in thread
From: ori @ 2023-04-27  0:44 UTC (permalink / raw)
  To: 9front

Quoth Yury Chumak <sfynkx@gmail.com>:
> Hello all..
> 
> Noticed that rc returns error 'stack overflow' if code consists
> 'switch' construction with big amount of 'case' blocks. Steps to
> reproduce:
> 
> 1.Generate test script by next code:
> #!/bin/rc
> echo '#!/bin/rc' >test.rc
> echo 'switch($*){' >>test.rc
> for(i in `{seq 1 250}){
>     echo ' case ' $i>>test.rc
>     echo '    echo number ' $i>>test.rc
> }
> echo ' case *' >>test.rc
> echo '    echo number none'>>test.rc
> echo '}'>>test.rc
> 
> 2. Run result script:
> % chmod +x test.rc
> % test.rc
> ./test.rc:497: token 248: yacc stack overflow
> 
> The 'switch' construction works normaly with number 'case' block less than 247.
> In comparison linux's bash has no limitation to number elements in
> 'case' block (at least 500k tested).
> 

Yes, the grammar for this is right recursive,
which means the full stack is collected before
it's reduced:

	body:	cmd
	|	cmdsan body		{$$=tree2(';', $1, $2);}

You can play around with refactoring it to be
left recursive, or maybe bringing in the C rewrite
of the parser from plan9port (ideally with tests).

But I'm not sure why it matters; you can hit a
limit with a huge switch, but it doesn't seem
like a limit for practical code.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9front] Bug in rc
  2023-04-27  0:44 ` ori
@ 2023-04-27  2:05   ` Yury Chumak
  2023-04-27  2:15   ` Anthony Martin
  1 sibling, 0 replies; 5+ messages in thread
From: Yury Chumak @ 2023-04-27  2:05 UTC (permalink / raw)
  To: 9front

Hitting limit is a half decision. In general, a strange decision is to
implement such non-limiting constructions using recursion. By the way,
in my real code there is a limit of 141 'case'-blocks, but there are
complex 'case' conditions in the blocks - apparently they are
additionally expanded.. So the test example is ideal at maximum, but
really the limit can be reached faster.

чт, 27 апр. 2023 г. в 03:46, <ori@eigenstate.org>:
>
> Quoth Yury Chumak <sfynkx@gmail.com>:
> > Hello all..
> >
> > Noticed that rc returns error 'stack overflow' if code consists
> > 'switch' construction with big amount of 'case' blocks. Steps to
> > reproduce:
> >
> > 1.Generate test script by next code:
> > #!/bin/rc
> > echo '#!/bin/rc' >test.rc
> > echo 'switch($*){' >>test.rc
> > for(i in `{seq 1 250}){
> >     echo ' case ' $i>>test.rc
> >     echo '    echo number ' $i>>test.rc
> > }
> > echo ' case *' >>test.rc
> > echo '    echo number none'>>test.rc
> > echo '}'>>test.rc
> >
> > 2. Run result script:
> > % chmod +x test.rc
> > % test.rc
> > ./test.rc:497: token 248: yacc stack overflow
> >
> > The 'switch' construction works normaly with number 'case' block less than 247.
> > In comparison linux's bash has no limitation to number elements in
> > 'case' block (at least 500k tested).
> >
>
> Yes, the grammar for this is right recursive,
> which means the full stack is collected before
> it's reduced:
>
>         body:   cmd
>         |       cmdsan body             {$$=tree2(';', $1, $2);}
>
> You can play around with refactoring it to be
> left recursive, or maybe bringing in the C rewrite
> of the parser from plan9port (ideally with tests).
>
> But I'm not sure why it matters; you can hit a
> limit with a huge switch, but it doesn't seem
> like a limit for practical code.
>


-- 
Sphynkx

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9front] Bug in rc
  2023-04-27  0:44 ` ori
  2023-04-27  2:05   ` Yury Chumak
@ 2023-04-27  2:15   ` Anthony Martin
  2023-04-27  3:09     ` Yury Chumak
  1 sibling, 1 reply; 5+ messages in thread
From: Anthony Martin @ 2023-04-27  2:15 UTC (permalink / raw)
  To: 9front

ori@eigenstate.org once said:
> Quoth Yury Chumak <sfynkx@gmail.com>:
> > Hello all..
> >
> > Noticed that rc returns error 'stack overflow' if code consists
> > 'switch' construction with big amount of 'case' blocks. Steps to
> > reproduce:
>
> [...]
>
> But I'm not sure why it matters; you can hit a
> limit with a huge switch, but it doesn't seem
> like a limit for practical code.

I agree with Ori. If you actually need a workaround,
increase YYMAXDEPTH in /sys/src/cmd/rc/rc.h and
recompile.

Cheers,
  Anthony

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9front] Bug in rc
  2023-04-27  2:15   ` Anthony Martin
@ 2023-04-27  3:09     ` Yury Chumak
  0 siblings, 0 replies; 5+ messages in thread
From: Yury Chumak @ 2023-04-27  3:09 UTC (permalink / raw)
  To: 9front

Thankx for advice, I'll see that code later. Anycase its not portable
decision.. By the way there is non-c decision also - to split huge
switch to some less ones =))

чт, 27 апр. 2023 г. в 05:37, Anthony Martin <ality@pbrane.org>:
>
> ori@eigenstate.org once said:
> > Quoth Yury Chumak <sfynkx@gmail.com>:
> > > Hello all..
> > >
> > > Noticed that rc returns error 'stack overflow' if code consists
> > > 'switch' construction with big amount of 'case' blocks. Steps to
> > > reproduce:
> >
> > [...]
> >
> > But I'm not sure why it matters; you can hit a
> > limit with a huge switch, but it doesn't seem
> > like a limit for practical code.
>
> I agree with Ori. If you actually need a workaround,
> increase YYMAXDEPTH in /sys/src/cmd/rc/rc.h and
> recompile.
>
> Cheers,
>   Anthony



-- 
Sphynkx

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-27  9:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-26 18:20 [9front] Bug in rc Yury Chumak
2023-04-27  0:44 ` ori
2023-04-27  2:05   ` Yury Chumak
2023-04-27  2:15   ` Anthony Martin
2023-04-27  3:09     ` Yury Chumak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).