Port onto libakstdlib 2b79aca and convert the eight bool predicates
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2% bypassed, against 86.4% on the same tree before this. The submodule bump 669b2b3 -> 2b79aca needed no source change of its own: the release is drop-in for what akbasic already used. Seven of the eight sites the earlier port left on raw libc change their own signature rather than swallowing an error, per andrew's ruling on libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end, peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's scroll/newline/putchar_at/echo_line/edit_key chain all return an akerr_ErrorContext * and hand the answer back through an out parameter. is_waiting_for and is_waiting_for_any are a public header change; every call site that used one as a term in a condition hoists it into a statement first. verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the comparator's signature, so there is no out parameter to report through -- which is what libakstdlib#38 concluded. It carries a comment saying so and saying why the bypass is safe there. Six snprintf sites stay raw because they want truncation as an answer rather than an error, and aksl_snprintf cannot express that until libakstdlib#34 hands the required length back. Each of the six says so at the site. Two of them, in host.c, are a latent defect rather than a decision: a host type name over 31 characters truncates silently and two sharing a prefix then collide, where structtype.c refuses the same case. DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the PASS in it returned past CLEANUP, so a scan error left the file open. Hoisting the loop into its own helper to convert fgets fixes it. Refs libakstdlib#26, libakstdlib#38 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
68
src/value.c
68
src/value.c
@@ -13,6 +13,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
#include <akstdlib.h>
|
||||
|
||||
#include <akbasic/error.h>
|
||||
#include <akbasic/value.h>
|
||||
@@ -119,13 +120,17 @@ static akerr_ErrorContext *require_numeric(akbasic_Value *self, akbasic_Value *r
|
||||
static akerr_ErrorContext *set_string(akbasic_Value *dest, const char *src)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
size_t srclen = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (strlen(src) < AKBASIC_MAX_STRING_LENGTH),
|
||||
PASS(errctx, aksl_strlen(src, &srclen));
|
||||
FAIL_ZERO_RETURN(errctx, (srclen < AKBASIC_MAX_STRING_LENGTH),
|
||||
AKBASIC_ERR_VALUE,
|
||||
"String result of %zu characters exceeds the %d character limit",
|
||||
strlen(src), AKBASIC_MAX_STRING_LENGTH - 1);
|
||||
strncpy(dest->stringval, src, AKBASIC_MAX_STRING_LENGTH - 1);
|
||||
dest->stringval[AKBASIC_MAX_STRING_LENGTH - 1] = '\0';
|
||||
srclen, AKBASIC_MAX_STRING_LENGTH - 1);
|
||||
/* aksl_strcpy always terminates and refuses rather than truncates, which is
|
||||
the behaviour this site already wanted. The length check above stays
|
||||
because it names the limit in the message. */
|
||||
PASS(errctx, aksl_strcpy(dest->stringval, sizeof(dest->stringval), src));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -134,7 +139,7 @@ akerr_ErrorContext *akbasic_valuepool_init(akbasic_ValuePool *obj)
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL pool in init");
|
||||
memset(obj, 0, sizeof(*obj));
|
||||
PASS(errctx, aksl_memset(obj, 0, sizeof(*obj)));
|
||||
obj->next = 0;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -201,7 +206,7 @@ akerr_ErrorContext *akbasic_value_clone(akbasic_Value *self, akbasic_Value *dest
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
dest->valuetype = self->valuetype;
|
||||
memcpy(dest->stringval, self->stringval, sizeof(dest->stringval));
|
||||
PASS(errctx, aksl_memcpy(dest->stringval, self->stringval, sizeof(dest->stringval)));
|
||||
dest->intval = self->intval;
|
||||
dest->floatval = self->floatval;
|
||||
dest->boolvalue = self->boolvalue;
|
||||
@@ -223,6 +228,7 @@ akerr_ErrorContext *akbasic_value_clone(akbasic_Value *self, akbasic_Value *dest
|
||||
akerr_ErrorContext *akbasic_value_to_string(akbasic_Value *self, char *dest, size_t len)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int written = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL value in to_string");
|
||||
FAIL_ZERO_RETURN(errctx, (dest != NULL), AKERR_NULLPOINTER, "NULL destination in to_string");
|
||||
@@ -230,20 +236,22 @@ akerr_ErrorContext *akbasic_value_to_string(akbasic_Value *self, char *dest, siz
|
||||
|
||||
switch ( self->valuetype ) {
|
||||
case AKBASIC_TYPE_STRING:
|
||||
snprintf(dest, len, "%s", self->stringval);
|
||||
PASS(errctx, aksl_snprintf(&written, dest, len, "%s", self->stringval));
|
||||
break;
|
||||
case AKBASIC_TYPE_INTEGER:
|
||||
snprintf(dest, len, "%" PRId64, self->intval);
|
||||
PASS(errctx, aksl_snprintf(&written, dest, len, "%" PRId64, self->intval));
|
||||
break;
|
||||
case AKBASIC_TYPE_FLOAT:
|
||||
snprintf(dest, len, "%f", self->floatval);
|
||||
PASS(errctx, aksl_snprintf(&written, dest, len, "%f", self->floatval));
|
||||
break;
|
||||
case AKBASIC_TYPE_BOOLEAN:
|
||||
/* Go's %t, which is "true"/"false" and not the numeric -1/0. */
|
||||
snprintf(dest, len, "%s", (self->boolvalue == AKBASIC_TRUE ? "true" : "false"));
|
||||
PASS(errctx, aksl_snprintf(&written, dest, len, "%s",
|
||||
(self->boolvalue == AKBASIC_TRUE ? "true" : "false")));
|
||||
break;
|
||||
default:
|
||||
snprintf(dest, len, "(UNDEFINED STRING REPRESENTATION FOR %d)", (int)self->valuetype);
|
||||
PASS(errctx, aksl_snprintf(&written, dest, len, "(UNDEFINED STRING REPRESENTATION FOR %d)",
|
||||
(int)self->valuetype));
|
||||
break;
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
@@ -486,6 +494,7 @@ akerr_ErrorContext *akbasic_value_math_plus(akbasic_Value *self, akbasic_Value *
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Value *out = NULL;
|
||||
char buf[AKBASIC_MAX_STRING_LENGTH * 2];
|
||||
int written = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (self != NULL), AKERR_NULLPOINTER, "NULL value in math plus");
|
||||
FAIL_ZERO_RETURN(errctx, (rval != NULL), AKERR_NULLPOINTER, "nil rval");
|
||||
@@ -511,13 +520,16 @@ akerr_ErrorContext *akbasic_value_math_plus(akbasic_Value *self, akbasic_Value *
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
out->floatval = self->floatval + rval_as_float(rval);
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_STRING && rval->valuetype == AKBASIC_TYPE_STRING ) {
|
||||
snprintf(buf, sizeof(buf), "%s%s", self->stringval, rval->stringval);
|
||||
PASS(errctx, aksl_snprintf(&written, buf, sizeof(buf), "%s%s",
|
||||
self->stringval, rval->stringval));
|
||||
PASS(errctx, set_string(out, buf));
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_STRING && rval->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
snprintf(buf, sizeof(buf), "%s%" PRId64, self->stringval, rval->intval);
|
||||
PASS(errctx, aksl_snprintf(&written, buf, sizeof(buf), "%s%" PRId64,
|
||||
self->stringval, rval->intval));
|
||||
PASS(errctx, set_string(out, buf));
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_STRING && rval->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
snprintf(buf, sizeof(buf), "%s%f", self->stringval, rval->floatval);
|
||||
PASS(errctx, aksl_snprintf(&written, buf, sizeof(buf), "%s%f",
|
||||
self->stringval, rval->floatval));
|
||||
PASS(errctx, set_string(out, buf));
|
||||
} else {
|
||||
FAIL_RETURN(errctx, AKBASIC_ERR_TYPE, "Invalid arithmetic operation");
|
||||
@@ -586,14 +598,14 @@ akerr_ErrorContext *akbasic_value_math_multiply(akbasic_Value *self, akbasic_Val
|
||||
*/
|
||||
FAIL_NONZERO_RETURN(errctx, (rval->intval < 0), AKBASIC_ERR_VALUE,
|
||||
"String multiplier %" PRId64 " must not be negative", rval->intval);
|
||||
srclen = strlen((*dest)->stringval);
|
||||
PASS(errctx, aksl_strlen((*dest)->stringval, &srclen));
|
||||
FAIL_NONZERO_RETURN(errctx,
|
||||
(srclen != 0 && (uint64_t)rval->intval > (AKBASIC_MAX_STRING_LENGTH - 1) / srclen),
|
||||
AKBASIC_ERR_VALUE,
|
||||
"Repeated string of %zu x %" PRId64 " characters exceeds the %d character limit",
|
||||
srclen, rval->intval, AKBASIC_MAX_STRING_LENGTH - 1);
|
||||
for ( i = 0; i < rval->intval; i++ ) {
|
||||
memcpy(buf + offset, (*dest)->stringval, srclen);
|
||||
PASS(errctx, aksl_memcpy(buf + offset, (*dest)->stringval, srclen));
|
||||
offset += srclen;
|
||||
}
|
||||
buf[offset] = '\0';
|
||||
@@ -619,6 +631,7 @@ akerr_ErrorContext *akbasic_value_math_multiply(akbasic_Value *self, akbasic_Val
|
||||
akerr_ErrorContext *akbasic_value_less_than(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
@@ -626,7 +639,8 @@ akerr_ErrorContext *akbasic_value_less_than(akbasic_Value *self, akbasic_Value *
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, self->floatval < rval_as_float(rval)));
|
||||
} else {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, strcmp(self->stringval, rval->stringval) < 0));
|
||||
PASS(errctx, aksl_strcmp(self->stringval, rval->stringval, &cmp));
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, cmp < 0));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -634,6 +648,7 @@ akerr_ErrorContext *akbasic_value_less_than(akbasic_Value *self, akbasic_Value *
|
||||
akerr_ErrorContext *akbasic_value_less_than_equal(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
@@ -641,7 +656,8 @@ akerr_ErrorContext *akbasic_value_less_than_equal(akbasic_Value *self, akbasic_V
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, self->floatval <= rval_as_float(rval)));
|
||||
} else {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, strcmp(self->stringval, rval->stringval) <= 0));
|
||||
PASS(errctx, aksl_strcmp(self->stringval, rval->stringval, &cmp));
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, cmp <= 0));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -649,6 +665,7 @@ akerr_ErrorContext *akbasic_value_less_than_equal(akbasic_Value *self, akbasic_V
|
||||
akerr_ErrorContext *akbasic_value_greater_than(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
@@ -656,7 +673,8 @@ akerr_ErrorContext *akbasic_value_greater_than(akbasic_Value *self, akbasic_Valu
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, self->floatval > rval_as_float(rval)));
|
||||
} else {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, strcmp(self->stringval, rval->stringval) > 0));
|
||||
PASS(errctx, aksl_strcmp(self->stringval, rval->stringval, &cmp));
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, cmp > 0));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -664,6 +682,7 @@ akerr_ErrorContext *akbasic_value_greater_than(akbasic_Value *self, akbasic_Valu
|
||||
akerr_ErrorContext *akbasic_value_greater_than_equal(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
@@ -671,7 +690,8 @@ akerr_ErrorContext *akbasic_value_greater_than_equal(akbasic_Value *self, akbasi
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, self->floatval >= rval_as_float(rval)));
|
||||
} else {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, strcmp(self->stringval, rval->stringval) >= 0));
|
||||
PASS(errctx, aksl_strcmp(self->stringval, rval->stringval, &cmp));
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, cmp >= 0));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -679,6 +699,7 @@ akerr_ErrorContext *akbasic_value_greater_than_equal(akbasic_Value *self, akbasi
|
||||
akerr_ErrorContext *akbasic_value_is_equal(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
@@ -686,7 +707,8 @@ akerr_ErrorContext *akbasic_value_is_equal(akbasic_Value *self, akbasic_Value *r
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, self->floatval == rval_as_float(rval)));
|
||||
} else {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, strcmp(self->stringval, rval->stringval) == 0));
|
||||
PASS(errctx, aksl_strcmp(self->stringval, rval->stringval, &cmp));
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, cmp == 0));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -694,6 +716,7 @@ akerr_ErrorContext *akbasic_value_is_equal(akbasic_Value *self, akbasic_Value *r
|
||||
akerr_ErrorContext *akbasic_value_is_not_equal(akbasic_Value *self, akbasic_Value *rval, akbasic_Value *scratch, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
PASS(errctx, binary_prologue(self, rval, scratch, dest));
|
||||
if ( self->valuetype == AKBASIC_TYPE_INTEGER ) {
|
||||
@@ -701,7 +724,8 @@ akerr_ErrorContext *akbasic_value_is_not_equal(akbasic_Value *self, akbasic_Valu
|
||||
} else if ( self->valuetype == AKBASIC_TYPE_FLOAT ) {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, self->floatval != rval_as_float(rval)));
|
||||
} else {
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, strcmp(self->stringval, rval->stringval) != 0));
|
||||
PASS(errctx, aksl_strcmp(self->stringval, rval->stringval, &cmp));
|
||||
PASS(errctx, akbasic_value_set_bool(*dest, cmp != 0));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user