diff --git a/src/bro.bif b/src/bro.bif index d3bbd7c072..76317f5dd4 100644 --- a/src/bro.bif +++ b/src/bro.bif @@ -3624,17 +3624,28 @@ function NFS3::mode2string%(mode: count%): string function piped_exec%(program: string, to_write: string%): bool %{ const char* prog = program->CheckString(); + FILE* f = popen(prog, "w"); if ( ! f ) { reporter->Error("Failed to popen %s", prog); - return new Val(false, TYPE_BOOL); + return new Val(0, TYPE_BOOL); } - fprintf(f, "%s", to_write->CheckString()); + const u_char* input_data = to_write->Bytes(); + int input_data_len = to_write->Len(); + + int bytes_written = fwrite(input_data, 1, input_data_len, f); + pclose(f); - return new Val(true, TYPE_BOOL); + if ( bytes_written != input_data_len ) + { + reporter->Error("Failed to write all given data to %s", prog); + return new Val(0, TYPE_BOOL); + } + + return new Val(1, TYPE_BOOL); %} ## Enables the communication system. Note that by default, diff --git a/testing/btest/Baseline/bifs.piped_exec/test.txt b/testing/btest/Baseline/bifs.piped_exec/test.txt new file mode 100644 index 0000000000..a23f66ba7e Binary files /dev/null and b/testing/btest/Baseline/bifs.piped_exec/test.txt differ diff --git a/testing/btest/bifs/piped_exec.bro b/testing/btest/bifs/piped_exec.bro index 4405f0b500..32fd5c5f80 100644 --- a/testing/btest/bifs/piped_exec.bro +++ b/testing/btest/bifs/piped_exec.bro @@ -1,6 +1,12 @@ # @TEST-EXEC: bro %INPUT >output # @TEST-EXEC: btest-diff output +# @TEST-EXEC: btest-diff test.txt + global cmds = "print \"hello world\";"; cmds = string_cat(cmds, "\nprint \"foobar\";"); piped_exec("bro", cmds); + +# Test null output. +piped_exec("cat > test.txt", "\x00\x00hello\x00\x00"); +