Skip to content

Commit

Permalink
Merge 40c474b into c669846
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteemann committed Jun 27, 2022
2 parents c669846 + 40c474b commit 381c9f7
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 106 deletions.
29 changes: 24 additions & 5 deletions include/velocypack/Builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ class Builder {
openCompoundValue(unindexed ? 0x14 : 0x0b);
}

template<typename T>
uint8_t* addUnchecked(Slice key, T const& sub) {
bool needCleanup = !_stack.empty();
if (needCleanup) {
reportAdd();
}
try {
set(key);
return writeValue(sub);
} catch (...) {
// clean up in case of an exception
if (needCleanup) {
cleanupAdd();
}
throw;
}
}

template<typename T>
uint8_t* addUnchecked(std::string_view attrName, T const& sub) {
bool needCleanup = !_stack.empty();
Expand Down Expand Up @@ -496,7 +514,7 @@ class Builder {
}
}
try {
checkKeyIsString(Slice(sub));
checkKeyHasValidType(Slice(sub));
auto oldPos = _pos;
reserve(1 + sizeof(void*));
// store pointer. this doesn't need to be portable
Expand Down Expand Up @@ -700,23 +718,24 @@ class Builder {

void appendTag(uint64_t tag);

inline void checkKeyIsString(bool isString) {
inline void checkKeyHasValidType(bool isValid) {
if (!_stack.empty()) {
ValueLength const pos = _stack.back().startPos;
if (_start[pos] == 0x0b || _start[pos] == 0x14) {
if (VELOCYPACK_UNLIKELY(!_keyWritten && !isString)) {
if (VELOCYPACK_UNLIKELY(!_keyWritten && !isValid)) {
throw Exception(Exception::BuilderKeyMustBeString);
}
_keyWritten = !_keyWritten;
}
}
}

inline void checkKeyIsString(Slice item) {
inline void checkKeyHasValidType(Slice item) {
if (!_stack.empty()) {
ValueLength pos = _stack.back().startPos;
if (_start[pos] == 0x0b || _start[pos] == 0x14) {
if (VELOCYPACK_UNLIKELY(!_keyWritten && !item.isString())) {
if (VELOCYPACK_UNLIKELY(!_keyWritten && !item.isString() &&
!item.isSmallInt() && !item.isUInt())) {
throw Exception(Exception::BuilderKeyMustBeString);
}
_keyWritten = !_keyWritten;
Expand Down
8 changes: 5 additions & 3 deletions src/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,8 @@ uint8_t* Builder::set(Value const& item) {
auto const oldPos = _pos;
auto ctype = item.cType();

checkKeyIsString(item.valueType() == ValueType::String);
checkKeyHasValidType(item.valueType() == ValueType::String ||
item.valueType() == ValueType::UInt);

// This method builds a single further VPack item at the current
// append position. If this is an array or object, then an index
Expand Down Expand Up @@ -1159,7 +1160,7 @@ uint8_t* Builder::set(Value const& item) {
}

uint8_t* Builder::set(Slice const& item) {
checkKeyIsString(item);
checkKeyHasValidType(item);

if (VELOCYPACK_UNLIKELY(options->disallowCustom && item.isCustom())) {
// Custom values explicitly disallowed as a security precaution
Expand All @@ -1181,7 +1182,8 @@ uint8_t* Builder::set(ValuePair const& pair) {

auto const oldPos = _pos;

checkKeyIsString(pair.valueType() == ValueType::String);
checkKeyHasValidType(pair.valueType() == ValueType::String ||
pair.valueType() == ValueType::UInt);

if (pair.valueType() == ValueType::String) {
uint64_t size = pair.getSize();
Expand Down
3 changes: 2 additions & 1 deletion src/asm-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
extern std::size_t (*JSONStringCopy)(uint8_t*, uint8_t const*, std::size_t);

// Now a version which also stops at high bit set bytes:
extern std::size_t (*JSONStringCopyCheckUtf8)(uint8_t*, uint8_t const*, std::size_t);
extern std::size_t (*JSONStringCopyCheckUtf8)(uint8_t*, uint8_t const*,
std::size_t);

// White space skipping:
extern std::size_t (*JSONSkipWhiteSpace)(uint8_t const*, std::size_t);
Expand Down
6 changes: 3 additions & 3 deletions src/asm-utf8check.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool validate_utf8_fast_sse42(uint8_t const* src, std::size_t len);
#ifdef __AVX2__
bool validate_utf8_fast_avx_asciipath(char const* src, std::size_t len);
bool validate_utf8_fast_avx(uint8_t const* src, std::size_t len);
#endif // __AVX2__
#endif // ASM_OPTIMIZATIONS
#endif // __AVX2__
#endif // ASM_OPTIMIZATIONS

} // namespace arangodb::velocypack
132 changes: 45 additions & 87 deletions src/powers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,93 +16,51 @@ typedef struct Fp {
int exp;
} Fp;

static Fp powers_ten[] = {{18054884314459144840U, -1220},
{13451937075301367670U, -1193},
{10022474136428063862U, -1166},
{14934650266808366570U, -1140},
{11127181549972568877U, -1113},
{16580792590934885855U, -1087},
{12353653155963782858U, -1060},
{18408377700990114895U, -1034},
{13715310171984221708U, -1007},
{10218702384817765436U, -980},
{15227053142812498563U, -954},
{11345038669416679861U, -927},
{16905424996341287883U, -901},
{12595523146049147757U, -874},
{9384396036005875287U, -847},
{13983839803942852151U, -821},
{10418772551374772303U, -794},
{15525180923007089351U, -768},
{11567161174868858868U, -741},
{17236413322193710309U, -715},
{12842128665889583758U, -688},
{9568131466127621947U, -661},
{14257626930069360058U, -635},
{10622759856335341974U, -608},
{15829145694278690180U, -582},
{11793632577567316726U, -555},
{17573882009934360870U, -529},
{13093562431584567480U, -502},
{9755464219737475723U, -475},
{14536774485912137811U, -449},
{10830740992659433045U, -422},
{16139061738043178685U, -396},
{12024538023802026127U, -369},
{17917957937422433684U, -343},
{13349918974505688015U, -316},
{9946464728195732843U, -289},
{14821387422376473014U, -263},
{11042794154864902060U, -236},
{16455045573212060422U, -210},
{12259964326927110867U, -183},
{18268770466636286478U, -157},
{13611294676837538539U, -130},
{10141204801825835212U, -103},
{15111572745182864684U, -77},
{11258999068426240000U, -50},
{16777216000000000000U, -24},
{12500000000000000000U, 3},
{9313225746154785156U, 30},
{13877787807814456755U, 56},
{10339757656912845936U, 83},
{15407439555097886824U, 109},
{11479437019748901445U, 136},
{17105694144590052135U, 162},
{12744735289059618216U, 189},
{9495567745759798747U, 216},
{14149498560666738074U, 242},
{10542197943230523224U, 269},
{15709099088952724970U, 295},
{11704190886730495818U, 322},
{17440603504673385349U, 348},
{12994262207056124023U, 375},
{9681479787123295682U, 402},
{14426529090290212157U, 428},
{10748601772107342003U, 455},
{16016664761464807395U, 481},
{11933345169920330789U, 508},
{17782069995880619868U, 534},
{13248674568444952270U, 561},
{9871031767461413346U, 588},
{14708983551653345445U, 614},
{10959046745042015199U, 641},
{16330252207878254650U, 667},
{12166986024289022870U, 694},
{18130221999122236476U, 720},
{13508068024458167312U, 747},
{10064294952495520794U, 774},
{14996968138956309548U, 800},
{11173611982879273257U, 827},
{16649979327439178909U, 853},
{12405201291620119593U, 880},
{9242595204427927429U, 907},
{13772540099066387757U, 933},
{10261342003245940623U, 960},
{15290591125556738113U, 986},
{11392378155556871081U, 1013},
{16975966327722178521U, 1039},
{12648080533535911531U, 1066}};
static Fp powers_ten[] = {
{18054884314459144840U, -1220}, {13451937075301367670U, -1193},
{10022474136428063862U, -1166}, {14934650266808366570U, -1140},
{11127181549972568877U, -1113}, {16580792590934885855U, -1087},
{12353653155963782858U, -1060}, {18408377700990114895U, -1034},
{13715310171984221708U, -1007}, {10218702384817765436U, -980},
{15227053142812498563U, -954}, {11345038669416679861U, -927},
{16905424996341287883U, -901}, {12595523146049147757U, -874},
{9384396036005875287U, -847}, {13983839803942852151U, -821},
{10418772551374772303U, -794}, {15525180923007089351U, -768},
{11567161174868858868U, -741}, {17236413322193710309U, -715},
{12842128665889583758U, -688}, {9568131466127621947U, -661},
{14257626930069360058U, -635}, {10622759856335341974U, -608},
{15829145694278690180U, -582}, {11793632577567316726U, -555},
{17573882009934360870U, -529}, {13093562431584567480U, -502},
{9755464219737475723U, -475}, {14536774485912137811U, -449},
{10830740992659433045U, -422}, {16139061738043178685U, -396},
{12024538023802026127U, -369}, {17917957937422433684U, -343},
{13349918974505688015U, -316}, {9946464728195732843U, -289},
{14821387422376473014U, -263}, {11042794154864902060U, -236},
{16455045573212060422U, -210}, {12259964326927110867U, -183},
{18268770466636286478U, -157}, {13611294676837538539U, -130},
{10141204801825835212U, -103}, {15111572745182864684U, -77},
{11258999068426240000U, -50}, {16777216000000000000U, -24},
{12500000000000000000U, 3}, {9313225746154785156U, 30},
{13877787807814456755U, 56}, {10339757656912845936U, 83},
{15407439555097886824U, 109}, {11479437019748901445U, 136},
{17105694144590052135U, 162}, {12744735289059618216U, 189},
{9495567745759798747U, 216}, {14149498560666738074U, 242},
{10542197943230523224U, 269}, {15709099088952724970U, 295},
{11704190886730495818U, 322}, {17440603504673385349U, 348},
{12994262207056124023U, 375}, {9681479787123295682U, 402},
{14426529090290212157U, 428}, {10748601772107342003U, 455},
{16016664761464807395U, 481}, {11933345169920330789U, 508},
{17782069995880619868U, 534}, {13248674568444952270U, 561},
{9871031767461413346U, 588}, {14708983551653345445U, 614},
{10959046745042015199U, 641}, {16330252207878254650U, 667},
{12166986024289022870U, 694}, {18130221999122236476U, 720},
{13508068024458167312U, 747}, {10064294952495520794U, 774},
{14996968138956309548U, 800}, {11173611982879273257U, 827},
{16649979327439178909U, 853}, {12405201291620119593U, 880},
{9242595204427927429U, 907}, {13772540099066387757U, 933},
{10261342003245940623U, 960}, {15290591125556738113U, 986},
{11392378155556871081U, 1013}, {16975966327722178521U, 1039},
{12648080533535911531U, 1066}};

static Fp find_cachedpow10(int exp, int* k) {
const double one_log_ten = 0.30102999566398114;
Expand Down
30 changes: 23 additions & 7 deletions tests/testsBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2749,6 +2749,28 @@ TEST(BuilderTest, AddKeysSeparately2) {
ASSERT_EQ(R"({"bar":{},"baz":1,"bumm":[13],"foo":[]})", b.toJson());
}

TEST(BuilderTest, AddKeysSeparatelySmallIntKey) {
uint8_t buf[] = {0x31};
Slice s(buf);

Builder b;
b.openObject();
// add integer key
ASSERT_NE(nullptr, b.add(s));
b.close();
}

TEST(BuilderTest, AddKeysSeparatelyUIntKey) {
uint8_t buf[] = {0x28, 0x01};
Slice s(buf);

Builder b;
b.openObject();
// add integer key
ASSERT_NE(nullptr, b.add(s));
b.close();
}

TEST(BuilderTest, AddKeysSeparatelyFail) {
{
Builder b;
Expand Down Expand Up @@ -2786,8 +2808,8 @@ TEST(BuilderTest, AddKeysSeparatelyFail) {
ASSERT_VELOCYPACK_EXCEPTION(b.add(Value(12, ValueType::UTCDate)),
Exception::BuilderKeyMustBeString);
}
uint8_t buf[] = {0x31};
{
uint8_t buf[] = {0x31};
Builder b;
b.openObject();
ASSERT_VELOCYPACK_EXCEPTION(b.add(Value(&buf, ValueType::External)),
Expand Down Expand Up @@ -2823,12 +2845,6 @@ TEST(BuilderTest, AddKeysSeparatelyFail) {
ASSERT_VELOCYPACK_EXCEPTION(b.add(Value(113)),
Exception::BuilderKeyMustBeString);
}
Slice s(buf);
{
Builder b;
b.openObject();
ASSERT_VELOCYPACK_EXCEPTION(b.add(s), Exception::BuilderKeyMustBeString);
}
{
Builder b;
b.openObject();
Expand Down

0 comments on commit 381c9f7

Please sign in to comment.