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:
111
src/structtype.c
111
src/structtype.c
@@ -23,6 +23,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
#include <akstdlib.h>
|
||||
|
||||
#include <akbasic/error.h>
|
||||
#include <akbasic/runtime.h>
|
||||
@@ -88,10 +89,22 @@ static const char *next_word(const char *cursor, char *dest, size_t len)
|
||||
return cursor;
|
||||
}
|
||||
|
||||
/** @brief Compare a word against a keyword. Verbs are case-insensitive here as everywhere. */
|
||||
static bool word_is(const char *word, const char *keyword)
|
||||
/**
|
||||
* @brief Compare a word against a keyword. Verbs are case-insensitive here as everywhere.
|
||||
*
|
||||
* The answer leaves through @p dest rather than the return value because the
|
||||
* comparison itself can fail, and a `bool` has nowhere to put that. See
|
||||
* libakstdlib #38.
|
||||
*/
|
||||
static akerr_ErrorContext *word_is(const char *word, const char *keyword, bool *dest)
|
||||
{
|
||||
return (strcmp(word, keyword) == 0);
|
||||
PREPARE_ERROR(errctx);
|
||||
int cmp = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (dest != NULL), AKERR_NULLPOINTER, "NULL dest in word_is");
|
||||
PASS(errctx, aksl_strcmp(word, keyword, &cmp));
|
||||
*dest = (cmp == 0);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,8 +116,9 @@ static bool word_is(const char *word, const char *keyword)
|
||||
static akerr_ErrorContext *type_from_suffix(const char *name, akbasic_Type *dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
size_t len = strlen(name);
|
||||
size_t len = 0;
|
||||
|
||||
PASS(errctx, aksl_strlen(name, &len));
|
||||
FAIL_ZERO_RETURN(errctx, (len >= 2), AKBASIC_ERR_SYNTAX,
|
||||
"Field name \"%s\" carries no type suffix", name);
|
||||
switch ( name[len - 1] ) {
|
||||
@@ -125,7 +139,7 @@ akerr_ErrorContext *akbasic_structtype_table_init(akbasic_StructTypeTable *obj)
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL table in structtype init");
|
||||
memset(obj, 0, sizeof(*obj));
|
||||
PASS(errctx, aksl_memset(obj, 0, sizeof(*obj)));
|
||||
obj->count = 0;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -134,12 +148,17 @@ akerr_ErrorContext *akbasic_structtype_find(akbasic_StructTypeTable *obj, const
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int i = 0;
|
||||
int cmp = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && name != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in structtype find");
|
||||
*dest = -1;
|
||||
for ( i = 0; i < obj->count; i++ ) {
|
||||
if ( obj->types[i].used && strcmp(obj->types[i].name, name) == 0 ) {
|
||||
if ( !obj->types[i].used ) {
|
||||
continue;
|
||||
}
|
||||
PASS(errctx, aksl_strcmp(obj->types[i].name, name, &cmp));
|
||||
if ( cmp == 0 ) {
|
||||
*dest = i;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -151,9 +170,14 @@ akerr_ErrorContext *akbasic_structtype_field(akbasic_StructTypeTable *obj, int t
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
char known[AKBASIC_MAX_STRING_LENGTH];
|
||||
const char *separator = NULL;
|
||||
akbasic_StructType *type = NULL;
|
||||
size_t used = 0;
|
||||
size_t namelen = 0;
|
||||
size_t seplen = 0;
|
||||
int written = 0;
|
||||
int i = 0;
|
||||
int cmp = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && name != NULL && dest != NULL), AKERR_NULLPOINTER,
|
||||
"NULL argument in structtype field");
|
||||
@@ -162,7 +186,8 @@ akerr_ErrorContext *akbasic_structtype_field(akbasic_StructTypeTable *obj, int t
|
||||
type = &obj->types[typeindex];
|
||||
|
||||
for ( i = 0; i < type->fieldcount; i++ ) {
|
||||
if ( strcmp(type->fields[i].name, name) == 0 ) {
|
||||
PASS(errctx, aksl_strcmp(type->fields[i].name, name, &cmp));
|
||||
if ( cmp == 0 ) {
|
||||
*dest = &type->fields[i];
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -176,11 +201,20 @@ akerr_ErrorContext *akbasic_structtype_field(akbasic_StructTypeTable *obj, int t
|
||||
*/
|
||||
known[0] = '\0';
|
||||
for ( i = 0; i < type->fieldcount; i++ ) {
|
||||
int written = snprintf(known + used, sizeof(known) - used, "%s%s",
|
||||
(i == 0 ? "" : ", "), type->fields[i].name);
|
||||
if ( written < 0 || (size_t)written >= sizeof(known) - used ) {
|
||||
separator = (i == 0 ? "" : ", ");
|
||||
PASS(errctx, aksl_strlen(separator, &seplen));
|
||||
PASS(errctx, aksl_strlen(type->fields[i].name, &namelen));
|
||||
/*
|
||||
* The list is advisory, so a full buffer ends it rather than failing the
|
||||
* lookup. aksl_snprintf treats truncation as an error, which is right
|
||||
* everywhere the output matters -- so the fit is decided here, before the
|
||||
* call, instead of being read back out of a return value afterwards.
|
||||
*/
|
||||
if ( used + seplen + namelen >= sizeof(known) ) {
|
||||
break;
|
||||
}
|
||||
PASS(errctx, aksl_snprintf(&written, known + used, sizeof(known) - used, "%s%s",
|
||||
separator, type->fields[i].name));
|
||||
used += (size_t)written;
|
||||
}
|
||||
FAIL_RETURN(errctx, AKBASIC_ERR_UNDEFINED, "%s has no field %s (%s)",
|
||||
@@ -223,6 +257,8 @@ static akerr_ErrorContext *scan_names(akbasic_Runtime *obj)
|
||||
int64_t i = 0;
|
||||
int open = -1;
|
||||
int existing = 0;
|
||||
size_t namelen = 0;
|
||||
bool matched = false;
|
||||
const akbasic_Verb *verb = NULL;
|
||||
|
||||
for ( i = 0; i < AKBASIC_MAX_SOURCE_LINES; i++ ) {
|
||||
@@ -231,9 +267,11 @@ static akerr_ErrorContext *scan_names(akbasic_Runtime *obj)
|
||||
}
|
||||
cursor = next_word(skip_lineno(obj->source[i].code), word, sizeof(word));
|
||||
|
||||
if ( word_is(word, "END") ) {
|
||||
PASS(errctx, word_is(word, "END", &matched));
|
||||
if ( matched ) {
|
||||
(void)next_word(cursor, name, sizeof(name));
|
||||
if ( !word_is(name, "TYPE") ) {
|
||||
PASS(errctx, word_is(name, "TYPE", &matched));
|
||||
if ( !matched ) {
|
||||
continue; /* an ordinary END, not our terminator */
|
||||
}
|
||||
FAIL_ZERO_RETURN(errctx, (open >= 0), AKBASIC_ERR_SYNTAX,
|
||||
@@ -242,7 +280,8 @@ static akerr_ErrorContext *scan_names(akbasic_Runtime *obj)
|
||||
open = -1;
|
||||
continue;
|
||||
}
|
||||
if ( !word_is(word, "TYPE") ) {
|
||||
PASS(errctx, word_is(word, "TYPE", &matched));
|
||||
if ( !matched ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -268,16 +307,17 @@ static akerr_ErrorContext *scan_names(akbasic_Runtime *obj)
|
||||
FAIL_ZERO_RETURN(errctx, (table->count < AKBASIC_MAX_STRUCT_TYPES), AKBASIC_ERR_BOUNDS,
|
||||
"More than %d TYPE declarations", AKBASIC_MAX_STRUCT_TYPES);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (strlen(name) < AKBASIC_MAX_STRUCT_NAME), AKBASIC_ERR_BOUNDS,
|
||||
PASS(errctx, aksl_strlen(name, &namelen));
|
||||
FAIL_ZERO_RETURN(errctx, (namelen < AKBASIC_MAX_STRUCT_NAME), AKBASIC_ERR_BOUNDS,
|
||||
"TYPE name \"%s\" exceeds the %d character limit",
|
||||
name, AKBASIC_MAX_STRUCT_NAME - 1);
|
||||
open = table->count;
|
||||
memset(&table->types[open], 0, sizeof(table->types[open]));
|
||||
/* The memset is what terminates this: bounded at size - 1, the last byte
|
||||
is the zero it already wrote. Refused above rather than truncated,
|
||||
because two long names truncating to the same prefix would collide
|
||||
silently -- and truncation is an error everywhere else here. */
|
||||
strncpy(table->types[open].name, name, sizeof(table->types[open].name) - 1);
|
||||
PASS(errctx, aksl_memset(&table->types[open], 0, sizeof(table->types[open])));
|
||||
/* aksl_strcpy always terminates and refuses rather than truncates, which
|
||||
is the behaviour this site already wanted -- two long names truncating
|
||||
to the same prefix would collide silently. The length check above stays
|
||||
because it names the limit in the message. */
|
||||
PASS(errctx, aksl_strcpy(table->types[open].name, sizeof(table->types[open].name), name));
|
||||
table->types[open].used = true;
|
||||
table->types[open].firstline = i;
|
||||
table->types[open].lastline = -1;
|
||||
@@ -302,34 +342,38 @@ static akerr_ErrorContext *parse_field(akbasic_StructTypeTable *table, akbasic_S
|
||||
const char *cursor = NULL;
|
||||
akbasic_StructField *field = NULL;
|
||||
akbasic_Type valuetype = AKBASIC_TYPE_UNDEFINED;
|
||||
size_t namelen = 0;
|
||||
bool matched = false;
|
||||
int referenced = 0;
|
||||
int cmp = 0;
|
||||
int i = 0;
|
||||
|
||||
cursor = next_word(skip_lineno(line), fieldname, sizeof(fieldname));
|
||||
if ( fieldname[0] == '\0' || fieldname[0] == '\'' ) {
|
||||
SUCCEED_RETURN(errctx); /* a blank line inside the block */
|
||||
}
|
||||
if ( word_is(fieldname, "REM") ) {
|
||||
PASS(errctx, word_is(fieldname, "REM", &matched));
|
||||
if ( matched ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (type->fieldcount < AKBASIC_MAX_STRUCT_FIELDS), AKBASIC_ERR_BOUNDS,
|
||||
"TYPE %s declares more than %d fields", type->name, AKBASIC_MAX_STRUCT_FIELDS);
|
||||
for ( i = 0; i < type->fieldcount; i++ ) {
|
||||
FAIL_NONZERO_RETURN(errctx, (strcmp(type->fields[i].name, fieldname) == 0),
|
||||
AKBASIC_ERR_VALUE,
|
||||
PASS(errctx, aksl_strcmp(type->fields[i].name, fieldname, &cmp));
|
||||
FAIL_NONZERO_RETURN(errctx, (cmp == 0), AKBASIC_ERR_VALUE,
|
||||
"TYPE %s declares %s twice", type->name, fieldname);
|
||||
}
|
||||
|
||||
PASS(errctx, type_from_suffix(fieldname, &valuetype));
|
||||
FAIL_ZERO_RETURN(errctx, (strlen(fieldname) < AKBASIC_MAX_STRUCT_NAME), AKBASIC_ERR_BOUNDS,
|
||||
PASS(errctx, aksl_strlen(fieldname, &namelen));
|
||||
FAIL_ZERO_RETURN(errctx, (namelen < AKBASIC_MAX_STRUCT_NAME), AKBASIC_ERR_BOUNDS,
|
||||
"Field name \"%s\" exceeds the %d character limit",
|
||||
fieldname, AKBASIC_MAX_STRUCT_NAME - 1);
|
||||
field = &type->fields[type->fieldcount];
|
||||
memset(field, 0, sizeof(*field));
|
||||
/* Terminated by the memset above; refused rather than truncated, for the
|
||||
same reason a type name is. */
|
||||
strncpy(field->name, fieldname, sizeof(field->name) - 1);
|
||||
PASS(errctx, aksl_memset(field, 0, sizeof(*field)));
|
||||
/* Refused rather than truncated, for the same reason a type name is. */
|
||||
PASS(errctx, aksl_strcpy(field->name, sizeof(field->name), fieldname));
|
||||
field->valuetype = valuetype;
|
||||
field->typeindex = -1;
|
||||
field->offset = -1;
|
||||
@@ -354,13 +398,16 @@ static akerr_ErrorContext *parse_field(akbasic_StructTypeTable *table, akbasic_S
|
||||
* not fit in one. So the declaration has to name it.
|
||||
*/
|
||||
cursor = next_word(cursor, word, sizeof(word));
|
||||
FAIL_ZERO_RETURN(errctx, word_is(word, "AS"), AKBASIC_ERR_SYNTAX,
|
||||
PASS(errctx, word_is(word, "AS", &matched));
|
||||
FAIL_ZERO_RETURN(errctx, matched, AKBASIC_ERR_SYNTAX,
|
||||
"Line %" PRId64 ": %s must name its type -- %s AS TYPENAME, or %s AS PTR TO TYPENAME",
|
||||
lineno, fieldname, fieldname, fieldname);
|
||||
cursor = next_word(cursor, word, sizeof(word));
|
||||
if ( word_is(word, "PTR") ) {
|
||||
PASS(errctx, word_is(word, "PTR", &matched));
|
||||
if ( matched ) {
|
||||
cursor = next_word(cursor, word, sizeof(word));
|
||||
FAIL_ZERO_RETURN(errctx, word_is(word, "TO"), AKBASIC_ERR_SYNTAX,
|
||||
PASS(errctx, word_is(word, "TO", &matched));
|
||||
FAIL_ZERO_RETURN(errctx, matched, AKBASIC_ERR_SYNTAX,
|
||||
"Line %" PRId64 ": expected PTR TO TYPENAME", lineno);
|
||||
cursor = next_word(cursor, word, sizeof(word));
|
||||
field->kind = AKBASIC_FIELD_POINTER;
|
||||
@@ -466,7 +513,7 @@ akerr_ErrorContext *akbasic_structtype_scan(akbasic_Runtime *obj)
|
||||
}
|
||||
}
|
||||
for ( i = t; i < table->count; i++ ) {
|
||||
memset(&table->types[i], 0, sizeof(table->types[i]));
|
||||
PASS(errctx, aksl_memset(&table->types[i], 0, sizeof(table->types[i])));
|
||||
}
|
||||
table->count = t;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user