Browse Source

Fix segfault on term creation before initialization.

Stefano Cossu 1 day ago
parent
commit
4b46b3d52a
2 changed files with 11 additions and 1 deletions
  1. 6 1
      include/core.h
  2. 5 0
      src/term.c

+ 6 - 1
include/core.h

@@ -101,12 +101,15 @@ typedef int LSUP_rc;
 #define LSUP_END            88803
 
 /** @brief Conflict warning.
+ *
  * An attempt to create or update a resource was made, but the resource existed
  * with a different form or value. The caller should find the value of the
  * existing resource to be different than the one that was attempted to store.
  * If this is returned from the iteration of multiple updates, it means that
  * other resources in the loop may have changed state and the operation as a
  * whole completed successfully.
+ *
+ * The error-level counterpart to this is #LSUP_CONFLICT_ERR.
  */
 #define LSUP_CONFLICT       88804
 
@@ -148,10 +151,12 @@ typedef int LSUP_rc;
  * is returned from the iteration of multiple updates, it means that the
  * operation has been interrupted and any state change within the loop prior to
  * the error has been rolled back.
+ *
+ * The warning-level counterpart to this is #LSUP_CONFLICT.
  */
 #define LSUP_CONFLICT_ERR   -88891
 
-/// Error while handling environment setup.
+/// Error while handling environment setup; or environment not initialized.
 #define LSUP_ENV_ERR        -88890
 ///@}  END defgroup return
 

+ 5 - 0
src/term.c

@@ -751,6 +751,11 @@ term_init (
         LSUP_Term *term, LSUP_TermType type,
         const char *data, void *metadata)
 {
+    // Exit early if environment is not initialized.
+    // EXCEPT for IRIRef which is used inside of LSUP_init().
+    if (!LSUP_IS_INIT && type != LSUP_TERM_IRIREF)
+        return LSUP_ENV_ERR;
+
     // Undefined type. Make quick work of it.
     if (type == LSUP_TERM_UNDEFINED) {
         term->type = type;