From 6c8cd62277a9c81d1d24c146067c405ec90cbfb2 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Tue, 8 Apr 2025 01:32:41 +0100 Subject: Put some error paths in the fridge --- src/build/mkentprops.c | 59 +++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 25 deletions(-) (limited to 'src/build/mkentprops.c') diff --git a/src/build/mkentprops.c b/src/build/mkentprops.c index 287bdaa..bd4b082 100644 --- a/src/build/mkentprops.c +++ b/src/build/mkentprops.c @@ -29,11 +29,11 @@ #define fS "s" #endif -static noreturn die(int status, const char *s) { +static cold noreturn die(int status, const char *s) { fprintf(stderr, "mkentprops: fatal: %s\n", s); exit(status); } -static noreturn dieparse(const os_char *file, int line, const char *s) { +static cold noreturn dieparse(const os_char *file, int line, const char *s) { fprintf(stderr, "mkentprops: %" fS ":%d: %s\n", file, line, s); exit(2); } @@ -57,7 +57,7 @@ static int art_nnodes = 0; #define ART_NULL ((u16)-1) static inline int art_newnode() { - if (art_nnodes == ART_MAXNODES) die(2, "out of tree nodes"); + if_cold (art_nnodes == ART_MAXNODES) die(2, "out of tree nodes"); return art_nnodes++; } @@ -79,7 +79,7 @@ static int decls[MAXDECLS]; static int ndecls = 0; static inline int art_newleaf() { - if (art_nleaves == ART_MAXLEAVES) die(2, "out of leaf nodes"); + if_cold (art_nleaves == ART_MAXLEAVES) die(2, "out of leaf nodes"); return art_nleaves++; } @@ -152,7 +152,7 @@ static struct art_leaf *helpgetleaf(u16 *art, const char *s, int len, ++*countvar; } // if parsefile is null then we don't care about dupes (looking at subtable) - else if (parsefile && art_leaves[leaf.leafidx].varstr != VAR_NONE) { + else if (parsefile) if_cold (art_leaves[leaf.leafidx].varstr != VAR_NONE) { dieparse(parsefile, parseline, "duplicate property name"); } return art_leaves + leaf.leafidx; @@ -160,26 +160,26 @@ static struct art_leaf *helpgetleaf(u16 *art, const char *s, int len, static inline void handleentry(char *k, char *v, int vlen, const os_char *file, int line) { - if (ndecls == MAXDECLS) die(2, "out of declaration entries"); + if_cold (ndecls == MAXDECLS) die(2, "out of declaration entries"); decls[ndecls++] = k - sbase; char *propname = memchr(v, '/', vlen); - if (!propname) { + if_cold (!propname) { dieparse(file, line, "network name not in class/property format"); } *propname++ = '\0'; int sublen = propname - v; - if (sublen > 65535) { + if_cold (sublen > 65535) { dieparse(file, line, "network class name is far too long"); } vlen -= sublen; struct art_leaf *leaf = helpgetleaf(&art_root, v, sublen, 0, 0, &nclasses); u16 *subtree = &leaf->subtree; for (;;) { - if (vlen > 65535) { + if_cold (vlen > 65535) { dieparse(file, line, "property (SendTable) name is far too long"); } char *nextpart = memchr(propname, '/', vlen); - if (!nextpart) { + if_cold (!nextpart) { leaf = helpgetleaf(subtree, propname, vlen, file, line, &leaf->nsubs); leaf->varstr = k - sbase; @@ -196,7 +196,9 @@ static inline void handleentry(char *k, char *v, int vlen, static inline void parse(const os_char *file, int len) { char *s = sbase; // for convenience - if (s[len - 1] != '\n') dieparse(file, 0, "invalid text file (missing EOL)"); + if_cold (s[len - 1] != '\n') { + dieparse(file, 0, "invalid text file (missing EOL)"); + } enum { BOL = 0, KEY = 4, KWS = 8, VAL = 12, COM = 16, ERR = -1 }; static const s8 statetrans[] = { // layout: any, space|tab, #, \n @@ -217,8 +219,12 @@ static inline void parse(const os_char *file, int len) { } int newstate = statetrans[transidx]; if_cold (newstate == ERR) { - if (state == BOL) dieparse(file, line, "unexpected indentation"); - if (s[i] == '\n') dieparse(file, line, "unexpected end of line"); + if_cold (state == BOL) { + dieparse(file, line, "unexpected indentation"); + } + if_cold (s[i] == '\n') { + dieparse(file, line, "unexpected end of line"); + } dieparse(file, line, "unexpected comment"); } switch_exhaust (newstate) { @@ -239,11 +245,14 @@ static inline void parse(const os_char *file, int len) { } } -static inline noreturn diewrite() { die(100, "couldn't write to file"); } -#define _(x) if (fprintf(out, "%s\n", x) < 0) diewrite(); -#define _i(x) for (int i = 0; i < indent; ++i) fputc('\t', out); _(x) -#define F(f, ...) if (fprintf(out, f "\n", __VA_ARGS__) < 0) diewrite(); -#define Fi(...) for (int i = 0; i < indent; ++i) fputc('\t', out); F(__VA_ARGS__) +static cold noreturn diewrite() { die(100, "couldn't write to file"); } +#define _(x) if_cold (fprintf(out, "%s\n", x) < 0) diewrite(); +#define _doindent() \ + for (int i = 0; i < indent; ++i) \ + if_cold (fputc('\t', out) == EOF) diewrite(); +#define _i(x) _doindent(); _(x) +#define F(f, ...) if_cold (fprintf(out, f "\n", __VA_ARGS__) < 0) diewrite(); +#define Fi(...) _doindent(); F(__VA_ARGS__) #define H() \ _( "/* This file is autogenerated by src/build/mkentprops.c. DO NOT EDIT! */") \ _( "") @@ -360,24 +369,24 @@ _( "}") } int OS_MAIN(int argc, os_char *argv[]) { - if (argc != 2) die(1, "wrong number of arguments"); + if_cold (argc != 2) die(1, "wrong number of arguments"); int f = os_open_read(argv[1]); - if (f == -1) die(100, "couldn't open file"); + if_cold (f == -1) die(100, "couldn't open file"); vlong len = os_fsize(f); - if (len > 1u << 30 - 1) die(2, "input file is far too large"); + if_cold (len > 1u << 30 - 1) die(2, "input file is far too large"); sbase = malloc(len); - if (!sbase) die(100, "couldn't allocate memory"); - if (os_read(f, sbase, len) != len) die(100, "couldn't read file"); + if_cold (!sbase) die(100, "couldn't allocate memory"); + if_cold (os_read(f, sbase, len) != len) die(100, "couldn't read file"); os_close(f); parse(argv[1], len); FILE *out = fopen(".build/include/entprops.gen.h", "wb"); - if (!out) die(100, "couldn't open entprops.gen.h"); + if_cold (!out) die(100, "couldn't open entprops.gen.h"); H(); dodecls(out); out = fopen(".build/include/entpropsinit.gen.h", "wb"); - if (!out) die(100, "couldn't open entpropsinit.gen.h"); + if_cold (!out) die(100, "couldn't open entpropsinit.gen.h"); H(); doinit(out); return 0; -- cgit v1.2.3-54-g00ecf