Browse files

Tests for overflow handling

  • Loading branch information...
1 parent 4bb7bee commit c058c13bead8270334742c0aa03a25759fd31709 @felixge felixge committed with piscisaureus Sep 9, 2011
Showing with 48 additions and 6 deletions.
  1. +1 −1 include/uv.h
  2. +9 −5 src/unix/process.c
  3. +4 −0 test/test-list.h
  4. +34 −0 test/test-spawn-sync.c
View
2 include/uv.h
@@ -886,8 +886,8 @@ typedef struct uv_spawn_sync_t{
int combine;
char *stdout_buf;
- int stdout_size;
int stdout_read;
+ int stdout_size;
char *stderr_buf;
int stderr_size;
View
14 src/unix/process.c
@@ -451,13 +451,17 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
}
if (spawn->stdout_buf && FD_ISSET(stdout_pipe[0], &pipes)) {
- /* Check for buffer overflow. */
if (spawn->stdout_size - spawn->stdout_read <= 0) {
- perror("buffer overflow");
+ /* Check for buffer overflow. */
goto error;
}
+
r = read(stdout_pipe[0], spawn->stdout_buf + spawn->stdout_read, spawn->stdout_size - spawn->stdout_read);
+
+ /* @TODO HANDLE EAGAIN / EINTR */
if (r == -1) {
+ /*uv_err_new(loop, errno);*/
+ /* TODO return -1 */
perror("read");
goto error;
}
@@ -466,11 +470,11 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
}
if (spawn->stderr_buf && FD_ISSET(stderr_pipe[0], &pipes)) {
- /* Check for buffer overflow. */
if (spawn->stderr_size - spawn->stderr_read <= 0) {
- perror("buffer overflow");
+ /* Check for buffer overflow. */
goto error;
}
+
r = read(stderr_pipe[0], spawn->stderr_buf + spawn->stderr_read, spawn->stderr_size - spawn->stderr_read);
if (r == -1) {
perror("read");
@@ -528,5 +532,5 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
}
kill(spawn->pid, SIGKILL);
- return -1;
+ return 0;
}
View
4 test/test-list.h
@@ -84,6 +84,8 @@ TEST_DECLARE (spawn_sync_exit_signal)
TEST_DECLARE (spawn_sync_stdio)
TEST_DECLARE (spawn_sync_stdout)
TEST_DECLARE (spawn_sync_stderr)
+TEST_DECLARE (spawn_sync_stdout_overflow)
+TEST_DECLARE (spawn_sync_stderr_overflow)
TEST_DECLARE (spawn_sync_combine_stdio)
TEST_DECLARE (spawn_sync_timeout)
TEST_DECLARE (fs_file_noent)
@@ -213,6 +215,8 @@ TASK_LIST_START
TEST_ENTRY (spawn_sync_stdio)
TEST_ENTRY (spawn_sync_stdout)
TEST_ENTRY (spawn_sync_stderr)
+ TEST_ENTRY (spawn_sync_stdout_overflow)
+ TEST_ENTRY (spawn_sync_stderr_overflow)
TEST_ENTRY (spawn_sync_combine_stdio)
TEST_ENTRY (spawn_sync_timeout)
#ifdef _WIN32
View
34 test/test-spawn-sync.c
@@ -157,6 +157,40 @@ TEST_IMPL(spawn_sync_stderr) {
return 0;
}
+TEST_IMPL(spawn_sync_stdout_overflow) {
+ int r;
+ uv_init();
+
+ init_process_options("stdout_stderr");
+
+ spawn.stdout_size = 1;
+
+ r = uv_spawn_sync(uv_default_loop(), &spawn);
+ debug(r);
+
+ ASSERT(r == 0);
+ ASSERT(spawn.stdout_read == spawn.stdout_size);
+
+ return 0;
+}
+
+TEST_IMPL(spawn_sync_stderr_overflow) {
+ int r;
+ uv_init();
+
+ init_process_options("stdout_stderr");
+
+ spawn.stderr_size = 1;
+
+ r = uv_spawn_sync(uv_default_loop(), &spawn);
+ debug(r);
+
+ ASSERT(r == 0);
+ ASSERT(spawn.stderr_read == spawn.stderr_size);
+
+ return 0;
+}
+
TEST_IMPL(spawn_sync_combine_stdio) {
int r;
char *expected_stdout = "stdout\nstderr\n";

0 comments on commit c058c13

Please sign in to comment.