Fix double-free in DynamicConverter
authorNicholas Ormrod <njormrod@fb.com>
Tue, 25 Jul 2017 17:50:36 +0000 (10:50 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 25 Jul 2017 18:07:38 +0000 (11:07 -0700)
commitd1ddabb086a2a605b6fa4f95d7b3f72b7865d48b
treeb7265618e63d0d5850e729d044b7c33473f31dbc
parentfbfd3029afb38ba11ff8f403aac3704c47141119
Fix double-free in DynamicConverter

Summary:
If an exception is thrown during the construction of value in a container, then the Transformer iterator will not successfully reconstruct a cache_ object after explicitly calling its destructor (due to the exception being thrown), which results in a double-free when cache_ is destroyed as part of Transformer's destructor.

Conveniently, there exists a piece of code designed to solve just this problem: folly::Optional. Replace cache_ and valid_ with folly::Optional.

This also fixes the unnecessary requirement that container value types have default constructors, since cache_ is no longer default constructed inside of Transformer.

Reviewed By: markisaa

Differential Revision: D5472342

fbshipit-source-id: eade1f7ce260b9b3406d92af8255b5ffa4e4a51c
folly/DynamicConverter.h
folly/test/DynamicConverterTest.cpp