NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/52547: history_get_history_state() can report wrong history length



>Number:         52547
>Category:       lib
>Synopsis:       history_get_history_state() can report wrong history length
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Sep 17 05:30:00 +0000 2017
>Originator:     Yen Chi Hsuan
>Release:        8.99.2
>Organization:
>Environment:
NetBSD  8.99.2 NetBSD 8.99.2 (GENERIC) #0: Sat Sep 16 09:28:38 UTC 2017  mkrepro%mkrepro.NetBSD.org@localhost:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
With NetBSD's libedit, history_get_history_state() can report wrong history length if called immediately after read_history(). The reason is that read_history() does not update history_length [1].

[1] https://github.com/NetBSD/src/blob/trunk/lib/libedit/readline.c#L1350
>How-To-Repeat:
Create readline_test.c:

#include <readline/history.h>

int main()
{
        read_history("readline_history.txt");
        HISTORY_STATE *state = history_get_history_state();
        printf("%d\n", state->length);
        return 0;
}

And readline_history.txt:

_HiStOrY_V2_
1+1
1+2

Compiling it:

$ gcc readline_test.c -ledit

The result is 0:

$ ./a.out
0

Which should be 2.
>Fix:
Apple has a patch for readline.c with correct history_length handling [1]. Indeed the same test program gives correct results on macOS with Apple's forked version libedit-48.50.1.

int
read_history(const char *filename)
{
	HistEvent ev;

	if (h == NULL || e == NULL)
		rl_initialize();
	if (filename == NULL && (filename = _default_history_file()) == NULL)
		return errno;
	if (history(h, &ev, H_LOAD, filename) == -1)
		return (errno ? errno : EINVAL);
	if (history(h, &ev, H_GETSIZE) == 0)
		history_length = ev.num;

	return (!(history_length > 0)); /* return 0 if all is okay */
}

[1] https://opensource.apple.com/source/libedit/libedit-48.50.1/src/readline.c.auto.html



Home | Main Index | Thread Index | Old Index