Commit 752e0abc authored by Kenton Varda's avatar Kenton Varda

Fix bugs in format-detection heuristics.

parent 0d872f83
...@@ -643,12 +643,24 @@ private: ...@@ -643,12 +643,24 @@ private:
return p == PLAUSIBLE || p == WRONG_TYPE; return p == PLAUSIBLE || p == WRONG_TYPE;
} }
Plausibility isPlausiblyFlat(kj::ArrayPtr<const byte> prefix) { Plausibility isPlausiblyFlat(kj::ArrayPtr<const byte> prefix, uint segmentCount = 1) {
if (prefix.size() < 8) { if (prefix.size() < 8) {
// Not enough prefix to say. // Not enough prefix to say.
return PLAUSIBLE; return PLAUSIBLE;
} }
if ((prefix[0] & 3) == 2) {
// Far pointer. Verify the segment ID.
uint32_t segmentId = prefix[4] | (prefix[5] << 8)
| (prefix[6] << 16) | (prefix[7] << 24);
if (segmentId == 0 || segmentId >= segmentCount) {
KJ_DBG(segmentId, segmentCount);
return IMPOSSIBLE;
} else {
return PLAUSIBLE;
}
}
if ((prefix[0] & 3) != 0) { if ((prefix[0] & 3) != 0) {
// Not a struct pointer. // Not a struct pointer.
return IMPOSSIBLE; return IMPOSSIBLE;
...@@ -700,6 +712,9 @@ private: ...@@ -700,6 +712,9 @@ private:
uint32_t segmentCount = prefix[0] | (prefix[1] << 8) uint32_t segmentCount = prefix[0] | (prefix[1] << 8)
| (prefix[2] << 16) | (prefix[3] << 24); | (prefix[2] << 16) | (prefix[3] << 24);
// Actually, the bytes store segmentCount - 1.
++segmentCount;
if (segmentCount > 65536) { if (segmentCount > 65536) {
// While technically possible, this is so implausible that we should mark it impossible. // While technically possible, this is so implausible that we should mark it impossible.
// This helps to make sure we fail fast on packed input. // This helps to make sure we fail fast on packed input.
...@@ -709,8 +724,8 @@ private: ...@@ -709,8 +724,8 @@ private:
return IMPLAUSIBLE; return IMPLAUSIBLE;
} }
uint32_t segment0Size = prefix[5] | (prefix[6] << 8) uint32_t segment0Size = prefix[4] | (prefix[5] << 8)
| (prefix[7] << 16) | (prefix[8] << 24); | (prefix[6] << 16) | (prefix[7] << 24);
if (segment0Size > (1 << 27)) { if (segment0Size > (1 << 27)) {
// Segment larger than 1G seems implausible. // Segment larger than 1G seems implausible.
...@@ -728,7 +743,7 @@ private: ...@@ -728,7 +743,7 @@ private:
return PLAUSIBLE; return PLAUSIBLE;
} }
return isPlausiblyFlat(prefix.slice(segment0Offset, prefix.size())); return isPlausiblyFlat(prefix.slice(segment0Offset, prefix.size()), segmentCount);
} }
Plausibility isPlausiblyPacked(kj::ArrayPtr<const byte> prefix) { Plausibility isPlausiblyPacked(kj::ArrayPtr<const byte> prefix) {
......
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