From 2eb037b3f1abebbac697c7d804f1e6339c907aaa Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 28 Feb 2023 22:39:02 -0500 Subject: Fix possible hang when writing nested Turtle lists --- NEWS | 3 ++- src/writer.c | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 4d8fd5fa..a86e4d8b 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,7 @@ serd (0.31.0) unstable; urgency=medium * Fix crash when trying to read chunks without starting * Fix hang when skipping an error at EOF when lax parsing * Fix incorrect parsing of strange quote escape patterns + * Fix possible hang when writing nested Turtle lists * Gracefully handle bad characters in Turtle blank node syntax * Gracefully handle bad characters in Turtle datatype syntax * Improve serdi man page @@ -17,7 +18,7 @@ serd (0.31.0) unstable; urgency=medium * Replace duplicated dox_to_sphinx script with sphinxygen dependency * Test header for warnings more strictly - -- David Robillard Wed, 01 Mar 2023 00:36:43 +0000 + -- David Robillard Wed, 01 Mar 2023 03:38:47 +0000 serd (0.30.16) stable; urgency=medium diff --git a/src/writer.c b/src/writer.c index 575d3fb9..d4b47e0a 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1,4 +1,4 @@ -// Copyright 2011-2020 David Robillard +// Copyright 2011-2023 David Robillard // SPDX-License-Identifier: ISC #include "byte_sink.h" @@ -121,6 +121,14 @@ supports_uriref(const SerdWriter* writer) return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG; } +static void +deindent(SerdWriter* writer) +{ + if (writer->indent) { + --writer->indent; + } +} + static void w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) { @@ -766,7 +774,7 @@ write_list_obj(SerdWriter* writer, const SerdNode* lang) { if (!strcmp((const char*)object->buf, NS_RDF "nil")) { - --writer->indent; + deindent(writer); write_sep(writer, SEP_LIST_END); return true; } @@ -856,7 +864,7 @@ serd_writer_write_statement(SerdWriter* writer, write_sep(writer, SEP_END_O); write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); if (!(flags & SERD_ANON_O_BEGIN)) { - --writer->indent; + deindent(writer); } } else { // Abbreviate S @@ -868,8 +876,7 @@ serd_writer_write_statement(SerdWriter* writer, } else { // No abbreviation if (writer->context.subject.type) { - assert(writer->indent > 0); - --writer->indent; + deindent(writer); if (serd_stack_is_empty(&writer->anon_stack)) { write_sep(writer, SEP_END_S); } @@ -926,7 +933,7 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) return SERD_ERR_UNKNOWN; } - --writer->indent; + deindent(writer); write_sep(writer, SEP_ANON_END); free_context(writer); writer->context = *anon_stack_top(writer); -- cgit v1.2.1