Browse Source

Fix regression mem leak.

Stefano Cossu 3 years ago
parent
commit
d99b20ad72
4 changed files with 19 additions and 18 deletions
  1. 8 8
      TODO.md
  2. 6 6
      include/codec_base.h
  3. 4 3
      src/codec_nt.c
  4. 1 1
      test/test_codec_nt.c

+ 8 - 8
TODO.md

@@ -1,18 +1,18 @@
 # Quick TODO list
 
-P = pending; W = working on it; D = done.
+*P* = pending; *W* = working on it; *D* = done.
 
 ## Critical for MVP
 
 - *D* LMDB back end
-- D* Hash table back end
-- D* Python bindings
-- D* Namespace manager
-- W* N3 serialization / deserialization
-- P Environment
+- *D* Hash table back end
+- *D* Python bindings
+- *D* Namespace manager
+- *W* N3 serialization / deserialization
+- *P* Environment
 - *P* Turtle serialization / deserialization
-- P* Better error handling
-- P Python tests
+- *P* Better error handling
+- *P* Python tests
 
 
 ## Non-critical for MVP

+ 6 - 6
include/codec_base.h

@@ -66,9 +66,9 @@ typedef LSUP_rc (*term_enc_fn_t)(
  * Implementations MUST set the "codec" member of the iterator to the address
  * of the codec that generated it.
  *
- * @param[in] gr The graph to be encoded. The graph's namespace map is used by the
- * codec for namespace prefixing. The graph may only be freed after the loop is
- * finalized.
+ * @param[in] gr The graph to be encoded. The graph's namespace map is used by
+ * the codec for namespace prefixing. The graph may only be freed after the
+ * loop is finalized.
  *
  * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
  * function and, eventually, to a #gr_codec_done_fn_t function.
@@ -87,9 +87,9 @@ typedef LSUP_CodecIterator * (*gr_encode_init_fn_t)(const LSUP_Graph *gr);
  *
  * @param[out] res Handle to be populated with a string obtained from encoding.
  *  The output data should be UTF-8 [TODO or UTF-16] encoded. This pointer
- *  should be eventually freed manually at the end of the loop. It is
- *  reallocated at each iteration, so memory from a previous iteration may be
- *  overwritten with new data.
+ *  must be initialized (even to NULL) and should be eventually freed manually
+ *  at the end of the loop. It is reallocated at each iteration, so memory from
+ *  a previous iteration may be overwritten with new data.
  *
  * @return LSUP_OK if a new token was processed; LSUP_END if the end of the
  *  loop was reached.

+ 4 - 3
src/codec_nt.c

@@ -103,8 +103,6 @@ gr_to_nt_init (const LSUP_Graph *gr);
 
 static LSUP_rc
 gr_to_nt_iter (LSUP_CodecIterator *it, unsigned char **res) {
-    *res = NULL;
-
     LSUP_rc rc = LSUP_graph_iter_next (it->gr_it, it->trp);
     if (rc != LSUP_OK) return rc;
 
@@ -116,7 +114,10 @@ gr_to_nt_iter (LSUP_CodecIterator *it, unsigned char **res) {
     unsigned char *tmp = realloc (
             *res, strlen (it->str_s) + strlen (it->str_p)
             + strlen (it->str_o) + 6);
-    if (UNLIKELY (!tmp)) return LSUP_MEM_ERR;
+    if (UNLIKELY (!tmp)) {
+        *res = NULL;
+        return LSUP_MEM_ERR;
+    }
 
     sprintf ((char*)tmp, "%s %s %s .\n", it->str_s, it->str_p, it->str_o);
     *res = tmp;

+ 1 - 1
test/test_codec_nt.c

@@ -167,7 +167,7 @@ static int test_encode_nt_graph()
     LSUP_CodecIterator *it = nt_codec.encode_graph_init (gr);
     ASSERT (it != NULL, "Error creating codec iterator!");
 
-    char *tmp;
+    char *tmp = NULL;
     LSUP_rc rc;
     while ((rc = nt_codec.encode_graph_iter (it, (unsigned char **)&tmp)) != LSUP_END) {
         ASSERT (rc >= 0, "Encoding step failed!");