Menu

#377 M68K/BCPL: toCStr is broken

m68k-amiga
closed-fixed
Shell/DOS (109)
5
2013-02-18
2012-02-26
Mark K
No

I compared in the AROS "toCStr" implementation with the corresponding routine in the Kickstart 1.3 ROM ($FF4EDA).

This routine should convert a BCPL-type string to C string in-place. Call with BPTR to BCPL string in D1. Returns a pointer to C string (i.e. the BPTR shifted left by 2) in D1.

The AROS implementation is broken:
- The conversion is supposed to be in-place, but the AROS version just returns a pointer to the first character of the BCPL string (i.e., the byte following the length byte). That means the C-type string starts at an odd address, which is a bad idea if BCPL programs try to work with the string...
- It doesn't ensure there's a terminating 0 byte, so the resulting C string might have random garbage appended to it. Without converting in-place, there's no space to put the terminating 0.

Here's a suggested fixed version, completely untested. (The Kickstart code uses A3 as a scratch register. It returns with A3 pointing to the byte following the terminating 0, but that's probably not used by anything.)

BCPL toCStr /* -80, char *, @string */
lsl.l #2,%d1 ;We return a pointer to the string
move.l %d1,%d0
moveq #0,%d2
move.b %a0(@d0),%d2 ;Number of bytes to copy in d2
beq.b EmptyString ;If string is empty there's nothing to do - the BCPL 0 length byte serves as the C terminating 0
subq.w #1,%d2 ;Minus 1 for dbf
Loop:
addq.l #1,%d0 ;Point to first character
move.b %a0(@d0),%a0(@d0, -1) ;Sorry, not sure of the correct syntax for this weird assembler!
dbf %d2,Loop

clr.b %a0(@d0) ;Write the terminating 0 byte

EmptyString:
move.l %d1,%d0 ;Return pointer to the string
BRTS

Discussion

  • Jason S. McMullan

    Fixed in r44787 - please verify and close

     
  • Jason S. McMullan

    • status: open --> open-fixed
     
  • Krzysztof Smiechowicz

    • assigned_to: nobody --> ezrec
    • status: open-fixed --> closed-fixed
     

Log in to post a comment.

MongoDB Logo MongoDB