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:
@@ -28,6 +28,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
#include <akstdlib.h>
|
||||
|
||||
#include <akbasic/error.h>
|
||||
#include <akbasic/runtime.h>
|
||||
@@ -194,7 +195,11 @@ akerr_ErrorContext *akbasic_struct_to_string(akbasic_Runtime *obj, int typeindex
|
||||
PREPARE_ERROR(errctx);
|
||||
char rendered[AKBASIC_MAX_STRING_LENGTH];
|
||||
akbasic_StructType *type = NULL;
|
||||
const char *separator = NULL;
|
||||
size_t used = 0;
|
||||
size_t seplen = 0;
|
||||
size_t namelen = 0;
|
||||
size_t renderedlen = 0;
|
||||
int written = 0;
|
||||
int i = 0;
|
||||
|
||||
@@ -204,6 +209,20 @@ akerr_ErrorContext *akbasic_struct_to_string(akbasic_Runtime *obj, int typeindex
|
||||
AKBASIC_ERR_BOUNDS, "Structure type index %d is out of range", typeindex);
|
||||
type = &obj->structtypes.types[typeindex];
|
||||
|
||||
/*
|
||||
* These two stay on raw snprintf on purpose, and are two of the six such
|
||||
* sites left in src/. Both want truncation as an *answer*: `dest` and `len`
|
||||
* come from the caller, this renders a value for display, and a name too long
|
||||
* for the buffer should print short rather than fail the whole PRINT. The
|
||||
* second one reads the return value to detect it -- on overflow `used` lands
|
||||
* past `len`, which is exactly what makes the field loop below and the
|
||||
* closing ")" both skip.
|
||||
*
|
||||
* aksl_snprintf treats truncation as AKERR_OUTOFBOUNDS and its `count` is 0
|
||||
* on that path, so it can express neither the tolerance nor the detection.
|
||||
* libakstdlib #34 is the issue that would give `count` the required length
|
||||
* back; until it lands there is nothing to convert these to.
|
||||
*/
|
||||
if ( depth >= AKBASIC_MAX_STRUCT_DEPTH ) {
|
||||
snprintf(dest, len, "%s(...)", type->name);
|
||||
SUCCEED_RETURN(errctx);
|
||||
@@ -222,7 +241,7 @@ akerr_ErrorContext *akbasic_struct_to_string(akbasic_Runtime *obj, int typeindex
|
||||
break;
|
||||
case AKBASIC_FIELD_POINTER:
|
||||
if ( slot->structbase == NULL ) {
|
||||
snprintf(rendered, sizeof(rendered), "NOTHING");
|
||||
PASS(errctx, aksl_snprintf(&written, rendered, sizeof(rendered), "NOTHING"));
|
||||
} else {
|
||||
PASS(errctx, akbasic_struct_to_string(obj, slot->structtype, slot->structbase,
|
||||
depth + 1, rendered, sizeof(rendered)));
|
||||
@@ -232,15 +251,24 @@ akerr_ErrorContext *akbasic_struct_to_string(akbasic_Runtime *obj, int typeindex
|
||||
PASS(errctx, akbasic_value_to_string(slot, rendered, sizeof(rendered)));
|
||||
break;
|
||||
}
|
||||
written = snprintf(dest + used, len - used, "%s%s=%s",
|
||||
(i == 0 ? "" : ", "), field->name, rendered);
|
||||
if ( written < 0 || (size_t)written >= len - used ) {
|
||||
separator = (i == 0 ? "" : ", ");
|
||||
PASS(errctx, aksl_strlen(separator, &seplen));
|
||||
PASS(errctx, aksl_strlen(field->name, &namelen));
|
||||
PASS(errctx, aksl_strlen(rendered, &renderedlen));
|
||||
/*
|
||||
* A render that does not fit ends the list rather than failing it, so the
|
||||
* fit is decided here -- aksl_snprintf treats truncation as an error, and
|
||||
* the one byte counted beyond the three lengths is the `=`.
|
||||
*/
|
||||
if ( used + seplen + namelen + 1 + renderedlen >= len ) {
|
||||
break;
|
||||
}
|
||||
PASS(errctx, aksl_snprintf(&written, dest + used, len - used, "%s%s=%s",
|
||||
separator, field->name, rendered));
|
||||
used += (size_t)written;
|
||||
}
|
||||
if ( used + 1 < len ) {
|
||||
snprintf(dest + used, len - used, ")");
|
||||
PASS(errctx, aksl_snprintf(&written, dest + used, len - used, ")"));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user