hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit e295c34bda59ac3d22c2e971f5ab838578277228
parent 1a7e6d3e33649ee2ae21b86e276d33d5c3adb5f0
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 25 Feb 2022 02:24:47 +0100

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.

Diffstat:
Mstrings/dup.ha | 24++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/strings/dup.ha b/strings/dup.ha @@ -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); +};