From 2e3dbb884199bba6cc07345f6d394f1ac53465ac Mon Sep 17 00:00:00 2001
From: Muh Muhten <muh.muhten@gmail.com>
Date: Tue, 19 Feb 2019 00:34:04 -0500
Subject: [PATCH 4/9] Pass on the error message when rel_path is invalid

"Module path must be a string" is not a useful error message when the
reason the module path isn't a string is because the string it was got
replaced with an invalid with an error message for some other reason.

Also fixes a few memory leaks on early exits.
---
 src/linker.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

--- a/src/linker.c
+++ b/src/linker.c
@@ -138,10 +138,20 @@ static jv jv_basename(jv name) {
 
 // Asummes validated relative path to module
 static jv find_lib(jq_state *jq, jv rel_path, jv search, const char *suffix, jv jq_origin, jv lib_origin) {
-  if (jv_get_kind(search) != JV_KIND_ARRAY)
-    return jv_invalid_with_msg(jv_string_fmt("Module search path must be an array"));
-  if (jv_get_kind(rel_path) != JV_KIND_STRING)
+  if (!jv_is_valid(rel_path)) {
+    jv_free(search);
+    return rel_path;
+  }
+  if (jv_get_kind(rel_path) != JV_KIND_STRING) {
+    jv_free(rel_path);
+    jv_free(search);
     return jv_invalid_with_msg(jv_string_fmt("Module path must be a string"));
+  }
+  if (jv_get_kind(search) != JV_KIND_ARRAY) {
+    jv_free(rel_path);
+    jv_free(search);
+    return jv_invalid_with_msg(jv_string_fmt("Module search path must be an array"));
+  }
 
   struct stat st;
   int ret;
