lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
41 stars 50 forks source link

Can a ManagedString contain a zero character? #385

Open martinwork opened 8 months ago

martinwork commented 8 months ago

Arising from support ticket https://support.microbit.org/helpdesk/tickets/67883 (private)

A MakeCode string containing a zero character becomes empty when it's converted to a DAL/CODAL ManagedString.

This project displays the string in the MakeCode simulator, but not in micro:bit. https://makecode.microbit.org/_bT89KTV9U2wA The support ticket is about putting 0 in a string to send to serial.

The ManagedString constructor used in the conversion empties strings that contain a zero character. ​https://github.com/microsoft/pxt-common-packages/blob/master/libs/core/pxt.h#L26 https://github.com/lancaster-university/codal-core/blob/master/source/types/ManagedString.cpp#L244 https://github.com/lancaster-university/microbit-dal/blob/master/source/types/ManagedString.cpp#L252

The constructor from a ManagedBuffer doesn't filter zeros. https://github.com/lancaster-university/codal-core/blob/master/source/types/ManagedString.cpp#L221

initString says "We assume the string is... null terminated", but doesn't really assume that. https://github.com/lancaster-university/codal-core/blob/master/source/types/ManagedString.cpp#L77

martinwork commented 8 months ago

@JohnVidler @finneyj

The ticket is about using MakeCode serial.writeString etc. to send a string containing a zero, created with text from char code(0), to communicate with a device.

There is a block for serial.writeBuffer, but creating the buffer in Javascript mode and filling the buffer is just less convenient. Of course, the mess can be hidden in a function, custom.ts or extension, but...

It still feels like a bug that the string created in MakeCode becomes empty. As far as I can see from the GitHub history this hasn't changed since the earliest DAL. I suppose if strings can contain zero, everything that accepts a string must deal with zero!