troglobit / editline

A small replacement for GNU readline() for UNIX
https://troglobit.com/projects/editline/
Other
282 stars 58 forks source link

strdup implementation has off-by-one error #17

Closed Buzzzz closed 6 years ago

Buzzzz commented 6 years ago

In src/sysunix.c there is this:

#ifndef HAVE_STRDUP
/* Return an allocated copy of a string. */
char *strdup(const char *p)
{
    char *new = malloc(sizeof(char) * strlen(p));

This forgets to malloc the terminating NUL for the string and causes undefined behavior for the subsequent strcpy.

This should be

malloc(strlen(p) + 1);

Note that multiplying by sizeof(char) is considered silly, since it is 1 by definition in the C Standard.

troglobit commented 6 years ago

That old piece of code ... not sure if it's even worth keeping. Did you just stumble upon this or are you actively using editline on a target without strdup()?

Buzzzz commented 6 years ago

Joachim,

I was just browsing the code and it jumped at me, as I was looking for an alternative To “linenoise” for an embedded SPARC system with RTEMS as the OS.

Jens

troglobit commented 6 years ago

OK, according to the RTEMS 5.0.0 POSIX compliance guide it supports strdup(), so then I'll just remove that code path.

troglobit commented 6 years ago

Fixed, in my patch queue pending new completion API (FSF compat)