From f6437f606bd85dbc6d7581146955f85902230ca0 Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Sun, 8 May 2022 15:27:32 -0400
Subject: Fix memory consumption when reading documents

---
 NEWS              |  6 ++++++
 doc/serdi.1       |  4 ++--
 src/n3.c          | 14 +++++++++++++-
 src/serd_config.h |  4 ++--
 wscript           |  2 +-
 5 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/NEWS b/NEWS
index cb1dbb93..34ecc18d 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,9 @@
+serd (0.30.13) unstable;
+
+  * Fix memory consumption when reading documents
+
+ -- David Robillard <d@drobilla.net>  Fri, 10 Jun 2022 13:00:11 +0000
+
 serd (0.30.12) stable;
 
   * Fix warnings and build issues with clang 13 and VS 2019
diff --git a/doc/serdi.1 b/doc/serdi.1
index a459136d..1d69add2 100644
--- a/doc/serdi.1
+++ b/doc/serdi.1
@@ -1,6 +1,6 @@
-.Dd May 26, 2022
+.Dd Jun 10, 2022
 .Dt SERDI 1
-.Os Serd 0.30.12
+.Os Serd 0.30.13
 .Sh NAME
 .Nm serdi
 .Nd read and write RDF syntax
diff --git a/src/n3.c b/src/n3.c
index 33828258..60d975c2 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -1572,7 +1572,7 @@ read_wrappedGraph(SerdReader* const reader, ReadContext* const ctx)
         reader, SERD_ERR_BAD_SYNTAX, "missing predicate object list\n");
     }
 
-    pop_node(reader, ctx->subject);
+    ctx->subject = pop_node(reader, ctx->subject);
     read_ws_star(reader);
     if (peek_byte(reader) == '.') {
       eat_byte_safe(reader, '.');
@@ -1606,6 +1606,10 @@ tokcmp(SerdReader* const reader,
 SerdStatus
 read_n3_statement(SerdReader* const reader)
 {
+#ifndef NDEBUG
+  const size_t orig_stack_size = reader->stack.size;
+#endif
+
   SerdStatementFlags flags   = 0;
   ReadContext        ctx     = {0, 0, 0, 0, 0, 0, &flags};
   bool               ate_dot = false;
@@ -1646,6 +1650,7 @@ read_n3_statement(SerdReader* const reader)
     } else if (!tokcmp(reader, ctx.subject, "prefix", 6)) {
       st = read_prefixID(reader, true, false);
     } else if (!tokcmp(reader, ctx.subject, "graph", 5)) {
+      ctx.subject = pop_node(reader, ctx.subject);
       read_ws_star(reader);
       TRY(st, read_labelOrSubject(reader, &ctx.graph));
       read_ws_star(reader);
@@ -1678,8 +1683,15 @@ read_n3_statement(SerdReader* const reader)
       st = (eat_byte_check(reader, '.') == '.') ? SERD_SUCCESS
                                                 : SERD_ERR_BAD_SYNTAX;
     }
+
+    ctx.subject = pop_node(reader, ctx.subject);
     break;
   }
+
+#ifndef NDEBUG
+  assert(reader->stack.size == orig_stack_size);
+#endif
+
   return st;
 }
 
diff --git a/src/serd_config.h b/src/serd_config.h
index 17c56929..1af3f423 100644
--- a/src/serd_config.h
+++ b/src/serd_config.h
@@ -1,5 +1,5 @@
 /*
-  Copyright 2021 David Robillard <d@drobilla.net>
+  Copyright 2021-2022 David Robillard <d@drobilla.net>
 
   Permission to use, copy, modify, and/or distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
@@ -29,7 +29,7 @@
 #define SERD_CONFIG_H
 
 // Define version unconditionally so a warning will catch a mismatch
-#define SERD_VERSION "0.30.12"
+#define SERD_VERSION "0.30.13"
 
 #if !defined(SERD_NO_DEFAULT_CONFIG)
 
diff --git a/wscript b/wscript
index 5965892e..8f717d32 100644
--- a/wscript
+++ b/wscript
@@ -11,7 +11,7 @@ from waflib.extras import autowaf
 # major increment <=> incompatible changes
 # minor increment <=> compatible changes (additions)
 # micro increment <=> no interface changes
-SERD_VERSION       = '0.30.12'
+SERD_VERSION       = '0.30.13'
 SERD_MAJOR_VERSION = '0'
 
 # Mandatory waf variables
-- 
cgit v1.2.1