lib: fix error handling

Currently if a Xapian exception happens in notmuch_message_get_header,
the exception is not caught leading to crash. In
notmuch_message_get_date the exception is caught, but an internal error
is raised, again leading to crash.

This patch fixes the error handling by making both functions catch the
Xapian exceptions, print an error and return NULL or 0.

The 'notmuch->exception_reported' is also set, as is done elsewhere,
even if I don't really get the idea of that field.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@iki.fi>
This commit is contained in:
Tomi Valkeinen 2013-11-19 07:10:31 +02:00 committed by David Bremner
parent 40a3bf7e68
commit 075d53dde5

View file

@ -412,6 +412,7 @@ _notmuch_message_ensure_message_file (notmuch_message_t *message)
const char * const char *
notmuch_message_get_header (notmuch_message_t *message, const char *header) notmuch_message_get_header (notmuch_message_t *message, const char *header)
{ {
try {
std::string value; std::string value;
/* Fetch header from the appropriate xapian value field if /* Fetch header from the appropriate xapian value field if
@ -426,6 +427,13 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header)
if (!value.empty()) if (!value.empty())
return talloc_strdup (message, value.c_str ()); return talloc_strdup (message, value.c_str ());
} catch (Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred when reading header: %s\n",
error.get_msg().c_str());
message->notmuch->exception_reported = TRUE;
return NULL;
}
/* Otherwise fall back to parsing the file */ /* Otherwise fall back to parsing the file */
_notmuch_message_ensure_message_file (message); _notmuch_message_ensure_message_file (message);
if (message->message_file == NULL) if (message->message_file == NULL)
@ -766,7 +774,9 @@ notmuch_message_get_date (notmuch_message_t *message)
try { try {
value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP); value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
} catch (Xapian::Error &error) { } catch (Xapian::Error &error) {
INTERNAL_ERROR ("Failed to read timestamp value from document."); fprintf (stderr, "A Xapian exception occurred when reading date: %s\n",
error.get_msg().c_str());
message->notmuch->exception_reported = TRUE;
return 0; return 0;
} }