From 2e25fe1e64f487fd91ff6f636bf3249b761ffcdc Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 13 Jan 2021 19:34:11 +0100 Subject: Use a fixed-size reader stack This improves performance, and makes the reader more suitable for embedded or network-facing applications, at the cost of requiring the user to specify a maximum stack size. --- NEWS | 1 + doc/serdi.1 | 9 ++ include/serd/serd.h | 7 +- src/n3.c | 270 ++++++++++++++++++++++++---------------------- src/reader.c | 64 ++++++----- src/reader.h | 62 +++++------ src/serdi.c | 17 ++- src/stack.h | 12 ++- src/string.c | 2 + src/writer.c | 6 +- test/meson.build | 5 +- test/test_read_chunk.c | 2 +- test/test_reader_writer.c | 6 +- test/test_string.c | 2 +- 14 files changed, 259 insertions(+), 206 deletions(-) diff --git a/NEWS b/NEWS index f204ae93..d900e544 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ serd (1.0.1) unstable; * Remove serd_uri_to_path() * Remove useless character counting from API * Rename SerdChunk to SerdStringView + * Use a fixed-size reader stack * Use char* for strings in public API -- David Robillard Wed, 13 Jan 2021 13:29:44 +0000 diff --git a/doc/serdi.1 b/doc/serdi.1 index 8747be92..3ae42ea0 100644 --- a/doc/serdi.1 +++ b/doc/serdi.1 @@ -9,6 +9,7 @@ .Op Fl abefhlqv .Op Fl c Ar prefix .Op Fl i Ar syntax +.Op Fl k Ar bytes .Op Fl o Ar syntax .Op Fl p Ar prefix .Op Fl r Ar root @@ -68,6 +69,14 @@ Case is ignored, valid values are: .Dq TriG , .Dq Turtle . .Pp +.It Fl k Ar bytes +Parser stack size. +For performance reasons, parsing is performed with a fixed-size stack. +By default, the stack is one page (4096 bytes), which should be sufficient for parsing most documents. +This option can be used to increase or decrease the amount of memory available for parsing. +It must be large enough to fit one statement, with some extra space for internal records. +Something around twice the length of the longest statement in text (as if it were written in NTriples or NQuads) is a reasonable value. +.Pp .It Fl l Lax (non-strict) parsing. If this is enabled, recoverable syntax errors will print a warning, but parsing will proceed starting at the next statement if possible. diff --git a/include/serd/serd.h b/include/serd/serd.h index d51adebe..63d094ef 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -96,7 +96,8 @@ typedef enum { SERD_ERR_NOT_FOUND, ///< Not found SERD_ERR_ID_CLASH, ///< Encountered clashing blank node IDs SERD_ERR_BAD_CURIE, ///< Invalid CURIE (e.g. prefix does not exist) - SERD_ERR_INTERNAL ///< Unexpected internal error (should not happen) + SERD_ERR_INTERNAL, ///< Unexpected internal error (should not happen) + SERD_ERR_OVERFLOW ///< Stack overflow } SerdStatus; /// RDF syntax type @@ -903,7 +904,9 @@ serd_sink_write_end(const SerdSink* SERD_NONNULL sink, /// Create a new RDF reader SERD_API SerdReader* SERD_ALLOCATED -serd_reader_new(SerdSyntax syntax, const SerdSink* SERD_NONNULL sink); +serd_reader_new(SerdSyntax syntax, + const SerdSink* SERD_NONNULL sink, + size_t stack_size); /** Enable or disable strict parsing diff --git a/src/n3.c b/src/n3.c index 439474dc..c33febeb 100644 --- a/src/n3.c +++ b/src/n3.c @@ -45,7 +45,7 @@ fancy_syntax(const SerdReader* reader) } static SerdStatus -read_collection(SerdReader* reader, ReadContext ctx, Ref* dest); +read_collection(SerdReader* reader, ReadContext ctx, SerdNode** dest); static SerdStatus read_predicateObjectList(SerdReader* reader, ReadContext ctx, bool* ate_dot); @@ -64,7 +64,7 @@ read_HEX(SerdReader* reader) // Read UCHAR escape, initial \ is already eaten by caller static inline SerdStatus -read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) +read_UCHAR(SerdReader* reader, SerdNode* dest, uint32_t* char_code) { const int b = peek_byte(reader); unsigned length = 0; @@ -78,7 +78,6 @@ read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) default: return SERD_ERR_BAD_SYNTAX; } - eat_byte_safe(reader, b); uint8_t buf[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; @@ -106,9 +105,9 @@ read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) SERD_ERR_BAD_SYNTAX, "unicode character 0x%X out of range\n", code); - push_bytes(reader, dest, replacement_char, 3); - *char_code = 0xFFFD; - return SERD_SUCCESS; + *char_code = 0xFFFD; + const SerdStatus st = push_bytes(reader, dest, replacement_char, 3); + return st ? st : SERD_SUCCESS; } // Build output in buf @@ -119,62 +118,55 @@ read_UCHAR(SerdReader* reader, Ref dest, uint32_t* char_code) buf[3] = (uint8_t)(0x80u | (c & 0x3Fu)); c >>= 6; c |= (16 << 12); // set bit 4 - /* fallthru */ + // fallthru case 3: buf[2] = (uint8_t)(0x80u | (c & 0x3Fu)); c >>= 6; c |= (32 << 6); // set bit 5 - /* fallthru */ + // fallthru case 2: buf[1] = (uint8_t)(0x80u | (c & 0x3Fu)); c >>= 6; c |= 0xC0; // set bits 6 and 7 - /* fallthru */ + // fallthru case 1: buf[0] = (uint8_t)c; - /* fallthru */ + // fallthru default: break; } - push_bytes(reader, dest, buf, size); *char_code = code; - return SERD_SUCCESS; + return push_bytes(reader, dest, buf, size); } // Read ECHAR escape, initial \ is already eaten by caller static inline SerdStatus -read_ECHAR(SerdReader* reader, Ref dest, SerdNodeFlags* flags) +read_ECHAR(SerdReader* reader, SerdNode* dest, SerdNodeFlags* flags) { const int c = peek_byte(reader); switch (c) { case 't': eat_byte_safe(reader, 't'); - push_byte(reader, dest, '\t'); - return SERD_SUCCESS; + return push_byte(reader, dest, '\t'); case 'b': eat_byte_safe(reader, 'b'); - push_byte(reader, dest, '\b'); - return SERD_SUCCESS; + return push_byte(reader, dest, '\b'); case 'n': *flags |= SERD_HAS_NEWLINE; eat_byte_safe(reader, 'n'); - push_byte(reader, dest, '\n'); - return SERD_SUCCESS; + return push_byte(reader, dest, '\n'); case 'r': *flags |= SERD_HAS_NEWLINE; eat_byte_safe(reader, 'r'); - push_byte(reader, dest, '\r'); - return SERD_SUCCESS; + return push_byte(reader, dest, '\r'); case 'f': eat_byte_safe(reader, 'f'); - push_byte(reader, dest, '\f'); - return SERD_SUCCESS; + return push_byte(reader, dest, '\f'); case '\\': case '"': case '\'': - push_byte(reader, dest, eat_byte_safe(reader, c)); - return SERD_SUCCESS; + return push_byte(reader, dest, eat_byte_safe(reader, c)); default: return SERD_ERR_BAD_SYNTAX; } @@ -216,22 +208,21 @@ read_utf8_bytes(SerdReader* reader, uint8_t bytes[4], uint32_t* size, uint8_t c) } static SerdStatus -read_utf8_character(SerdReader* reader, Ref dest, uint8_t c) +read_utf8_character(SerdReader* reader, SerdNode* dest, uint8_t c) { uint32_t size = 0; uint8_t bytes[4] = {0, 0, 0, 0}; SerdStatus st = read_utf8_bytes(reader, bytes, &size, c); if (st) { push_bytes(reader, dest, replacement_char, 3); - } else { - push_bytes(reader, dest, bytes, size); + return st; } - return st; + return push_bytes(reader, dest, bytes, size); } static SerdStatus -read_utf8_code(SerdReader* reader, Ref dest, uint32_t* code, uint8_t c) +read_utf8_code(SerdReader* reader, SerdNode* dest, uint32_t* code, uint8_t c) { uint32_t size = 0; uint8_t bytes[4] = {0, 0, 0, 0}; @@ -241,15 +232,20 @@ read_utf8_code(SerdReader* reader, Ref dest, uint32_t* code, uint8_t c) return st; } - push_bytes(reader, dest, bytes, size); - *code = parse_counted_utf8_char(bytes, size); + if (!(st = push_bytes(reader, dest, bytes, size))) { + *code = parse_counted_utf8_char(bytes, size); + } + return st; } // Read one character (possibly multi-byte) // The first byte, c, has already been eaten by caller static inline SerdStatus -read_character(SerdReader* reader, Ref dest, SerdNodeFlags* flags, uint8_t c) +read_character(SerdReader* reader, + SerdNode* dest, + SerdNodeFlags* flags, + uint8_t c) { if (!(c & 0x80)) { switch (c) { @@ -264,9 +260,9 @@ read_character(SerdReader* reader, Ref dest, SerdNodeFlags* flags, uint8_t c) default: break; } + return push_byte(reader, dest, c); } - return read_utf8_character(reader, dest, c); } @@ -332,7 +328,7 @@ eat_delim(SerdReader* reader, const char delim) // Initial triple quotes are already eaten by caller static SerdStatus read_STRING_LITERAL_LONG(SerdReader* reader, - Ref ref, + SerdNode* ref, SerdNodeFlags* flags, uint8_t q) { @@ -373,7 +369,7 @@ read_STRING_LITERAL_LONG(SerdReader* reader, // Initial quote is already eaten by caller static SerdStatus read_STRING_LITERAL(SerdReader* reader, - Ref ref, + SerdNode* ref, SerdNodeFlags* flags, uint8_t q) { @@ -412,7 +408,7 @@ read_STRING_LITERAL(SerdReader* reader, } static SerdStatus -read_String(SerdReader* reader, Ref node, SerdNodeFlags* flags) +read_String(SerdReader* reader, SerdNode* node, SerdNodeFlags* flags) { const int q1 = peek_byte(reader); eat_byte_safe(reader, q1); @@ -457,7 +453,7 @@ is_PN_CHARS_BASE(const uint32_t c) } static SerdStatus -read_PN_CHARS_BASE(SerdReader* reader, Ref dest) +read_PN_CHARS_BASE(SerdReader* reader, SerdNode* dest) { uint32_t code = 0; const int c = peek_byte(reader); @@ -487,7 +483,7 @@ is_PN_CHARS(const uint32_t c) } static SerdStatus -read_PN_CHARS(SerdReader* reader, Ref dest) +read_PN_CHARS(SerdReader* reader, SerdNode* dest) { uint32_t code = 0; const int c = peek_byte(reader); @@ -507,7 +503,7 @@ read_PN_CHARS(SerdReader* reader, Ref dest) } static SerdStatus -read_PERCENT(SerdReader* reader, Ref dest) +read_PERCENT(SerdReader* reader, SerdNode* dest) { push_byte(reader, dest, eat_byte_safe(reader, '%')); const uint8_t h1 = read_HEX(reader); @@ -521,7 +517,7 @@ read_PERCENT(SerdReader* reader, Ref dest) } static SerdStatus -read_PN_LOCAL_ESC(SerdReader* reader, Ref dest) +read_PN_LOCAL_ESC(SerdReader* reader, SerdNode* dest) { eat_byte_safe(reader, '\\'); @@ -557,7 +553,7 @@ read_PN_LOCAL_ESC(SerdReader* reader, Ref dest) } static SerdStatus -read_PLX(SerdReader* reader, Ref dest) +read_PLX(SerdReader* reader, SerdNode* dest) { const int c = peek_byte(reader); switch (c) { @@ -571,7 +567,7 @@ read_PLX(SerdReader* reader, Ref dest) } static SerdStatus -read_PN_LOCAL(SerdReader* reader, Ref dest, bool* ate_dot) +read_PN_LOCAL(SerdReader* reader, SerdNode* dest, bool* ate_dot) { int c = peek_byte(reader); SerdStatus st = SERD_SUCCESS; @@ -610,10 +606,9 @@ read_PN_LOCAL(SerdReader* reader, Ref dest, bool* ate_dot) trailing_unescaped_dot = (c == '.'); } - SerdNode* const n = deref(reader, dest); if (trailing_unescaped_dot) { // Ate trailing dot, pop it from stack/node and inform caller - --n->n_bytes; + --dest->n_bytes; serd_stack_pop(&reader->stack, 1); *ate_dot = true; } @@ -623,19 +618,21 @@ read_PN_LOCAL(SerdReader* reader, Ref dest, bool* ate_dot) // Read the remainder of a PN_PREFIX after some initial characters static SerdStatus -read_PN_PREFIX_tail(SerdReader* reader, Ref dest) +read_PN_PREFIX_tail(SerdReader* reader, SerdNode* dest) { - int c = 0; + SerdStatus st = SERD_SUCCESS; + int c = 0; while ((c = peek_byte(reader))) { // Middle: (PN_CHARS | '.')* if (c == '.') { - push_byte(reader, dest, eat_byte_safe(reader, c)); - } else if (read_PN_CHARS(reader, dest)) { + if ((st = push_byte(reader, dest, eat_byte_safe(reader, c)))) { + return st; + } + } else if ((st = read_PN_CHARS(reader, dest))) { break; } } - const SerdNode* const n = deref(reader, dest); - if (serd_node_string(n)[n->n_bytes - 1] == '.' && + if (!st && serd_node_string(dest)[dest->n_bytes - 1] == '.' && read_PN_CHARS(reader, dest)) { return r_err(reader, SERD_ERR_BAD_SYNTAX, "prefix ends with `.'\n"); } @@ -644,7 +641,7 @@ read_PN_PREFIX_tail(SerdReader* reader, Ref dest) } static SerdStatus -read_PN_PREFIX(SerdReader* reader, Ref dest) +read_PN_PREFIX(SerdReader* reader, SerdNode* dest) { if (!read_PN_CHARS_BASE(reader, dest)) { return read_PN_PREFIX_tail(reader, dest); @@ -654,33 +651,33 @@ read_PN_PREFIX(SerdReader* reader, Ref dest) } static SerdStatus -read_LANGTAG(SerdReader* reader, Ref* dest) +read_LANGTAG(SerdReader* reader, SerdNode** dest) { int c = peek_byte(reader); if (!is_alpha(c)) { return r_err(reader, SERD_ERR_BAD_SYNTAX, "unexpected `%c'\n", c); } - *dest = push_node(reader, SERD_LITERAL, "", 0); + if (!(*dest = push_node(reader, SERD_LITERAL, "", 0))) { + return SERD_ERR_OVERFLOW; + } SerdStatus st = SERD_SUCCESS; TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, c))); while ((c = peek_byte(reader)) && is_alpha(c)) { TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, c))); } - while (peek_byte(reader) == '-') { TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, '-'))); while ((c = peek_byte(reader)) && (is_alpha(c) || is_digit(c))) { TRY(st, push_byte(reader, *dest, eat_byte_safe(reader, c))); } } - return SERD_SUCCESS; } static SerdStatus -read_IRIREF_scheme(SerdReader* reader, Ref dest) +read_IRIREF_scheme(SerdReader* reader, SerdNode* dest) { int c = peek_byte(reader); if (!is_alpha(c)) { @@ -710,7 +707,7 @@ read_IRIREF_scheme(SerdReader* reader, Ref dest) } static SerdStatus -read_IRIREF(SerdReader* reader, Ref* dest) +read_IRIREF(SerdReader* reader, SerdNode** dest) { if (!eat_byte_check(reader, '<')) { return SERD_ERR_BAD_SYNTAX; @@ -795,7 +792,10 @@ read_IRIREF(SerdReader* reader, Ref* dest) } static SerdStatus -read_PrefixedName(SerdReader* reader, Ref dest, bool read_prefix, bool* ate_dot) +read_PrefixedName(SerdReader* reader, + SerdNode* dest, + bool read_prefix, + bool* ate_dot) { SerdStatus st = SERD_SUCCESS; if (read_prefix && ((st = read_PN_PREFIX(reader, dest)) > SERD_FAILURE)) { @@ -814,7 +814,7 @@ read_PrefixedName(SerdReader* reader, Ref dest, bool read_prefix, bool* ate_dot) } static SerdStatus -read_0_9(SerdReader* reader, Ref str, bool at_least_one) +read_0_9(SerdReader* reader, SerdNode* str, bool at_least_one) { unsigned count = 0; SerdStatus st = SERD_SUCCESS; @@ -831,8 +831,8 @@ read_0_9(SerdReader* reader, Ref str, bool at_least_one) static SerdStatus read_number(SerdReader* reader, - Ref* dest, - Ref* datatype, + SerdNode** dest, + SerdNode** datatype, SerdNodeFlags* flags, bool* ate_dot) { @@ -845,9 +845,14 @@ read_number(SerdReader* reader, SerdStatus st = SERD_SUCCESS; int c = peek_byte(reader); bool has_decimal = false; + if (!*dest) { + return SERD_ERR_OVERFLOW; + } + if (c == '-' || c == '+') { push_byte(reader, *dest, eat_byte_safe(reader, c)); } + if ((c = peek_byte(reader)) == '.') { has_decimal = true; // decimal case 2 (e.g. '.0' or `-.0' or `+.0') @@ -900,22 +905,24 @@ read_number(SerdReader* reader, } static SerdStatus -read_iri(SerdReader* reader, Ref* dest, bool* ate_dot) +read_iri(SerdReader* reader, SerdNode** dest, bool* ate_dot) { switch (peek_byte(reader)) { case '<': return read_IRIREF(reader, dest); default: - *dest = push_node(reader, SERD_CURIE, "", 0); + if (!(*dest = push_node(reader, SERD_CURIE, "", 0))) { + return SERD_ERR_OVERFLOW; + } return read_PrefixedName(reader, *dest, true, ate_dot); } } static SerdStatus read_literal(SerdReader* reader, - Ref* dest, - Ref* datatype, - Ref* lang, + SerdNode** dest, + SerdNode** datatype, + SerdNode** lang, SerdNodeFlags* flags, bool* ate_dot) { @@ -954,7 +961,7 @@ read_literal(SerdReader* reader, } static SerdStatus -read_verb(SerdReader* reader, Ref* dest) +read_verb(SerdReader* reader, SerdNode** dest) { if (peek_byte(reader) == '<') { return read_IRIREF(reader, dest); @@ -963,11 +970,13 @@ read_verb(SerdReader* reader, Ref* dest) /* Either a qname, or "a". Read the prefix first, and if it is in fact "a", produce that instead. */ - *dest = push_node(reader, SERD_CURIE, "", 0); + if (!(*dest = push_node(reader, SERD_CURIE, "", 0))) { + return SERD_ERR_OVERFLOW; + } SerdStatus st = read_PN_PREFIX(reader, *dest); bool ate_dot = false; - SerdNode* node = deref(reader, *dest); + SerdNode* node = *dest; const int next = peek_byte(reader); if (!st && node->n_bytes == 1 && serd_node_string(node)[0] == 'a' && next != ':' && !is_PN_CHARS_BASE((uint32_t)next)) { @@ -986,35 +995,34 @@ read_verb(SerdReader* reader, Ref* dest) } static SerdStatus -read_BLANK_NODE_LABEL(SerdReader* reader, Ref* dest, bool* ate_dot) +read_BLANK_NODE_LABEL(SerdReader* reader, SerdNode** dest, bool* ate_dot) { eat_byte_safe(reader, '_'); eat_byte_check(reader, ':'); - const Ref ref = *dest = push_node(reader, - SERD_BLANK, - reader->bprefix ? reader->bprefix : "", - reader->bprefix_len); + SerdNode* n = *dest = push_node(reader, + SERD_BLANK, + reader->bprefix ? reader->bprefix : "", + reader->bprefix_len); int c = peek_byte(reader); // First: (PN_CHARS | '_' | [0-9]) if (is_digit(c) || c == '_') { - push_byte(reader, ref, eat_byte_safe(reader, c)); - } else if (read_PN_CHARS(reader, ref)) { - *dest = pop_node(reader, *dest); + push_byte(reader, n, eat_byte_safe(reader, c)); + } else if (read_PN_CHARS(reader, n)) { + *dest = pop_node(reader, n); return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid name start\n"); } while ((c = peek_byte(reader))) { // Middle: (PN_CHARS | '.')* if (c == '.') { - push_byte(reader, ref, eat_byte_safe(reader, c)); - } else if (read_PN_CHARS(reader, ref)) { + push_byte(reader, n, eat_byte_safe(reader, c)); + } else if (read_PN_CHARS(reader, n)) { break; } } - SerdNode* n = deref(reader, ref); - char* buf = serd_node_buffer(n); - if (buf[n->n_bytes - 1] == '.' && read_PN_CHARS(reader, ref)) { + char* buf = serd_node_buffer(n); + if (buf[n->n_bytes - 1] == '.' && read_PN_CHARS(reader, n)) { // Ate trailing dot, pop it from stack/node and inform caller --n->n_bytes; serd_stack_pop(&reader->stack, 1); @@ -1027,7 +1035,7 @@ read_BLANK_NODE_LABEL(SerdReader* reader, Ref* dest, bool* ate_dot) buf[reader->bprefix_len] = 'B'; // Prevent clash reader->seen_genid = true; } else if (reader->seen_genid && buf[reader->bprefix_len] == 'B') { - *dest = pop_node(reader, *dest); + *dest = pop_node(reader, n); return r_err(reader, SERD_ERR_ID_CLASH, "found both `b' and `B' blank IDs, prefix required\n"); @@ -1037,24 +1045,24 @@ read_BLANK_NODE_LABEL(SerdReader* reader, Ref* dest, bool* ate_dot) return SERD_SUCCESS; } -static Ref +static SerdNode* read_blankName(SerdReader* reader) { eat_byte_safe(reader, '='); if (eat_byte_check(reader, '=') != '=') { r_err(reader, SERD_ERR_BAD_SYNTAX, "expected `='\n"); - return 0; + return NULL; } - Ref subject = 0; - bool ate_dot = false; + SerdNode* subject = 0; + bool ate_dot = false; read_ws_star(reader); read_iri(reader, &subject, &ate_dot); return subject; } static SerdStatus -read_anon(SerdReader* reader, ReadContext ctx, bool subject, Ref* dest) +read_anon(SerdReader* reader, ReadContext ctx, bool subject, SerdNode** dest) { const SerdStatementFlags old_flags = *ctx.flags; bool empty = false; @@ -1091,7 +1099,7 @@ read_anon(SerdReader* reader, ReadContext ctx, bool subject, Ref* dest) return r_err(reader, SERD_ERR_BAD_SYNTAX, "`.' inside blank\n"); } read_ws_star(reader); - serd_sink_write_end(reader->sink, deref(reader, *dest)); + serd_sink_write_end(reader->sink, *dest); *ctx.flags = old_flags; } return (eat_byte_check(reader, ']') == ']') ? SERD_SUCCESS @@ -1114,10 +1122,9 @@ read_object(SerdReader* reader, ReadContext* ctx, bool emit, bool* ate_dot) SerdStatus ret = SERD_FAILURE; bool simple = (ctx->subject != 0); - SerdNode* node = NULL; - Ref o = 0; - Ref datatype = 0; - Ref lang = 0; + SerdNode* o = 0; + SerdNode* datatype = 0; + SerdNode* lang = 0; uint32_t flags = 0; const int c = peek_byte(reader); if (!fancy_syntax(reader)) { @@ -1173,16 +1180,18 @@ read_object(SerdReader* reader, ReadContext* ctx, bool emit, bool* ate_dot) /* Either a boolean literal, or a qname. Read the prefix first, and if it is in fact a "true" or "false" literal, produce that instead. */ - o = push_node(reader, SERD_CURIE, "", 0); + if (!(o = push_node(reader, SERD_CURIE, "", 0))) { + return SERD_ERR_OVERFLOW; + } + while (!read_PN_CHARS_BASE(reader, o)) { } - node = deref(reader, o); - if ((node->n_bytes == 4 && !memcmp(serd_node_string(node), "true", 4)) || - (node->n_bytes == 5 && !memcmp(serd_node_string(node), "false", 5))) { - flags = flags | SERD_HAS_DATATYPE; - node->type = SERD_LITERAL; - datatype = push_node(reader, SERD_URI, XSD_BOOLEAN, XSD_BOOLEAN_LEN); - ret = SERD_SUCCESS; + if ((o->n_bytes == 4 && !memcmp(serd_node_string(o), "true", 4)) || + (o->n_bytes == 5 && !memcmp(serd_node_string(o), "false", 5))) { + flags = flags | SERD_HAS_DATATYPE; + o->type = SERD_LITERAL; + datatype = push_node(reader, SERD_URI, XSD_BOOLEAN, XSD_BOOLEAN_LEN); + ret = SERD_SUCCESS; } else if (read_PN_PREFIX_tail(reader, o) > SERD_FAILURE) { ret = SERD_ERR_BAD_SYNTAX; } else { @@ -1195,7 +1204,7 @@ read_object(SerdReader* reader, ReadContext* ctx, bool emit, bool* ate_dot) } if (!ret && simple && o) { - deref(reader, o)->flags = flags; + o->flags = flags; } if (!ret && emit && simple) { @@ -1273,8 +1282,8 @@ read_predicateObjectList(SerdReader* reader, ReadContext ctx, bool* ate_dot) static SerdStatus end_collection(SerdReader* reader, ReadContext ctx, - Ref n1, - Ref n2, + SerdNode* n1, + SerdNode* n2, SerdStatus st) { pop_node(reader, n2); @@ -1289,7 +1298,7 @@ end_collection(SerdReader* reader, } static SerdStatus -read_collection(SerdReader* reader, ReadContext ctx, Ref* dest) +read_collection(SerdReader* reader, ReadContext ctx, SerdNode** dest) { SerdStatus st = SERD_SUCCESS; eat_byte_safe(reader, '('); @@ -1310,10 +1319,15 @@ read_collection(SerdReader* reader, ReadContext ctx, Ref* dest) /* The order of node allocation here is necessarily not in stack order, so we create two nodes and recycle them throughout. */ - Ref n1 = push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); - Ref n2 = 0; - Ref node = n1; - Ref rest = 0; + SerdNode* n1 = + push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + SerdNode* n2 = 0; + SerdNode* node = n1; + SerdNode* rest = 0; + + if (!n1) { + return SERD_ERR_OVERFLOW; + } ctx.subject = *dest; while (!peek_delim(reader, ')')) { @@ -1348,7 +1362,7 @@ read_collection(SerdReader* reader, ReadContext ctx, Ref* dest) } static SerdStatus -read_subject(SerdReader* reader, ReadContext ctx, Ref* dest, int* s_type) +read_subject(SerdReader* reader, ReadContext ctx, SerdNode** dest, int* s_type) { SerdStatus st = SERD_SUCCESS; bool ate_dot = false; @@ -1375,7 +1389,7 @@ read_subject(SerdReader* reader, ReadContext ctx, Ref* dest, int* s_type) } static SerdStatus -read_labelOrSubject(SerdReader* reader, Ref* dest) +read_labelOrSubject(SerdReader* reader, SerdNode** dest) { bool ate_dot = false; switch (peek_byte(reader)) { @@ -1428,9 +1442,9 @@ read_base(SerdReader* reader, bool sparql, bool token) read_ws_star(reader); - Ref uri = 0; + SerdNode* uri = NULL; TRY(st, read_IRIREF(reader, &uri)); - TRY(st, serd_sink_write_base(reader->sink, deref(reader, uri))); + TRY(st, serd_sink_write_base(reader->sink, uri)); pop_node(reader, uri); read_ws_star(reader); @@ -1454,7 +1468,11 @@ read_prefixID(SerdReader* reader, bool sparql, bool token) } read_ws_star(reader); - Ref name = push_node(reader, SERD_LITERAL, "", 0); + SerdNode* name = push_node(reader, SERD_LITERAL, "", 0); + if (!name) { + return SERD_ERR_OVERFLOW; + } + if ((st = read_PN_PREFIX(reader, name)) > SERD_FAILURE) { return st; } @@ -1465,11 +1483,10 @@ read_prefixID(SerdReader* reader, bool sparql, bool token) } read_ws_star(reader); - Ref uri = 0; + SerdNode* uri = NULL; TRY(st, read_IRIREF(reader, &uri)); - st = serd_sink_write_prefix( - reader->sink, deref(reader, name), deref(reader, uri)); + st = serd_sink_write_prefix(reader->sink, name, uri); pop_node(reader, uri); pop_node(reader, name); @@ -1547,14 +1564,11 @@ read_wrappedGraph(SerdReader* reader, ReadContext* ctx) } static int -tokcmp(SerdReader* reader, Ref ref, const char* tok, size_t n) +tokcmp(SerdNode* node, const char* tok, size_t n) { - SerdNode* node = deref(reader, ref); - if (!node || node->n_bytes != n) { - return -1; - } - - return serd_strncasecmp(serd_node_string(node), tok, n); + return ((!node || node->n_bytes != n) + ? -1 + : serd_strncasecmp(serd_node_string(node), tok, n)); } SerdStatus @@ -1595,11 +1609,11 @@ read_n3_statement(SerdReader* reader) return st; } - if (!tokcmp(reader, ctx.subject, "base", 4)) { + if (!tokcmp(ctx.subject, "base", 4)) { st = read_base(reader, true, false); - } else if (!tokcmp(reader, ctx.subject, "prefix", 6)) { + } else if (!tokcmp(ctx.subject, "prefix", 6)) { st = read_prefixID(reader, true, false); - } else if (!tokcmp(reader, ctx.subject, "graph", 5)) { + } else if (!tokcmp(ctx.subject, "graph", 5)) { read_ws_star(reader); TRY(st, read_labelOrSubject(reader, &ctx.graph)); read_ws_star(reader); diff --git a/src/reader.c b/src/reader.c index ab9e54b5..1693ef64 100644 --- a/src/reader.c +++ b/src/reader.c @@ -42,9 +42,8 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...) } void -set_blank_id(SerdReader* reader, Ref ref, size_t buf_size) +set_blank_id(SerdReader* reader, SerdNode* node, size_t buf_size) { - SerdNode* node = deref(reader, ref); char* buf = (char*)(node + 1); const char* prefix = reader->bprefix ? (const char*)reader->bprefix : ""; @@ -58,15 +57,18 @@ genid_size(SerdReader* reader) return reader->bprefix_len + 1 + 10 + 1; // + "b" + UINT32_MAX + \0 } -Ref +SerdNode* blank_id(SerdReader* reader) { - Ref ref = push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); - set_blank_id(reader, ref, genid_size(reader)); + SerdNode* ref = + push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + if (ref) { + set_blank_id(reader, ref, genid_size(reader)); + } return ref; } -Ref +SerdNode* push_node_padded(SerdReader* reader, size_t maxlen, SerdNodeType type, @@ -76,6 +78,10 @@ push_node_padded(SerdReader* reader, void* mem = serd_stack_push_aligned( &reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode)); + if (!mem) { + return NULL; + } + SerdNode* const node = (SerdNode*)mem; node->n_bytes = n_bytes; node->flags = 0; @@ -85,14 +91,15 @@ push_node_padded(SerdReader* reader, memcpy(buf, str, n_bytes + 1); #ifdef SERD_STACK_CHECK - reader->allocs = (Ref*)realloc(reader->allocs, - sizeof(reader->allocs) * (++reader->n_allocs)); - reader->allocs[reader->n_allocs - 1] = ((char*)mem - reader->stack.buf); + reader->allocs = (SerdNode**)realloc( + reader->allocs, sizeof(reader->allocs) * (++reader->n_allocs)); + reader->allocs[reader->n_allocs - 1] = + (SerdNode*)((char*)mem - reader->stack.buf); #endif - return (Ref)((char*)node - reader->stack.buf); + return node; } -Ref +SerdNode* push_node(SerdReader* reader, SerdNodeType type, const char* str, @@ -102,41 +109,30 @@ push_node(SerdReader* reader, } SerdNode* -deref(SerdReader* reader, const Ref ref) -{ - return ref ? (SerdNode*)(reader->stack.buf + ref) : NULL; -} - -Ref -pop_node(SerdReader* reader, Ref ref) +pop_node(SerdReader* reader, const SerdNode* node) { - if (ref && ref != reader->rdf_first && ref != reader->rdf_rest && - ref != reader->rdf_nil) { + if (node && node != reader->rdf_first && node != reader->rdf_rest && + node != reader->rdf_nil) { #ifdef SERD_STACK_CHECK - SERD_STACK_ASSERT_TOP(reader, ref); + SERD_STACK_ASSERT_TOP(reader, node); --reader->n_allocs; #endif - SerdNode* const node = deref(reader, ref); - char* const top = reader->stack.buf + reader->stack.size; + char* const top = reader->stack.buf + reader->stack.size; serd_stack_pop_aligned(&reader->stack, (size_t)(top - (char*)node)); } - return 0; + return NULL; } SerdStatus -emit_statement(SerdReader* reader, ReadContext ctx, Ref o) +emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o) { - SerdNode* graph = deref(reader, ctx.graph); + SerdNode* graph = ctx.graph; if (!graph && reader->default_graph) { graph = reader->default_graph; } - const SerdStatus st = serd_sink_write(reader->sink, - *ctx.flags, - deref(reader, ctx.subject), - deref(reader, ctx.predicate), - deref(reader, o), - graph); + const SerdStatus st = serd_sink_write( + reader->sink, *ctx.flags, ctx.subject, ctx.predicate, o, graph); *ctx.flags &= SERD_ANON_CONT | SERD_LIST_CONT; // Preserve only cont flags return st; @@ -163,13 +159,13 @@ serd_reader_read_document(SerdReader* reader) } SerdReader* -serd_reader_new(SerdSyntax syntax, const SerdSink* sink) +serd_reader_new(SerdSyntax syntax, const SerdSink* sink, size_t stack_size) { SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader)); me->sink = sink; me->default_graph = NULL; - me->stack = serd_stack_new(SERD_PAGE_SIZE); + me->stack = serd_stack_new(stack_size); me->syntax = syntax; me->next_id = 1; me->strict = true; diff --git a/src/reader.h b/src/reader.h index c34f3a8b..162b5e04 100644 --- a/src/reader.h +++ b/src/reader.h @@ -41,18 +41,13 @@ # define SERD_STACK_ASSERT_TOP(reader, ref) #endif -/* Reference to a node in the stack (we can not use pointers since the - stack may be reallocated, invalidating any pointers to elements). -*/ -typedef size_t Ref; - typedef struct { - Ref graph; - Ref subject; - Ref predicate; - Ref object; - Ref datatype; - Ref lang; + SerdNode* graph; + SerdNode* subject; + SerdNode* predicate; + SerdNode* object; + SerdNode* datatype; + SerdNode* lang; SerdStatementFlags* flags; } ReadContext; @@ -60,9 +55,9 @@ struct SerdReaderImpl { const SerdSink* sink; SerdErrorFunc error_func; void* error_handle; - Ref rdf_first; - Ref rdf_rest; - Ref rdf_nil; + SerdNode* rdf_first; + SerdNode* rdf_rest; + SerdNode* rdf_nil; SerdNode* default_graph; SerdByteSource source; SerdStack stack; @@ -74,8 +69,8 @@ struct SerdReaderImpl { bool strict; ///< True iff strict parsing bool seen_genid; #ifdef SERD_STACK_CHECK - Ref* allocs; ///< Stack of push offsets - size_t n_allocs; ///< Number of stack pushes + SerdNode** allocs; ///< Stack of push offsets + size_t n_allocs; ///< Number of stack pushes #endif }; @@ -83,14 +78,14 @@ SERD_LOG_FUNC(3, 4) SerdStatus r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...); -Ref +SerdNode* push_node_padded(SerdReader* reader, size_t maxlen, SerdNodeType type, const char* str, size_t n_bytes); -Ref +SerdNode* push_node(SerdReader* reader, SerdNodeType type, const char* str, @@ -100,20 +95,17 @@ SERD_PURE_FUNC size_t genid_size(SerdReader* reader); -Ref +SerdNode* blank_id(SerdReader* reader); void -set_blank_id(SerdReader* reader, Ref ref, size_t buf_size); +set_blank_id(SerdReader* reader, SerdNode* node, size_t buf_size); SerdNode* -deref(SerdReader* reader, Ref ref); - -Ref -pop_node(SerdReader* reader, Ref ref); +pop_node(SerdReader* reader, const SerdNode* node); SerdStatus -emit_statement(SerdReader* reader, ReadContext ctx, Ref o); +emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o); SerdStatus read_n3_statement(SerdReader* reader); @@ -167,13 +159,15 @@ eat_string(SerdReader* reader, const char* str, unsigned n) } static inline SerdStatus -push_byte(SerdReader* reader, Ref ref, const int c) +push_byte(SerdReader* reader, SerdNode* node, const int c) { assert(c != EOF); SERD_STACK_ASSERT_TOP(reader, ref); - char* const s = (char*)serd_stack_push(&reader->stack, 1); - SerdNode* const node = (SerdNode*)(reader->stack.buf + ref); + char* const s = (char*)serd_stack_push(&reader->stack, 1); + if (!s) { + return SERD_ERR_OVERFLOW; + } *(uint8_t*)(s - 1) = (uint8_t)c; *s = '\0'; @@ -182,12 +176,20 @@ push_byte(SerdReader* reader, Ref ref, const int c) return SERD_SUCCESS; } -static inline void -push_bytes(SerdReader* reader, Ref ref, const uint8_t* bytes, unsigned len) +static inline SerdStatus +push_bytes(SerdReader* reader, + SerdNode* ref, + const uint8_t* bytes, + unsigned len) { + if (reader->stack.buf_size < reader->stack.size + len) { + return SERD_ERR_OVERFLOW; + } + for (unsigned i = 0; i < len; ++i) { push_byte(reader, ref, bytes[i]); } + return SERD_SUCCESS; } #endif // SERD_READER_H diff --git a/src/serdi.c b/src/serdi.c index 6049c31b..d2828ea2 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -28,8 +28,10 @@ # include #endif +#include #include #include +#include #include #define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg) @@ -102,6 +104,7 @@ print_usage(const char* name, bool error) fprintf(os, " -f Keep full URIs in input (don't qualify).\n"); fprintf(os, " -h Display this help and exit.\n"); fprintf(os, " -i SYNTAX Input syntax: turtle/ntriples/trig/nquads.\n"); + fprintf(os, " -k BYTES Parser stack size.\n"); fprintf(os, " -l Lax (non-strict) parsing.\n"); fprintf(os, " -o SYNTAX Output syntax: turtle/ntriples/nquads.\n"); fprintf(os, " -p PREFIX Add PREFIX to blank node IDs.\n"); @@ -174,6 +177,7 @@ main(int argc, char** argv) bool full_uris = false; bool lax = false; bool quiet = false; + size_t stack_size = 4194304; const char* add_prefix = NULL; const char* chop_prefix = NULL; const char* root_uri = NULL; @@ -212,6 +216,17 @@ main(int argc, char** argv) if (!(input_syntax = get_syntax(argv[a]))) { return print_usage(argv[0], true); } + } else if (argv[a][1] == 'k') { + if (++a == argc) { + return missing_arg(argv[0], 'k'); + } + char* endptr = NULL; + const long size = strtol(argv[a], &endptr, 10); + if (size <= 0 || size == LONG_MAX || *endptr != '\0') { + SERDI_ERRORF("invalid stack size `%s'\n", argv[a]); + return 1; + } + stack_size = (size_t)size; } else if (argv[a][1] == 'o') { if (++a == argc) { return missing_arg(argv[0], 'o'); @@ -284,7 +299,7 @@ main(int argc, char** argv) output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd); SerdReader* const reader = - serd_reader_new(input_syntax, serd_writer_sink(writer)); + serd_reader_new(input_syntax, serd_writer_sink(writer), stack_size); serd_reader_set_strict(reader, !lax); if (quiet) { diff --git a/src/stack.h b/src/stack.h index bcf1434b..c2335b3a 100644 --- a/src/stack.h +++ b/src/stack.h @@ -66,9 +66,9 @@ serd_stack_push(SerdStack* stack, size_t n_bytes) { const size_t new_size = stack->size + n_bytes; if (stack->buf_size < new_size) { - stack->buf_size += (stack->buf_size >> 1); // *= 1.5 - stack->buf = (char*)realloc(stack->buf, stack->buf_size); + return NULL; } + char* const ret = (stack->buf + stack->size); stack->size = new_size; return ret; @@ -85,12 +85,16 @@ static inline void* serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) { // Push one byte to ensure space for a pad count - serd_stack_push(stack, 1); + if (!serd_stack_push(stack, 1)) { + return NULL; + } // Push padding if necessary const size_t pad = align - stack->size % align; if (pad > 0) { - serd_stack_push(stack, pad); + if (!serd_stack_push(stack, pad)) { + return NULL; + } } // Set top of stack to pad count so we can properly pop later diff --git a/src/string.c b/src/string.c index f232acfa..d2ccea14 100644 --- a/src/string.c +++ b/src/string.c @@ -51,6 +51,8 @@ serd_strerror(SerdStatus status) return "Invalid CURIE"; case SERD_ERR_INTERNAL: return "Internal error"; + case SERD_ERR_OVERFLOW: + return "Stack overflow"; default: break; } diff --git a/src/writer.c b/src/writer.c index 2b2d5ed8..5a934e41 100644 --- a/src/writer.c +++ b/src/writer.c @@ -906,6 +906,10 @@ serd_writer_write_statement(SerdWriter* writer, if (flags & (SERD_ANON_S_BEGIN | SERD_ANON_O_BEGIN)) { WriteContext* ctx = (WriteContext*)serd_stack_push(&writer->anon_stack, sizeof(WriteContext)); + if (!ctx) { + return SERD_ERR_OVERFLOW; + } + *ctx = writer->context; WriteContext new_context = { serd_node_copy(graph), serd_node_copy(subject), NULL}; @@ -976,7 +980,7 @@ serd_writer_new(SerdSyntax syntax, writer->env = env; writer->root_node = NULL; writer->root_uri = SERD_URI_NULL; - writer->anon_stack = serd_stack_new(4 * sizeof(WriteContext)); + writer->anon_stack = serd_stack_new(SERD_PAGE_SIZE); writer->context = context; writer->list_subj = NULL; writer->empty = true; diff --git a/test/meson.build b/test/meson.build index a8c21e78..e45af26c 100644 --- a/test/meson.build +++ b/test/meson.build @@ -42,7 +42,7 @@ if get_option('utils') good_args = [ ['-v'], ['-h'], - ['-s', ' a .'], + ['-k', '512', '-s', ' a .'], ] foreach args : good_args @@ -53,6 +53,9 @@ if get_option('utils') ['-c'], ['-i', 'unknown'], ['-i'], + ['-k', '-1'], + ['-k', '9223372036854775807'], + ['-k', '1024junk'], ['-o', 'unknown'], ['-o'], ['-p'], diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index df8b43c2..9a482c02 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -85,7 +85,7 @@ main(void) serd_sink_set_statement_func(sink, on_statement); serd_sink_set_end_func(sink, on_end); - SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink, 4096); assert(reader); assert(!serd_reader_start_string(reader, diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 697469a9..b39ea761 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -83,7 +83,7 @@ test_read_chunks(void) FILE* const f = tmpfile(); static const char null = 0; SerdSink* sink = serd_sink_new(rt, NULL); - SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink, 4096); assert(reader); assert(sink); @@ -143,7 +143,7 @@ test_read_string(void) { ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); SerdSink* sink = serd_sink_new(rt, NULL); - SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink, 4096); assert(reader); assert(sink); @@ -261,7 +261,7 @@ test_reader(const char* path) { ReaderTest rt = {0, NULL}; SerdSink* const sink = serd_sink_new(&rt, NULL); - SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink, 4096); assert(reader); assert(sink); diff --git a/test/test_string.c b/test/test_string.c index 842ff3df..a3fb9247 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -39,7 +39,7 @@ test_strerror(void) { const char* msg = serd_strerror(SERD_SUCCESS); assert(!strcmp(msg, "Success")); - for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) { + for (int i = SERD_FAILURE; i <= SERD_ERR_OVERFLOW; ++i) { msg = serd_strerror((SerdStatus)i); assert(strcmp(msg, "Success")); } -- cgit v1.2.1