/* Compile with: gcc -g -std=c99 -Wall -Werror -o notmuchcrash -lnotmuch notmuchcrash.c */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #define DIE(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) void add_new_email(notmuch_database_t* db, char const* filename, char const* content) { char mail_path[64]; strcpy(mail_path, notmuch_database_get_path(db)); strcat(mail_path, filename); FILE* mf = fopen(mail_path, "w+"); if (!mf) DIE("Failed to open mail file"); if (fwrite(content, strlen(content), 1, mf) != 1) DIE("Failed to write mail"); if (fclose(mf)) DIE("Failed to close file"); if (notmuch_database_add_message(db, mail_path, NULL) != NOTMUCH_STATUS_FILE_NOT_EMAIL) DIE("Something went wrong when adding the email"); } int main() { /* Create new database. */ char db_path[32]; strcpy(db_path, "/tmp/notmuchcrash-XXXXXX"); if (!mkdtemp(db_path)) DIE("Failed to create unique directory"); notmuch_database_t* db; if (notmuch_database_create(db_path, &db)) DIE("Failed to create database"); /* This doesn't have to be a valid email, it will crash either way. */ char const* mail_data = "hello"; /* First, try to add a file before closing. */ add_new_email(db, "/new-mail-1@example.net:2,", mail_data); /* Close the database. */ notmuch_database_close(db); /* Now try again. This will crash. */ add_new_email(db, "/new-mail-2@example.net:2,", mail_data); return 0; }