~sircmpwn/hare

e295c34bda59ac3d22c2e971f5ab838578277228 — Haelwenn (lanodan) Monnier 2 years ago 1a7e6d3
strings: Fix dupall segfaulting on empty array

Note: Without append you get the following:
    ./strings/dup.ha:32:2: slice or array access out of bounds

This is effectively the same design as the code for hare::ast::ident_dup
before it got replaced with strings::dupall in commit
e0c55f90741758207b580df22d776af6976f3a70
Which causes a crash when hare::module::_walk finds an empty directory.
1 files changed, 22 insertions(+), 2 deletions(-)

M strings/dup.ha
M strings/dup.ha => strings/dup.ha +22 -2
@@ 27,9 27,9 @@ export fn dup(s: const str) str = {
// Creates a copy of a []str slice with all the strings duplicated. The result
// must be freed using [[freeall]].
export fn dupall(s: []str) []str = {
	let newsl = alloc([""...], len(s));
	let newsl: []str = alloc([], len(s));
	for (let i = 0z; i < len(s); i += 1) {
		newsl[i] = dup(s[i]);
		append(newsl, dup(s[i]));
	};
	return newsl;
};


@@ 51,3 51,23 @@ export fn freeall(s: []str) void = {
	assert(s == "hello");
	free(s);
};

@test fn dupall() void = {
	const payload: []str = [];

	let s = dupall(payload);
	assert(len(s) == len(payload));
	for (let i = 0z; i < len(s); i += 1) {
		assert(s[i] == payload[i]);
	};
	freeall(s);

	const payload: []str = ["a", "aaa"];

	let s = dupall(payload);
	assert(len(s) == len(payload));
	for (let i = 0z; i < len(s); i += 1) {
		assert(s[i] == payload[i]);
	};
	freeall(s);
};