Commit f1fe2fe9 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Fix stack-use-after-scope in folly::json::serialize

Summary:
[Folly] Fix stack-use-after-scope in `folly::json::serialize` internals in `std::sort` comparison in `Printer::printObject`.

Also avoid an extra layer of indirection around the comparison function. Possibly useful because the function is called repeatedly within the loop.

Fixes #1190.

Reviewed By: vitaut

Differential Revision: D17560072

fbshipit-source-id: 787584dc1f7d70762a712869788f8d892dd19ed5
parent a6306282
......@@ -121,16 +121,18 @@ struct Printer {
newline();
if (opts_.sort_keys || opts_.sort_keys_by) {
using ref = std::reference_wrapper<decltype(o.items())::value_type const>;
std::vector<ref> refs(o.items().begin(), o.items().end());
using SortByRef = FunctionRef<bool(dynamic const&, dynamic const&)>;
auto const& sort_keys_by = opts_.sort_keys_by
? SortByRef(opts_.sort_keys_by)
: SortByRef(std::less<dynamic>());
std::sort(refs.begin(), refs.end(), [&](ref a, ref b) {
auto sort_keys_by = [&](auto begin, auto end, const auto& comp) {
std::sort(begin, end, [&](ref a, ref b) {
// Only compare keys. No ordering among identical keys.
return sort_keys_by(a.get().first, b.get().first);
return comp(a.get().first, b.get().first);
});
};
std::vector<ref> refs(o.items().begin(), o.items().end());
if (opts_.sort_keys_by) {
sort_keys_by(refs.begin(), refs.end(), opts_.sort_keys_by);
} else {
sort_keys_by(refs.begin(), refs.end(), std::less<>());
}
printKVPairs(refs.cbegin(), refs.cend());
} else {
printKVPairs(o.items().begin(), o.items().end());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment