[PATCH v1 1/2] lib: Fix potential invalid read past an empty string

Subject: [PATCH v1 1/2] lib: Fix potential invalid read past an empty string

Date: Sat, 17 Jan 2015 09:51:45 -0600

To: notmuch@notmuchmail.org

Cc:

From: Todd


==22884== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x601600008291 at pc 0x7ff6295680e5 bp 0x7fff4ab9aa40 sp 0x7fff4ab9aa08
READ of size 1 at 0x601600008291 thread T0
    #0 0x7ff6295680e4 in __interceptor_strcmp ??:?
    #1 0x44763b in _thread_add_message /home/todd/.apps/notmuch/lib/thread.cc:255
    #2 0x4459e8 in notmuch_threads_get /home/todd/.apps/notmuch/lib/query.cc:496
    #3 0x41e2a7 in do_search_threads /home/todd/.apps/notmuch/notmuch-search.c:131
    #4 0x40a408 in main /home/todd/.apps/notmuch/notmuch.c:345
    #5 0x7ff627cb9ec4 in __libc_start_main /build/buildd/eglibc-2.19/csu/libc-start.c:287
    #6 0x40abf3 in _start ??:?
0x601600008291 is located 0 bytes to the right of 97-byte region [0x601600008230,0x601600008291)
allocated by thread T0 here:
    #0 0x7ff62956e41a in malloc ??:?
    #1 0x7ff628b8ab5d in talloc_strdup ??:?
---
 lib/thread.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/thread.cc b/lib/thread.cc
index 4542505..4c49f98 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -251,8 +251,8 @@ _thread_add_message (notmuch_thread_t *thread,
 		 term != NULL;
 		 term = term->next)
 	    {
-		/* We ignore initial 'K'. */
-		if (strcmp(tag, (term->string + 1)) == 0) {
+		/* Check for an empty string, and then ignore initial 'K'. */
+		if (*(term->string) && strcmp(tag, (term->string + 1)) == 0) {
 		    message_excluded = TRUE;
 		    break;
 		}
-- 
1.9.1


Thread: