~laumann/hadb

dccd6726e88987ae945dee23918ddce0f4541dd2 — Thomas Bracht Laumann Jespersen 4 months ago 677b49e
format/dwarf: fix some issues

 - decodeabbrev() should call freeabbrevs() not just free()
 - when parsing haschildren, do a check that the input is not exhausted
2 files changed, 19 insertions(+), 4 deletions(-)

M format/dwarf/abbrev.ha
M format/dwarf/leb128.ha
M format/dwarf/abbrev.ha => format/dwarf/abbrev.ha +8 -3
@@ 28,7 28,7 @@ export fn decodeabbrev(input: []u8) (([]abbrev, size) | partialread) = {

	let ret: []abbrev = [];
	let err = true;
	defer if (err) free(ret);
	defer if (err) freeabbrevs(ret);

	// Begin parsing entries
	for (true) {


@@ 38,6 38,9 @@ export fn decodeabbrev(input: []u8) (([]abbrev, size) | partialread) = {
		p = p[r..];
		const (t, r) = readuleb128(p)!;
		p = p[r..];

		if (len(p) == 0)
			return partialread;
		const haschildren = p[0] == 1;
		p = p[1..];



@@ 64,7 67,9 @@ export fn decodeabbrev(input: []u8) (([]abbrev, size) | partialread) = {
	return (ret, read);
};

@test fn simple_abbrev() void = {
@test fn decodeabbrev() void = {
	// Example from
	// https://wiki.osdev.org/DWARF#The_.debug_abbrev_section
	const bin: []u8 = [
		0x01, 0x11, 0x01, 0x25,
		0x0E, 0x13, 0x0B, 0x03,


@@ 141,7 146,7 @@ export fn decodeabbrev(input: []u8) (([]abbrev, size) | partialread) = {
		0x01, 0x11, 0x01, 0x25, 0x0e, 0x13, 0x0b, 0x03,
		0x0e, 0x1b, 0x0e, 0x11, 0x01, 0x12, 0x01, 0x10,
		0x06, 0x00, 0x00, 0x02, 0x2e, 0x01, 0x3f, 0x0c,
		0x03, 0x0e, 0x3a, 0x0b, 0x3b, 0x0b, 0x39, 0x0b, 
		0x03, 0x0e, 0x3a, 0x0b, 0x3b, 0x0b, 0x39, 0x0b,
		0x49, 0x13, 0x11, 0x01, 0x12, 0x01, 0x40, 0x06,
		0x96, 0x42, 0x0c, 0x01, 0x13, 0x00, 0x00, 0x03,
		0x34, 0x00, 0x03, 0x08, 0x3a, 0x0b, 0x3b, 0x0b,

M format/dwarf/leb128.ha => format/dwarf/leb128.ha +11 -1
@@ 60,7 60,17 @@ fn readsleb128(in: []u8) ((sleb128, size) | partialread) = {
	};
};

// TODO tests for partialread
@test fn readuleb128partial() void = {
	const cases: [_][]u8 = [
		[],
		[0x80],
		[57+0x80, 0x80],
	];
	for (let i = 0z; i < len(cases); i += 1) {
		const res = readuleb128(cases[i]);
		assert(res is partialread);
	};
};

@test fn decodesleb128() void = {
	const cases: [_]([]u8, sleb128, size) = [