M include/identifier.h => include/identifier.h +5 -2
@@ 10,6 10,10 @@
// In other words, the length of "a::b::c" is 5.
#define IDENT_MAX 255
+// Minimum buffer size needed to store an unparsed identifier, including the
+// terminating NUL byte.
+#define IDENT_BUFSIZ (IDENT_MAX / 2 + IDENT_MAX + 1)
+
struct identifier {
char *name;
struct identifier *ns;
@@ 22,8 26,7 @@ struct identifiers {
uint32_t identifier_hash(uint32_t init, const struct identifier *ident);
char *identifier_unparse(const struct identifier *ident);
-int identifier_unparse_static(
- const struct identifier *ident, char *buf, size_t len);
+int identifier_unparse_static(const struct identifier *ident, char *buf);
char *ident_to_sym(const struct identifier *ident);
void identifier_dup(struct identifier *new, const struct identifier *ident);
bool identifier_eq(const struct identifier *a, const struct identifier *b);
M src/check.c => src/check.c +2 -3
@@ 185,9 185,8 @@ check_expr_access(struct context *ctx,
case ACCESS_IDENTIFIER:
obj = scope_lookup(ctx->scope, &aexpr->access.ident);
if (!obj) {
- char buf[1024];
- identifier_unparse_static(&aexpr->access.ident,
- buf, sizeof(buf));
+ char buf[IDENT_BUFSIZ];
+ identifier_unparse_static(&aexpr->access.ident, buf);
error(ctx, aexpr->loc, expr,
"Unknown object '%s'", buf);
return;
M src/identifier.c => src/identifier.c +8 -13
@@ 1,5 1,4 @@
#include <assert.h>
-#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ 47,22 46,18 @@ identifier_unparse(const struct identifier *ident)
}
int
-identifier_unparse_static(const struct identifier *ident, char *buf, size_t len)
+identifier_unparse_static(const struct identifier *ident, char *buf)
{
- assert(len < INT_MAX);
if (ident->ns) {
- int prefix = identifier_unparse_static(ident->ns, buf, len);
- int n = snprintf(&buf[prefix], len - prefix,
+ int prefix = identifier_unparse_static(ident->ns, buf);
+ int n = snprintf(&buf[prefix], IDENT_BUFSIZ - prefix,
"::%s", ident->name);
- if (n >= (int)len) {
- buf[len - 1] = '\0';
- }
- return prefix + n;
- }
- int n = snprintf(buf, len, "%s", ident->name);
- if (n >= (int)len) {
- buf[len - 1] = '\0';
+ n += prefix;
+ assert(n < IDENT_BUFSIZ);
+ return n;
}
+ int n = snprintf(buf, IDENT_BUFSIZ, "%s", ident->name);
+ assert(n < IDENT_BUFSIZ);
return n;
}