1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: isolate, fix and regress-test sumtype cast causing memory corruption (#12329)

This commit is contained in:
Larpon
2021-10-28 18:57:30 +02:00
committed by GitHub
parent fa02418a55
commit 5e4594a121
5 changed files with 31 additions and 14 deletions

View File

@@ -35,18 +35,13 @@ fn test_parse_compact_text() {
assert title == toml.Any('TOML Example')
assert title as string == 'TOML Example'
owner := toml_doc.value('owner') as map[string]toml.Any
any_name := owner.value('name') or { panic(err) }
assert any_name.string() == 'Tom Preston-Werner'
database := toml_doc.value('database') as map[string]toml.Any
db_serv := database['server'] or {
panic('could not access "server" index in "database" variable')
}
assert db_serv as string == '192.168.1.1'
// TODO BUG depending on WHAT directory the tests is run from, this one assert sometimes fail?!?!
// assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
assert toml_doc.value('database.server') as string == '192.168.1.1'

View File

@@ -0,0 +1,24 @@
// This tests the `toml` module for a known memory corruption.
// The BUG shows below if no string `.clone()` nor any garbage-collection is done...
import os
import toml
const toml_text = os.read_file(os.real_path(os.join_path(os.dir(@FILE), 'testdata', 'toml_test')) +
'.toml') or { panic(err) }
fn test_toml_known_memory_corruption() {
toml_doc := toml.parse(toml_text) or { panic(err) }
owner := toml_doc.value('owner') as map[string]toml.Any
any_name := owner.value('name') or { panic(err) }
// This assert code path will cause the corruption.
assert any_name.string() == 'Tom Preston-Werner'
// This code then triggered the bug before the fix.
// Also see note in toml/any.v in function `pub fn (a Any) string() string`
assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
// Repeat the pattern
assert any_name.string() == 'Tom Preston-Werner'
assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
}

View File

@@ -24,18 +24,13 @@ fn test_toml() {
assert title == toml.Any('TOML Example')
assert title as string == 'TOML Example'
owner := toml_doc.value('owner') as map[string]toml.Any
any_name := owner.value('name') or { panic(err) }
assert any_name.string() == 'Tom Preston-Werner'
database := toml_doc.value('database') as map[string]toml.Any
db_serv := database['server'] or {
panic('could not access "server" index in "database" variable')
}
assert db_serv as string == '192.168.1.1'
// TODO BUG depending on WHAT directory the tests is run from, this one assert sometimes fail?!?!
// assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
assert toml_doc.value('owner.name') as string == 'Tom Preston-Werner'
assert toml_doc.value('database.server') as string == '192.168.1.1'