Skip to content

exercises(all-your-base): three tests are broken because they do not free the returned memory #319

Closed
@FedericoStra

Description

@FedericoStra

In contrast to all other tests, these three forget to call defer testing.allocator.free(actual);:

test "empty list" {
const expected = [_]u32{0};
const digits = [_]u32{};
const input_base = 2;
const output_base = 10;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
try testing.expectEqualSlices(u32, &expected, actual);
}
test "single zero" {
const expected = [_]u32{0};
const digits = [_]u32{0};
const input_base = 10;
const output_base = 2;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
try testing.expectEqualSlices(u32, &expected, actual);
}
test "multiple zeros" {
const expected = [_]u32{0};
const digits = [_]u32{ 0, 0, 0 };
const input_base = 10;
const output_base = 2;
const actual = try convert(testing.allocator, &digits, input_base, output_base);
try testing.expectEqualSlices(u32, &expected, actual);
}

This forces the solution to return a slice always referencing the same global array, which is incorrect because it can be modified hence cannot be guaranteed to be zero.

See for instance these solutions: mine, stgatilov, sezoka.

Some other solutions are even more broken because they return a slice referencing a local array (hence UB): Acaccia, mhennig4711.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions