You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/04/16 20:20:14 UTC

[couchdb] 03/07: Check that only strings are passed to print

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch fix-couchjs-utf8-conversions-take2
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 44216d3a1c29dec5473defcbe3c68a7378a72895
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Wed Apr 15 15:12:59 2020 -0500

    Check that only strings are passed to print
    
    This function won't do conversion of arbitrary JavaScript values to
    strings. Callers should ensure they only pass JavaScript strings
    instead.
---
 src/couch/priv/couch_js/60/main.cpp | 14 +++++++++++++-
 src/couch/priv/couch_js/60/util.cpp | 17 ++++++-----------
 src/couch/priv/couch_js/60/util.h   |  2 +-
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/src/couch/priv/couch_js/60/main.cpp b/src/couch/priv/couch_js/60/main.cpp
index db2157d..11f8152 100644
--- a/src/couch/priv/couch_js/60/main.cpp
+++ b/src/couch/priv/couch_js/60/main.cpp
@@ -283,7 +283,19 @@ static bool
 print(JSContext* cx, unsigned int argc, JS::Value* vp)
 {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
-    couch_print(cx, argc, args);
+
+    bool use_stderr = false;
+    if(argc > 1 && args[1].isTrue()) {
+        use_stderr = true;
+    }
+
+    if(!args[0].isString()) {
+        JS_ReportErrorUTF8(cx, "Unable to print non-string value.");
+        return false;
+    }
+
+    couch_print(cx, args[0], use_stderr);
+
     args.rval().setUndefined();
     return true;
 }
diff --git a/src/couch/priv/couch_js/60/util.cpp b/src/couch/priv/couch_js/60/util.cpp
index 2cf02fd..9ea9af8 100644
--- a/src/couch/priv/couch_js/60/util.cpp
+++ b/src/couch/priv/couch_js/60/util.cpp
@@ -257,21 +257,16 @@ couch_readline(JSContext* cx, FILE* fp)
 
 
 void
-couch_print(JSContext* cx, unsigned int argc, JS::CallArgs argv)
+couch_print(JSContext* cx, JS::HandleValue obj, bool use_stderr)
 {
-    uint8_t* bytes = nullptr;
-    FILE *stream = stdout;
+    FILE* stream = stdout;
 
-    if (argc) {
-        if (argc > 1 && argv[1].isTrue()) {
-          stream = stderr;
-        }
-        std::string val = js_to_string(cx, argv.get(0));
-        fprintf(stream, "%s", val.c_str());
-        JS_free(cx, bytes);
+    if(use_stderr) {
+        stream = stderr;
     }
 
-    fputc('\n', stream);
+    std::string val = js_to_string(cx, obj);
+    fprintf(stream, "%s\n", val.c_str());
     fflush(stream);
 }
 
diff --git a/src/couch/priv/couch_js/60/util.h b/src/couch/priv/couch_js/60/util.h
index 07efc47..4c27f0f 100644
--- a/src/couch/priv/couch_js/60/util.h
+++ b/src/couch/priv/couch_js/60/util.h
@@ -33,7 +33,7 @@ couch_args* couch_parse_args(int argc, const char* argv[]);
 int couch_fgets(char* buf, int size, FILE* fp);
 JSString* couch_readline(JSContext* cx, FILE* fp);
 size_t couch_readfile(const char* file, char** outbuf_p);
-void couch_print(JSContext* cx, unsigned int argc, JS::CallArgs argv);
+void couch_print(JSContext* cx, JS::HandleValue str, bool use_stderr);
 void couch_error(JSContext* cx, JSErrorReport* report);
 void couch_oom(JSContext* cx, void* data);
 bool couch_load_funcs(JSContext* cx, JS::HandleObject obj, JSFunctionSpec* funcs);