Skip to content

Commit 3d3b58d

Browse files
authored
Enforce R^W in std.popen() (#1101)
Because a popen pipe is unidirectional the mode argument should be "r" or "w" but not "rw".
1 parent f0849ad commit 3d3b58d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

quickjs-libc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ static JSValue js_std_popen(JSContext *ctx, JSValueConst this_val,
11501150
mode = JS_ToCString(ctx, argv[1]);
11511151
if (!mode)
11521152
goto fail;
1153-
if (mode[strspn(mode, "rw")] != '\0') {
1153+
if (strcmp(mode, "r") && strcmp(mode, "w")) {
11541154
JS_ThrowTypeError(ctx, "invalid file mode");
11551155
goto fail;
11561156
}

tests/test_std.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ function test_popen()
104104
std.writeFile(fname, content);
105105
assert(std.loadFile(fname), content);
106106

107+
// popen pipe is unidirectional so mode should
108+
// be either read or write but not both
109+
let caught = false;
110+
try {
111+
std.popen(cmd, "rw");
112+
} catch (e) {
113+
assert(/invalid file mode/.test(e.message));
114+
caught = true;
115+
}
116+
assert(caught);
117+
107118
/* execute shell command */
108119
f = std.popen(cmd + " " + fname, "r");
109120
str = f.readAsString();

0 commit comments

Comments
 (0)