I built from source the console only version today and Input “How old are you?”, ans fails with this output
How old are you? 4 malloc(): invalid size (unsorted)
Aborted
the cursor waits for an input but jitters like it keeps testing for an input like inkey would. Once I answer and enter I get that error. I just updated to Debian Trixie
Hi Barry,
Could you please give the exact steps and program you tried?
I was shy about reporting this because it seemed to obvious to get released.
```
#!/usr/local/bin/sbasic
input "How old are you? ", ans
print ans
```
I sent a email over to smallbasic@gmail.com with debugging info if that helps.
I tried your program on a Windows laptop and my Manjaro desktop. I could not reproduce it. The program you posted works fine. malloc(): invalid size (unsorted) indicates, that an element of an array was accessed that was not allocated by malloc(). I have to debug a little bit.
It’s related to src/platform/console/input_history.h
Try this change and run it again
void push(const char *input) {
std::string new_entry;
try {
new_entry = std::string(input);
} catch (const std::exception& e) {
fprintf(stderr, “\nCaught exception in push() %s\n”, e.what() );
return;
}
history_stack.push_back(new_entry);
current_index = ((int)history_stack.size());
}
Unfortunately after the input_history.h modification I received the same result / error. I put it back like it was.
I knew it was working on x86 systems so I talked to AI about the differences and compiler flags fixed it. Copied from AI:
When C/C++ code works fine on x86/x86_64 (Linux & Windows) but crashes with heap corruption on ARM64 (like your Debian Trixie container on Chromebook), the suspect is almost always char sign signedness.
The Architecture Catch
On x86 / x86_64: char is signed by default in GCC (-128 to 127).
On ARM / ARM64: char is unsigned by default in GCC (0 to 255).
On ARM64, if ch is negative (like -1 or -2 returned when no key is pressed or during terminal escape sequences), implicit casts to char* or calculations in dev_input_insert_char treat values as 255 (0xFF) instead of -1.
That causes strlen((char *)cstr) or string boundary checks to miscalculate array offsets, reading past the end of strings and overwriting malloc heap headers!
You don’t need to rewrite all the string logic. You can tell GCC on ARM64 to treat char as signed, matching x86 behavior exactly!
Reconfigure / rebuild with signed char forced
CXXFLAGS=“-g -O2 -fsigned-char” CFLAGS=“-g -O2 -fsigned-char” ./configure
End of AI
I’m still testing but so far everything is working as expected. Sorry for the long post!
This wasn’t a good fix, I sent an email with some code changes for your review.
I tested on a Raspberry Pi 3B with Raspberry Pi OS Trixi (64bit). Input did not crash but printed nonstop garbage. I realized that vt100_inputReadKey() returned 255 when no key was pressed. A fix was to change the following lines in vt100.cpp:
int getch() {
char c; --> signed char c
if (read(STDIN_FILENO, &c, 1) == 1) {
return (int)c;
}
return EOF;
}
uint32_t vt100_inputReadKey(void) {
char c = getch(); --> signed char c
After this changes everything works fine on the Pi so far.
After pulling down a fresh vt100.cpp and making your changes everything I’ve tested is working perfectly with one exception. If a program uses XDG-OPEN to open a web page when that browser is closed I still get the ;1R fragment after the console prompt. This edit stopped that from happening, you might know of a better way.
if (dev_get_millisecond_count() - startTime > 50) { → 100
Maybe my Chromebook is just slow. Thank you so much for your help.
most probably it will be fine to set the timeout to 100ms. I chose the 50ms without much thinking. Can you give me an example program? I would like to see if I also get this quite long timeout.
#!/usr/local/bin/sbasic
REM SmallBASIC
REM created: 11/01/2024
run(“xdg-open ‘https://radar.weather.gov/ridge/standard/KTBW_loop.gif’”)
@BarrySWTP: Chris merged my pull request. The singed char fix and increase if the timeout to 100ms are now online. I also added a fix, that INPUT doesn’t use 100% of one core of the CPU anymore. Thank you for reporting the bug.
@J7M Joerg, thanks for all your help and quick response.