Skip to content

Commit

Permalink
Lwt_unix.tcsetattr: Fix segfault on bad arguments (ocsigen#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenArchon committed Aug 31, 2020
1 parent c656870 commit 139a168
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/unix/unix_c/unix_tcsetattr_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ static void worker_tcsetattr(struct job_tcsetattr *job)
job->result = result;
job->error_code = errno;
} else {
decode_terminal_status(&termios, &(job->termios[0]));
job->result = tcsetattr(job->fd, job->when, &termios);
int result_decode = decode_terminal_status(&termios, &(job->termios[0]));
if (result_decode != 0) {
job->result = -1;
} else {
job->result = tcsetattr(job->fd, job->when, &termios);
}
job->error_code = errno;
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/unix/unix_c/unix_termios_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void encode_terminal_status(struct termios *terminal_status, value *dst)
}
}

void decode_terminal_status(struct termios *terminal_status, value *src)
int decode_terminal_status(struct termios *terminal_status, value *src)
{
long *pc;
int i;
Expand All @@ -232,7 +232,8 @@ void decode_terminal_status(struct termios *terminal_status, value *src)
if (i >= 0 && i < num) {
*dst = (*dst & ~msk) | pc[i];
} else {
unix_error(EINVAL, "tcsetattr", Nothing);
errno = EINVAL;
return EINVAL;
}
pc += num;
break;
Expand All @@ -253,11 +254,12 @@ void decode_terminal_status(struct termios *terminal_status, value *src)
speedtable[i].speed);
break;
}
if (res == -1) uerror("tcsetattr", Nothing);
if (res == -1) return res;
goto ok;
}
}
unix_error(EINVAL, "tcsetattr", Nothing);
errno = EINVAL;
return EINVAL;
ok:
break;
}
Expand All @@ -268,5 +270,6 @@ void decode_terminal_status(struct termios *terminal_status, value *src)
}
}
}
return 0;
}
#endif
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_termios_conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
#define NFIELDS 38

void encode_terminal_status(struct termios *terminal_status, value *dst);
void decode_terminal_status(struct termios *terminal_status, value *src);
int decode_terminal_status(struct termios *terminal_status, value *src);
#endif

0 comments on commit 139a168

Please sign in to comment.