GET$# can read past EOF#

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

GET$# can read past EOF#

Post by svein »

Edit: There's no bug, but missing EOF#F% in the inner loop.

GET$#F% (on all platforms) should return an empty string at EOF, and if it's not doing so it's a bug.
Appears to be a bug here.
Used program 1 to create the fault, only took 1 minute before M% started to go wild.
Used program 2 to find where the fault started, C% counted to 542.
Used program 3 to save the ascii codes around that point.
I have the file if by any chance you can't reproduce the error.

Svein

Edit: prog 2 and 3 updated

Code: Select all

    
      f$=@tmp$+"test.txt"
      REPEAT
        F%=OPENOUT f$
        IF F%=0 THEN ERROR 0,"can't create file"
        L%=RND(1000)
        FOR I%=0 TO L%
          BPUT#F%,RND(255)
        NEXT
        CLOSE#F%
        F%=OPENIN f$
        IF F%=0 THEN ERROR 0,"can't open file"
        N%=0 : M%=0
        REPEAT
          REPEAT
            a$=GET$#F%
            M%+=1
          UNTIL a$<>""
          N%+=1
        UNTIL EOF#F%
        CLOSE#F%
        PRINT L%,M%,N%
      UNTIL M%>L%
      REM N%=6 L%=548 M%=50851547

Code: Select all

   
          f$=@tmp$+"test.txt"
      REPEAT
        F%=OPENIN f$
        IF F%=0 THEN ERROR 0,"can't open file"
        N%=0 : M%=0
        C%=0
        REPEAT
          REPEAT
            a$=GET$#F%
            M%+=1
          UNTIL a$<>""
          N%+=1
          FOR B%=1 TO LEN(a$)
            PRINT ASCMID$(a$,B%);" ";
            C%+=1
          NEXT
        UNTIL EOF#F%
        CLOSE#F%
        PRINT L%,M%,N%
      UNTIL M%>L%
      REM C%=542

Code: Select all

   
       f$=@tmp$+"test.txt"
      F%=OPENIN f$
      IF F%=0 THEN ERROR 0,"can't open file"
      G%=OPENOUT (@tmp$+"ascii.txt")
      IF G%=0 THEN ERROR 0,"can't save"
      FOR A%=500 TO 600
        B%=BGET#F%
        a$=STR$A%+" "+STR$B%
        PRINT#G%,a$ : BPUT#G%,10
      NEXT
      CLOSE#F%
      CLOSE#G%

Code: Select all

500 214
501 87
502 158
503 123
504 138
505 224
506 159
507 95
508 28
509 10
510 170
511 95
512 214
513 168
514 31
515 155
516 228
517 211
518 29
519 153
520 236
521 86
522 105
523 191
524 54
525 211
526 52
527 70
528 118
529 77
530 36
531 140
532 111
533 184
534 38
535 21
536 122
537 120
538 216
539 174
540 108
541 105
542 57
543 38
544 217
545 110
546 87
547 82
548 105
549 249
550 231
551 99
552 5
553 203
554 153
555 120
556 35
557 229
558 180
559 127
560 32
561 155
562 230
563 121
564 91
565 78
566 52
567 254
568 180
569 25
570 34
571 115
572 62
573 180
574 250
575 79
576 78
577 173
578 231
579 98
580 100
581 248
582 145
583 30
584 96
585 31
586 34
587 229
588 231
589 113
590 115
591 79
592 138
593 189
594 184
595 142
596 121
597 148
598 82
599 105
600 223
Last edited by svein on Thu 10 Jan 2019, 12:05, edited 2 times in total.
guest

Re: GET$# can read past EOF#

Post by guest »

svein wrote: Mon 31 Dec 2018, 22:53I have the file if by any chance you can't reproduce the error.
Are you saying that you have a file which fails consistently every time you try to read it? If so I would certainly like a copy of that file.
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: GET$# can read past EOF#

Post by svein »

Yes the file fail every time on window 10, BBC4W and BBCsdl 32 bit basic.
I'll send you the file.
Svein
guest

Re: GET$# can read past EOF#

Post by guest »

svein wrote: Mon 31 Dec 2018, 23:30 I'll send you the file.
Thanks, but I don't properly understand what 'fault' you are reporting. In your program you have this loop:

Code: Select all

      REPEAT
        a$=GET$#F%
        M%+=1
      UNTIL a$<>""
Obviously if the file is already at EOF this is an 'infinite loop' (you are reading the file until something is returned, but nothing will ever be returned in that case) and M% will continue to be incremented indefinitely. What exactly is happening that you do not think should be happening?
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: GET$# can read past EOF#

Post by svein »

Hmm, maybe i just made a silly mistake.
The loop in program 1 looks ok though.
It goes thru several new files before locking.
I thought the loop would stop at UNTIL EOF#F%.

Code: Select all

        REPEAT
          REPEAT
            a$=GET$#F%
            M%+=1
          UNTIL a$<>""
          N%+=1
        UNTIL EOF#F%
If you listed the loop from program 2, it appears to lock at 542 bytes read, and the file is 549 bytes long. ?

Good night.
Svein
guest

Re: GET$# can read past EOF#

Post by guest »

svein wrote: Tue 01 Jan 2019, 00:12 The loop in program 1 looks ok though.
I've received the file you sent, but (here) it works exactly as I would expect: the program enters an 'infinite loop' as I described and has to be interrupted by pressing Escape. The value of M% depends on how long I wait before pressing Escape.
It goes thru several new files before locking.
That's to be expected because it will only enter the 'infinite loop' condition if the last two bytes of the file both happen to be 0, 10 or 13 (the delimiter characters for GET$#). In the file you sent, the last two bytes are 10 and 13 so this condition is met, but statistically if the file contains 'random' data you will only generate such a file with a proportion of (3/256)^2 which is approximately once every 7300 files.
it appears to lock at 542 bytes read, and the file is 549 bytes long. ?
You don't count the number of bytes read from the file, you count only the number of bytes in the strings returned by GET$#, so any 0, 10 or 13 characters in the file are never counted. Statistically you would expect a 549 bytes random file to contain 549*3/256 i.e. about 6 such bytes, so again the results are consistent with what theory would predict.

So I'm afraid I can't reproduce any fault here. Because you introduce random data into your test file the way the program behaves varies according to that data, and consequently you typically get different results each time you run the program. But they are nevertheless exactly the results I would expect to get.
guest

Re: GET$# can read past EOF#

Post by guest »

guest wrote: Tue 01 Jan 2019, 00:37So I'm afraid I can't reproduce any fault here.
Do you still believe there is a problem or can we call this 'case closed'?
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: GET$# can read past EOF#

Post by svein »

Sorry for delayed answer, yes case closed, my fault.
Svein