Skip to content

Commit 7425e9c

Browse files
committed
Overflow check identifiers, numbers, and strings.
Identifiers are up to 31 characters, numbers 20, and strings 254.
1 parent 83730af commit 7425e9c

File tree

2 files changed

+123
-24
lines changed

2 files changed

+123
-24
lines changed

pl0c.c

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,16 @@ static void ident(void) {
459459
loop = 1;
460460
token[i] = raw[loc];
461461
i = i + 1;
462+
if (i == 32) {
463+
{
464+
error();
465+
;
466+
(void)fprintf(stdout, "identifier too long\n");
467+
;
468+
exit(1);
469+
;
470+
};
471+
};
462472
;
463473
};
464474
};
@@ -474,6 +484,16 @@ static void ident(void) {
474484
loop = 1;
475485
token[i] = raw[loc];
476486
i = i + 1;
487+
if (i == 32) {
488+
{
489+
error();
490+
;
491+
(void)fprintf(stdout, "identifier too long\n");
492+
;
493+
exit(1);
494+
;
495+
};
496+
};
477497
;
478498
};
479499
};
@@ -492,6 +512,16 @@ static void ident(void) {
492512
loop = 1;
493513
token[i] = raw[loc];
494514
i = i + 1;
515+
if (i == 32) {
516+
{
517+
error();
518+
;
519+
(void)fprintf(stdout, "identifier too long\n");
520+
;
521+
exit(1);
522+
;
523+
};
524+
};
495525
;
496526
};
497527
};
@@ -508,6 +538,16 @@ static void ident(void) {
508538
loop = 1;
509539
token[i] = raw[loc];
510540
i = i + 1;
541+
if (i == 32) {
542+
{
543+
error();
544+
;
545+
(void)fprintf(stdout, "identifier too long\n");
546+
;
547+
exit(1);
548+
;
549+
};
550+
};
511551
;
512552
};
513553
};
@@ -551,11 +591,11 @@ static void number(void) {
551591
loop = 1;
552592
token[i] = raw[loc];
553593
i = i + 1;
554-
if (i == 256) {
594+
if (i == 21) {
555595
{
556596
error();
557597
;
558-
(void)fprintf(stdout, "token too long\n");
598+
(void)fprintf(stdout, "number too long\n");
559599
;
560600
exit(1);
561601
;
@@ -613,8 +653,31 @@ static void string(void) {
613653
{
614654
token[i] = '\\';
615655
i = i + 1;
656+
if (i == 255) {
657+
{
658+
error();
659+
;
660+
(void)fprintf(stdout, "string too long\n");
661+
;
662+
exit(1);
663+
;
664+
;
665+
};
666+
};
616667
token[i] = raw[loc];
617668
i = i + 1;
669+
if (i == 255) {
670+
{
671+
error();
672+
;
673+
(void)fprintf(stdout, "number too long\n");
674+
;
675+
exit(1);
676+
;
677+
;
678+
};
679+
};
680+
;
618681
};
619682
};
620683
;
@@ -645,21 +708,22 @@ static void string(void) {
645708
{
646709
token[i] = raw[loc];
647710
i = i + 1;
711+
if (i == 255) {
712+
{
713+
error();
714+
;
715+
(void)fprintf(stdout, "string too long\n");
716+
;
717+
exit(1);
718+
;
719+
;
720+
};
721+
};
722+
;
648723
};
649724
};
650725
};
651726
};
652-
if (i > 256) {
653-
{
654-
error();
655-
;
656-
(void)fprintf(stdout, "string too long\n");
657-
;
658-
exit(1);
659-
;
660-
;
661-
};
662-
};
663727
if (loop != 0) {
664728
loc = loc + 1;
665729
};

pl0c.pl0

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,12 @@ begin
447447
loop := 1;
448448
token[i] := raw[loc];
449449
i := i + 1;
450+
if i = 32 then
451+
begin
452+
call error;
453+
writeStr 'identifier too long\n';
454+
exit 1
455+
end;
450456
end;
451457
end;
452458

@@ -459,6 +465,12 @@ begin
459465
loop := 1;
460466
token[i] := raw[loc];
461467
i := i + 1;
468+
if i = 32 then
469+
begin
470+
call error;
471+
writeStr 'identifier too long\n';
472+
exit 1
473+
end;
462474
end;
463475
end;
464476
end;
@@ -472,6 +484,12 @@ begin
472484
loop := 1;
473485
token[i] := raw[loc];
474486
i := i + 1;
487+
if i = 32 then
488+
begin
489+
call error;
490+
writeStr 'identifier too long\n';
491+
exit 1
492+
end;
475493
end;
476494
end;
477495
end;
@@ -483,6 +501,12 @@ begin
483501
loop := 1;
484502
token[i] := raw[loc];
485503
i := i + 1;
504+
if i = 32 then
505+
begin
506+
call error;
507+
writeStr 'identifier too long\n';
508+
exit 1
509+
end;
486510
end;
487511
end;
488512

@@ -524,10 +548,10 @@ begin
524548
loop := 1;
525549
token[i] := raw[loc];
526550
i := i + 1;
527-
if i = 256 then
551+
if i = 21 then
528552
begin
529553
call error;
530-
writeStr 'token too long\n';
554+
writeStr 'number too long\n';
531555
exit 1;
532556
end;
533557

@@ -582,8 +606,20 @@ begin
582606
{ XXX: sanity check }
583607
token[i] := '\\';
584608
i := i + 1;
609+
if i = 255 then
610+
begin
611+
call error;
612+
writeStr 'string too long\n';
613+
exit 1;
614+
end;
585615
token[i] := raw[loc];
586-
i := i + 1
616+
i := i + 1;
617+
if i = 255 then
618+
begin
619+
call error;
620+
writeStr 'number too long\n';
621+
exit 1;
622+
end;
587623
end;
588624
end
589625
else if raw[loc] = '\n' then
@@ -602,14 +638,13 @@ begin
602638
begin
603639
{ XXX: sanity check }
604640
token[i] := raw[loc];
605-
i := i + 1
606-
end;
607-
608-
if i > 256 then
609-
begin
610-
call error;
611-
writeStr 'string too long\n';
612-
exit 1;
641+
i := i + 1;
642+
if i = 255 then
643+
begin
644+
call error;
645+
writeStr 'string too long\n';
646+
exit 1;
647+
end;
613648
end;
614649

615650
if loop # 0 then

0 commit comments

Comments
 (0)