unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* BUG: Using pointer that points to a destructed string's content
@ 2014-12-26 11:37 Tamas Szakaly
  2014-12-26 22:03 ` David Bremner
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Tamas Szakaly @ 2014-12-26 11:37 UTC (permalink / raw)
  To: notmuch

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear notmuch developers,

The following line is from _notmuch_message_add_directory_terms in
lib/message.cc (line 652 in HEAD):

direntry = (*i).c_str ();

'i' is a Xapian::TermIterator, whose operator* returns a std::string by value.
This means that c_str() is called on a temporary, which is destructed after the
full expression (essentially the particular line in this case), so 'direntry'
will point to a destructed std::string's data.
(See https://gcc.gnu.org/onlinedocs/gcc/Temporaries.html)

One possible modification to correct this issue is using strdup:

direntry = strdup((*i).c_str ());

Note:
Despite the fact that it is wrong, it *generally* works, because delete[]-ing
the underlying character array in the destructor of std::string does not really
touch the memory content, and there is only a minor chance that this memory area
will be allocated again (e.g. from another thread). This caused me some headache
though with 'notmuch new' on FreeBSD 11-CURRENT, where jemalloc is configured so
that freed memory will be filled with 0x5a's.

Best regards,
sghctoma

- -- 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJUnUiQAAoJEE8tbNCQOSmESAsH/ih+EFx0WJEzImBkNe4I4H+0
Wj9u/ymmpgLwWnV0rg0oxnYoX5T6zT2e1jwTD73H7N4A2Xf2Susjbr6csTP2YyQB
aUbZ5/Ajq+COgpoEXTQUbrIPcIbdl0X05/k9f/OdNqZMHVK6j08hw2oqtpsq6v1+
PiuAa7kKrMda5rzLk08z1/qmJ6D7G2Trl6r5LPfytZhPwrphAJ9bWBofIIJLBQ0R
RdeTmGuzc7FBw1a1JqJWkDL1lI91VTD49Wr/VqYXPbfcWbaHhVYSklDshyEYaK/+
skemzV+aIWJiNHpkALdh3t+070caXlv5hwa826Q4kB0FMmkNlShjFqpXLJToEWo=
=hshP
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-01-03 13:30 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-26 11:37 BUG: Using pointer that points to a destructed string's content Tamas Szakaly
2014-12-26 22:03 ` David Bremner
2014-12-26 23:06   ` Tamas Szakaly
2014-12-28 10:45     ` [PATCH] lib: another iterator-temporary/stale-pointer bug David Bremner
2015-01-01 14:49       ` Jani Nikula
2015-01-02 16:20       ` [PATCH] lib: convert two "iterator copy strings" into references David Bremner
2015-01-02 17:52         ` Jani Nikula
2015-01-02 20:07         ` Tomi Ollila
2015-01-03  9:03         ` David Bremner
2014-12-27  8:33 ` [PATCH] lib: collapse computation of directory_id into a single expression David Bremner
2015-01-03 13:30 ` BUG: Using pointer that points to a destructed string's content David Bremner

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).