Skip to content

Commit 78e9cc1

Browse files
authored
Correctly handle 0 length strings in conio (#69)
* Correctly handle 0 length strings in conio * Fix issue where we should still update the current x,y even for 0-length strings. * Fix issue where we should still update the current x,y even for 0-length strings. * Remove now redundant check for length=0 on cputsxy invocation. * cputsxy documentation cleanup.
1 parent c796803 commit 78e9cc1

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

include/mega65/conio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,8 @@ void fastcall cputs(const unsigned char* s);
914914
* @param y The Y coordinate where string will be printed
915915
* @param s An array of screen codes to print. Must have non-zero length.
916916
* @remarks This function works with screen codes only. To output ordinary
917-
* @warning Undefined behavior if `s` has zero length.
917+
* ASCII/PETSCII strings, use the "pcputsxy" macro. No pointer check is
918+
* performed. If s is null or invalid, behavior is undefined.
918919
*/
919920
void cputsxy(unsigned char x, unsigned char y, const unsigned char* s);
920921

@@ -946,7 +947,6 @@ void cputcxy(unsigned char x, unsigned char y, unsigned char c);
946947
* @param y The Y coordinate where character will be printed
947948
* @param count The number of characters to output. Must be larger than zero.
948949
* @param c The screen code of the characters to print
949-
* @warning Undefined behavior if `count` is zero.
950950
*/
951951
void cputncxy(
952952
unsigned char x, unsigned char y, unsigned char count, unsigned char c);

src/conio.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,11 @@ void cputs(const unsigned char* s)
571571
void cputsxy(unsigned char x, unsigned char y, const unsigned char* s)
572572
{
573573
const unsigned char len = (unsigned char)strlen((const char*)s);
574-
const unsigned int offset = (y * (unsigned int)g_curScreenW) + x;
575-
lcopy((unsigned long)s, SCREEN_RAM_BASE + offset, len);
576-
lfill(COLOR_RAM_BASE + offset, g_curTextColor, len);
574+
if (len > 0) {
575+
const unsigned int offset = (y * (unsigned int)g_curScreenW) + x;
576+
lcopy((unsigned long)s, SCREEN_RAM_BASE + offset, len);
577+
lfill(COLOR_RAM_BASE + offset, g_curTextColor, len);
578+
}
577579
g_curY = y + ((x + len) / g_curScreenW);
578580
g_curX = (x + len) % g_curScreenW;
579581
}
@@ -590,9 +592,11 @@ void cputcxy(unsigned char x, unsigned char y, unsigned char c)
590592
void cputncxy(
591593
unsigned char x, unsigned char y, unsigned char count, unsigned char c)
592594
{
593-
const unsigned int offset = (y * (unsigned int)g_curScreenW) + x;
594-
lfill(SCREEN_RAM_BASE + offset, c, count);
595-
lfill(COLOR_RAM_BASE + offset, g_curTextColor, count);
595+
if (count > 0) {
596+
const unsigned int offset = (y * (unsigned int)g_curScreenW) + x;
597+
lfill(SCREEN_RAM_BASE + offset, c, count);
598+
lfill(COLOR_RAM_BASE + offset, g_curTextColor, count);
599+
}
596600
g_curY = y + ((x + count) / g_curScreenW);
597601
g_curX = (x + count) % g_curScreenW;
598602
}
@@ -714,8 +718,7 @@ unsigned char cinput(
714718
}
715719

716720
while (1) {
717-
if (strlen(buffer) != 0)
718-
cputsxy(sx, sy, buffer);
721+
cputsxy(sx, sy, buffer);
719722
blink(1);
720723
cputc(224);
721724
blink(0);

0 commit comments

Comments
 (0)