nano-devel
[Top][All Lists]
Advanced

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

Re: [Nano-devel] solving a history bug in nano [patch]


From: Benno Schulenberg
Subject: Re: [Nano-devel] solving a history bug in nano [patch]
Date: Wed, 01 Jun 2016 19:37:06 +0200

On Wed, Jun 1, 2016, at 15:09, Tito wrote:
> > Running ./a.out here produces:
> >
> > Four...  1
> > Much...  1
> > Sizet... 1
> 
>   ./test
> Four...  1
> Much...  1
> Sizet... 0
> Minus... 0

Oh.  It seems to treat a length of -1 like if it were 0.
To verify that, please compile and run this:

#include <string.h>
#include <stdio.h>

int main()
{
        printf("Equal...     %x\n", strncmp("sam", "sam", (size_t)-1));
        printf("Longer...    %x\n", strncmp("same", "sa", (size_t)-1));
        printf("Shorter...   %x\n", strncmp("sa", "same", (size_t)-1));
        printf("Similar...   %x\n", strncmp("same", "so", (size_t)-1));
        printf("Related...   %x\n", strncmp("so", "same", (size_t)-1));
        printf("Different... %x\n", strncmp("same", "po", (size_t)-1));
        printf("Other...     %x\n", strncmp("po", "same", (size_t)-1));
}

Here it produces:

Equal...     0
Longer...    1
Shorter...   ffffffff
Similar...   ffffffff
Related...   1
Different... 1
Other...     ffffffff


When in the call of find_history I replace '(size_t)-1' with '0', I can't
start nano at all; it aborts immediately.  Only when giving the option
--ignore does it start up.  But then I get the same behavior as alpha:

^W  aa  <Enter>
^W  bb  <Enter>
Segmentation fault


Finally, looking at the code, I get it: when -1 treats *all* strings
as being equal, it also sees the given string as equal to "", the
empty string at the end of the history.  It deletes that item, and
then, on the second search it tries to assign the new search string
to that item.  Or something similar -- it is confusing.

If, in update_history(), you change the line after the line that
says (size_t)-1, from

    if (p != NULL) {

to

    if (p != NULL && p->data[0] != '\0') {

then your nano should not segfault any more.  (You do not
build up any history either, but this is just to confirm
that my understanding is correct.)

To solve it, we'll use the suggestion from alpha: pass to
find_history() the largest possible number that doesn't
have the high bit set.

Thanks for hanging in there, Tito, to get this to the right
solution.

Benno

-- 
http://www.fastmail.com - Email service worth paying for. Try it for free




reply via email to

[Prev in Thread] Current Thread [Next in Thread]