Permalink
Browse files

Proper buffer overflow handling

  • Loading branch information...
1 parent c058c13 commit 238bb9f71c23ddf2f1e8cf4032771723c8b474d4 @felixge felixge committed with piscisaureus Sep 9, 2011
Showing with 12 additions and 5 deletions.
  1. +3 −3 src/unix/process.c
  2. +9 −2 test/test-spawn-sync.c
View
@@ -453,15 +453,14 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
if (spawn->stdout_buf && FD_ISSET(stdout_pipe[0], &pipes)) {
if (spawn->stdout_size - spawn->stdout_read <= 0) {
/* Check for buffer overflow. */
+ uv_err_new_artificial(loop, UV_ENOBUFS);
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;
}
@@ -472,6 +471,7 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
if (spawn->stderr_buf && FD_ISSET(stderr_pipe[0], &pipes)) {
if (spawn->stderr_size - spawn->stderr_read <= 0) {
/* Check for buffer overflow. */
+ uv_err_new_artificial(loop, UV_ENOBUFS);
goto error;
}
@@ -532,5 +532,5 @@ int uv_spawn_sync(uv_loop_t* loop, uv_spawn_sync_t* spawn) {
}
kill(spawn->pid, SIGKILL);
- return 0;
+ return -1;
}
View
@@ -51,6 +51,7 @@ static void init_process_options(char* test) {
}
void debug(int r) {
+ fprintf(stderr, "----------------------------------------\n");
fprintf(stderr, "r: %i\n", r);
fprintf(stderr, "spawn.pid: %i\n", spawn.pid);
fprintf(stderr, "spawn.stdout_read: %i\n", spawn.stdout_read);
@@ -62,6 +63,10 @@ void debug(int r) {
fprintf(stderr, "spawn.exit_timeout: %i\n", spawn.exit_timeout);
fprintf(stderr, "spawn.exit_code: %i\n", spawn.exit_code);
fprintf(stderr, "spawn.exit_signal: %i\n", spawn.exit_signal);
+
+ uv_err_t err = uv_last_error(uv_default_loop());
+ fprintf(stderr, "uv_last_error_name: %s\n", uv_err_name(err));
+ fprintf(stderr, "----------------------------------------\n");
}
TEST_IMPL(spawn_sync_exit_code) {
@@ -168,8 +173,9 @@ TEST_IMPL(spawn_sync_stdout_overflow) {
r = uv_spawn_sync(uv_default_loop(), &spawn);
debug(r);
- ASSERT(r == 0);
+ ASSERT(r == -1);
ASSERT(spawn.stdout_read == spawn.stdout_size);
+ ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOBUFS);
return 0;
}
@@ -185,8 +191,9 @@ TEST_IMPL(spawn_sync_stderr_overflow) {
r = uv_spawn_sync(uv_default_loop(), &spawn);
debug(r);
- ASSERT(r == 0);
+ ASSERT(r == -1);
ASSERT(spawn.stderr_read == spawn.stderr_size);
+ ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOBUFS);
return 0;
}

0 comments on commit 238bb9f

Please sign in to comment.