提交 121598c2 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVStore: improved detection of the wrong encryption key (or file corruption)

上级 53fd89a8
...@@ -592,6 +592,11 @@ public class DataUtils { ...@@ -592,6 +592,11 @@ public class DataUtils {
while (i < size) { while (i < size) {
c = s.charAt(i++); c = s.charAt(i++);
if (c == '\\') { if (c == '\\') {
if (i == size) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT,
"Not a map: {0}", s);
}
c = s.charAt(i++); c = s.charAt(i++);
} else if (c == '\"') { } else if (c == '\"') {
break; break;
......
...@@ -10,6 +10,8 @@ import java.io.IOException; ...@@ -10,6 +10,8 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random;
import org.h2.mvstore.DataUtils; import org.h2.mvstore.DataUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
...@@ -32,6 +34,7 @@ public class TestDataUtils extends TestBase { ...@@ -32,6 +34,7 @@ public class TestDataUtils extends TestBase {
testEncodeLength(); testEncodeLength();
testFletcher(); testFletcher();
testMap(); testMap();
testMapRandomized();
testMaxShortVarIntVarLong(); testMaxShortVarIntVarLong();
testVarIntVarLong(); testVarIntVarLong();
testCheckValue(); testCheckValue();
...@@ -78,6 +81,24 @@ public class TestDataUtils extends TestBase { ...@@ -78,6 +81,24 @@ public class TestDataUtils extends TestBase {
assertEquals("1,2", m.get("c")); assertEquals("1,2", m.get("c"));
assertEquals("\"test\"", m.get("d")); assertEquals("\"test\"", m.get("d"));
} }
private void testMapRandomized() {
Random r = new Random(1);
String chars = "a_1,\\\":";
for (int i = 0; i < 1000; i++) {
StringBuilder buff = new StringBuilder();
for (int j = 0; j < 20; j++) {
buff.append(chars.charAt(r.nextInt(chars.length())));
}
try {
HashMap<String, String> map = DataUtils.parseMap(buff.toString());
assertFalse(map == null);
// ok
} catch (IllegalStateException e) {
// ok - but not another exception
}
}
}
private void testMaxShortVarIntVarLong() { private void testMaxShortVarIntVarLong() {
ByteBuffer buff = ByteBuffer.allocate(100); ByteBuffer buff = ByteBuffer.allocate(100);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论