Skip to content

Fix string operation truncation #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion linenoise.c
Original file line number Diff line number Diff line change
@@ -948,7 +948,7 @@ static int line_edit(int stdin_fd,
char seq[5];

if (eventmux_callback != NULL) {
int result = eventmux_callback(l.buf);
int result = eventmux_callback(l.buf, l.buflen);
if (result != 0)
return result;
}
2 changes: 1 addition & 1 deletion linenoise.h
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ typedef struct {
typedef void(line_completion_callback_t)(const char *, line_completions_t *);
typedef char *(line_hints_callback_t)(const char *, int *color, int *bold);
typedef void(line_free_hints_callback_t)(void *);
typedef int(line_eventmux_callback_t)(char *);
typedef int(line_eventmux_callback_t)(char *, size_t);
void line_set_completion_callback(line_completion_callback_t *);
void line_set_hints_callback(line_hints_callback_t *);
void line_set_free_hints_callback(line_free_hints_callback_t *);
2 changes: 1 addition & 1 deletion qtest.c
Original file line number Diff line number Diff line change
@@ -1401,7 +1401,7 @@ int main(int argc, char *argv[])
}
case 'l':
strncpy(lbuf, optarg, BUFSIZE);
buf[BUFSIZE - 1] = '\0';
lbuf[BUFSIZE - 1] = '\0';
logfile_name = lbuf;
break;
default:
1 change: 1 addition & 0 deletions scripts/aspell-pws
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ epoll
errno
etc
eventfd
eventmux
fadvise
fchdir
fchmod
1 change: 1 addition & 0 deletions scripts/pre-commit.hook
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ cppcheck_suppressions() {
"preprocessorErrorDirective:random.h"
"constVariablePointer:linenoise.c"
"staticFunction:linenoise.c"
"unusedStructMember:linenoise.h"
"nullPointerOutOfMemory:web.c"
"staticFunction:web.c"
"constParameterCallback:tools/fmtscan.c"
5 changes: 3 additions & 2 deletions web.c
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ char *web_recv(int fd, struct sockaddr_in *clientaddr)
return ret;
}

int web_eventmux(char *buf)
int web_eventmux(char *buf, size_t buflen)
{
fd_set listenset;

@@ -259,7 +259,8 @@ int web_eventmux(char *buf)
char *p = web_recv(web_connfd, &clientaddr);
char *buffer = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
web_send(web_connfd, buffer);
strncpy(buf, p, strlen(p) + 1);
strncpy(buf, p, buflen);
buf[buflen] = '\0';
free(p);
close(web_connfd);
return strlen(buf);
2 changes: 1 addition & 1 deletion web.h
Original file line number Diff line number Diff line change
@@ -9,6 +9,6 @@ char *web_recv(int fd, struct sockaddr_in *clientaddr);

void web_send(int out_fd, char *buffer);

int web_eventmux(char *buf);
int web_eventmux(char *buf, size_t buflen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to change the function prototype, there must be a good reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original callback doesn't take the length of the buffer. This may result buffer overflow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the compiler reports stringop-truncation warning for this, and stop compilation with -O3.


#endif