Skip to content

Commit 9675913

Browse files
fix: weird behaviours in LEFT$ when length is out of bounds
1 parent fb3dcd4 commit 9675913

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/ubasic.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ static char* printable_syntax(struct ubasic_ctx* ctx)
588588
const char* oldctx = ctx->ptr;
589589
if (tokenizer_token(ctx) != NUMBER && tokenizer_token(ctx) != HEXNUMBER && (*ctx->ptr == '"' || strchr(tokenizer_variable_name(ctx), '$'))) {
590590
ctx->ptr = oldctx;
591-
sprintf(buffer, "%s", str_expr(ctx));
591+
strlcpy(buffer, str_expr(ctx), MAX_STRINGLEN);
592592
strlcat(out, buffer, MAX_STRINGLEN);
593593
} else {
594594
ctx->ptr = oldctx;
@@ -632,7 +632,7 @@ static void print_statement(struct ubasic_ctx* ctx)
632632
accept(PRINT, ctx);
633633
const char* out = printable_syntax(ctx);
634634
if (out) {
635-
kprintf(out);
635+
putstring((console*)ctx->cons, out);
636636
}
637637
}
638638

@@ -1660,8 +1660,6 @@ void ubasic_set_string_variable(const char* var, const char* value, struct ubasi
16601660
error_set = true;
16611661
}
16621662

1663-
//kprintf("set string '%s' to '%s' %d\n", var, value, local);
1664-
16651663
if (!valid_string_var(var)) {
16661664
tokenizer_error_print(ctx, "Malformed variable name");
16671665
return;
@@ -2255,7 +2253,6 @@ int64_t ubasic_instr(struct ubasic_ctx* ctx)
22552253

22562254
char* ubasic_readstring(struct ubasic_ctx* ctx)
22572255
{
2258-
//kprintf("read string\n");
22592256
char* res = (char*)kmalloc(1024);
22602257
int ofs = 0;
22612258
*res = 0;
@@ -2270,7 +2267,6 @@ char* ubasic_readstring(struct ubasic_ctx* ctx)
22702267
break;
22712268
else
22722269
ofs++;
2273-
//kprintf("Got byte %d", res[ofs - 1]);
22742270
}
22752271
*(res+ofs) = 0;
22762272
char* ret = gc_strdup(res);
@@ -2363,8 +2359,16 @@ char* ubasic_left(struct ubasic_ctx* ctx)
23632359
PARAMS_GET_ITEM(BIP_STRING);
23642360
PARAMS_GET_ITEM(BIP_INT);
23652361
PARAMS_END("LEFT$");
2366-
if (intval > strlen(strval) || intval < 0)
2367-
intval = strlen(strval);
2362+
int64_t len = strlen(strval);
2363+
if (intval < 0) {
2364+
intval = 0;
2365+
}
2366+
if (len == 0 || intval == 0) {
2367+
return "";
2368+
}
2369+
if (intval > len) {
2370+
intval = len;
2371+
}
23682372
char* cut = gc_strdup(strval);
23692373
*(cut + intval) = 0;
23702374
return cut;
@@ -2381,6 +2385,9 @@ char* ubasic_mid(struct ubasic_ctx* ctx)
23812385
int64_t end = intval;
23822386
PARAMS_END("MID$");
23832387
int64_t len = strlen(strval);
2388+
if (len == 0) {
2389+
return "";
2390+
}
23842391
if (start > len) {
23852392
start = len;
23862393
}
@@ -2637,7 +2644,6 @@ const char* ubasic_get_string_variable(const char* var, struct ubasic_ctx* ctx)
26372644

26382645
for (j = 0; j < 2; j++)
26392646
{
2640-
//kprintf("Iter %d\n", j);
26412647
struct ub_var_string* cur = list[j];
26422648
for (; cur; cur = cur->next) {
26432649
if (!strcmp(var, cur->varname)) {

0 commit comments

Comments
 (0)