19

I have a fairly simple C++ program which only takes one argument that is a Base64 encoded string. I can call the program.

I am now trying to call this program using Node.js' child_process.spawn(), but it is throwing an "E2BIG" error when I pass in the same Base64 string.

The Base64 string I am testing with is 305016 bytes in length.

Running getconf ARG_MAX on my Linux box returns 2097152.

Why does child_process throw the error?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Smith
  • 2,390
  • 1
  • 34
  • 60
  • Have you seen [What defines the maximum size for a command single argument?](http://unix.stackexchange.com/questions/120642/what-defines-the-maximum-size-for-a-command-single-argument) on unix.SO? It mentions `MAX_ARG_STRLEN` as the maximum length of a string argument. – traktor Mar 27 '16 at 21:59
  • 1
    Thats useful information to know, but why would it work when running from the command line, but not work using child_process.spawn()? – Jonathan Smith Mar 28 '16 at 10:30
  • 1
    `let child = require('child_process').spawn('myprogram'); child.stdin.write(largeBase64String); child.stdin.end();` this code is not working? – Ali Reza Riahi Jul 12 '23 at 08:25
  • @AliRezaRiahi an **argument** is ostensibly a string passed in on the command line, whereas what you are suggesting is to provide input to the program via stdin, that's a different mechanism, and the asker was likely running into command line argument length limitations, whereas stdin is unlimited. So yes, it's a workaround and ultimately the better approach for large amounts of data, but the question is valid as is and is asking for the *reason*, not a *workaround*. – Wyck Jul 12 '23 at 14:47

1 Answers1

1

Try to strace it to see if Node.js is making the system call or not - i.e., check is it an internal Node.js limitation or is it the Linux system which is rejecting it.

The strcpy used by libuv in Node.js can return E2BIG.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mmomtchev
  • 2,497
  • 1
  • 8
  • 23