Skip to content

Commit

Permalink
Revert changes from GNU readline 5.0
Browse files Browse the repository at this point in the history
There are two changes: one whitespace-only change in `input.c`, and
one change in `readline.c` which was (by rumor) thought to solve a
memory leak. It is, however, a no-op:

```
 #define USE_STATIC
// ...

char * readline (prompt)
const char *prompt;
{
 char *value; // was initialized with NULL before
 #ifdef USE_STATIC
  static char result[4096];
 #endif
// ...

 #ifdef USE_STATIC
  memset (result, 0, 4096);
  if (value) {
    strcpy (result, value);
    free (value);
  }
  return (strdup (result));
 #else
  return (value);
 #endif
}
```

The initialization of value with NULL GNU readline was gone, and the
code within `USE_STATIC` (and the `#define` itself) was added.

The code takes the original value that comes in `value`, copies it
into a static area `result`, and then frees the original
`value` (which assumes that `result` was allocated with `malloc`
before). The copied string is then again duplicated (which implies
creating the required size with `malloc`), and returned.

The copied string does not differ from the original one: both are
`malloc`ed and need to be `free`d by the caller.

So, this does not fix a memory leak.

Reverting this, however, makes the directory `vendor$readline/`
identical to the original GNU readline (except the `Makefile.in`
adaption, and the documentation), so that it is clean that it can be
replaced by linking to the system`'s readline.
  • Loading branch information
olebole committed Nov 16, 2017
1 parent 375edd0 commit 033ada3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
6 changes: 3 additions & 3 deletions vendor/readline/input.c
Expand Up @@ -416,10 +416,10 @@ rl_read_key ()
while (rl_event_hook && rl_get_char (&c) == 0)
{
(*rl_event_hook) ();
if (rl_done) {
if (rl_done) /* XXX - experimental */
return ('\n');
}
if (rl_gather_tyi () < 0) {
if (rl_gather_tyi () < 0) /* XXX - EIO */
{
rl_done = 1;
return ('\n');
}
Expand Down
20 changes: 2 additions & 18 deletions vendor/readline/readline.c
Expand Up @@ -275,17 +275,11 @@ rl_set_prompt (prompt)

/* Read a line of input. Prompt with PROMPT. An empty PROMPT means
none. A return value of NULL means that EOF was encountered. */
#define USE_STATIC

char *
readline (prompt)
const char *prompt;
const char *prompt;
{
char *value = (char *) NULL;
#ifdef USE_STATIC
static char result[4096];
#endif

char *value;

/* If we are at EOF return a NULL string. */
if (rl_pending_input == EOF)
Expand All @@ -310,17 +304,7 @@ readline (prompt)
rl_clear_signals ();
#endif

#ifdef USE_STATIC
memset (result, 0, 4096);
if (value) {
strcpy (result, value);
free (value);
}

return (strdup (result));
#else
return (value);
#endif
}

#if defined (READLINE_CALLBACKS)
Expand Down

0 comments on commit 033ada3

Please sign in to comment.