Skip to content

Commit

Permalink
src: fix UB in overflow checks
Browse files Browse the repository at this point in the history
Refs: #45868
  • Loading branch information
bnoordhuis committed Dec 16, 2022
1 parent 4166d40 commit 92b11b9
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "stream_wrap.h"
#include "util-inl.h"

#include <climits>
#include <cstring>
#include <cstdlib>

Expand Down Expand Up @@ -190,7 +191,7 @@ class ProcessWrap : public HandleWrap {
if (!argv_v.IsEmpty() && argv_v->IsArray()) {
Local<Array> js_argv = argv_v.As<Array>();
int argc = js_argv->Length();
CHECK_GT(argc + 1, 0); // Check for overflow.
CHECK_LT(argc, INT_MAX); // Check for overflow.

// Heap allocate to detect errors. +1 is for nullptr.
options.args = new char*[argc + 1];
Expand Down Expand Up @@ -218,7 +219,7 @@ class ProcessWrap : public HandleWrap {
if (!env_v.IsEmpty() && env_v->IsArray()) {
Local<Array> env_opt = env_v.As<Array>();
int envc = env_opt->Length();
CHECK_GT(envc + 1, 0); // Check for overflow.
CHECK_LT(envc, INT_MAX); // Check for overflow.
options.env = new char*[envc + 1]; // Heap allocated to detect errors.
for (int i = 0; i < envc; i++) {
node::Utf8Value pair(env->isolate(),
Expand Down

0 comments on commit 92b11b9

Please sign in to comment.