Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

HHVM-183: toJSON() should throw if bson_as_json() fails #72

Merged
merged 2 commits into from
Mar 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/MongoDB/BSON/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,24 @@ Variant HHVM_FUNCTION(MongoDBBsonToJson, const String &data)

str = bson_as_json(b, &str_len);

if (!str) {
bson_reader_destroy(reader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work. The throw is a C++ exception which immediately returns from the function. The destroy should be first, and the return Variant() can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course :)

throw MongoDriver::Utils::throwUnexpectedValueException("Could not convert BSON document to a JSON string");
}

s = String(str_len, ReserveString);
data_s = (unsigned char*) s.bufferSlice().data();
memcpy(data_s, str, str_len);
s.setSize(str_len);

bson_free(str);
} else {
bson_reader_destroy(reader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here - I guess doing the return Variant isn't really a problem, but it's dead code.

throw MongoDriver::Utils::throwUnexpectedValueException("Could not read document from BSON reader");
return Variant();
}

if (bson_reader_read(reader, &eof) || !eof) {
bson_reader_destroy(reader);
throw MongoDriver::Utils::throwUnexpectedValueException("Reading document did not exhaust input buffer");
}

Expand Down