lib: fix handling of one character long directory names at top level

The code to skip multiple slashes in _notmuch_database_split_path()
skips back one character too much. This is compensated by a +1 in the
length parameter to the strndup() call. Mostly this works fine, but if
the path is to a file under a top level directory with one character
long name, the directory part is mistaken to be part of the file name
(slash == path in code). The returned directory name will be the empty
string and the basename will be the full path, breaking the indexing
logic in notmuch new.

Fix the multiple slash skipping to keep the slash variable pointing at
the last slash, and adjust strndup() accordingly.

The bug was introduced in

commit e890b0cf40
Author: Carl Worth <cworth@cworth.org>
Date:   Sat Dec 19 13:20:26 2009 -0800

    database: Store the parent ID for each directory document.

just a little over two months after the initial commit in the Notmuch
code history, making this the longest living bug in Notmuch to date.
This commit is contained in:
Jani Nikula 2016-04-10 22:43:22 +03:00 committed by David Bremner
parent 0f6b399d5b
commit a352d9ceaa
2 changed files with 2 additions and 7 deletions

View file

@ -1781,7 +1781,7 @@ _notmuch_database_split_path (void *ctx,
/* Finally, skip multiple slashes. */
while (slash != path) {
if (*slash != '/')
if (*(slash - 1) != '/')
break;
--slash;
@ -1794,7 +1794,7 @@ _notmuch_database_split_path (void *ctx,
*basename = path;
} else {
if (directory)
*directory = talloc_strndup (ctx, path, slash - path + 1);
*directory = talloc_strndup (ctx, path, slash - path);
}
return NOTMUCH_STATUS_SUCCESS;

View file

@ -170,7 +170,6 @@ test_expect_equal "$output" "(D) add_files, pass 3: queuing leftover directory $
No new mail. Removed 3 messages."
test_begin_subtest "One character directory at top level"
test_subtest_known_broken
generate_message [dir]=A
generate_message [dir]=A/B
@ -179,10 +178,6 @@ generate_message [dir]=A/B/C
output=$(NOTMUCH_NEW --debug)
test_expect_equal "$output" "Added 3 new messages to the database."
# clean up after the broken test to not mess up other tests
rm -rf "${MAIL_DIR}"/A
NOTMUCH_NEW 2>&1 > /dev/null
test_begin_subtest "Support single-message mbox"
cat > "${MAIL_DIR}"/mbox_file1 <<EOF
From test_suite@notmuchmail.org Fri Jan 5 15:43:57 2001