Bug in List add_array when reserving memory.

This commit is contained in:
Christoffer Lerno
2024-08-04 01:34:45 +02:00
parent a5b5f315d1
commit 620c67b04e
3 changed files with 25 additions and 23 deletions

View File

@@ -123,7 +123,7 @@ fn String List.to_tstring(&self)
fn void List.push(&self, Type element) @inline
{
self.ensure_capacity();
self.reserve(1);
self.entries[self.size++] = element;
}
@@ -244,7 +244,7 @@ fn void List.push_front(&self, Type type) @inline
**/
fn void List.insert_at(&self, usz index, Type type)
{
self.ensure_capacity();
self.reserve(1);
for (usz i = self.size; i > index; i--)
{
self.entries[i] = self.entries[i - 1];
@@ -401,10 +401,7 @@ macro usz List._remove_using_test(&self, ElementTest filter, bool $invert, ctx)
return size - self.size;
}
/**
* Reserve at least min_capacity
**/
fn void List.reserve(&self, usz min_capacity)
fn void List.ensure_capacity(&self, usz min_capacity) @local
{
if (!min_capacity) return;
if (self.capacity >= min_capacity) return;
@@ -433,7 +430,7 @@ fn void List.set(&self, usz index, Type value) @operator([]=)
self.entries[index] = value;
}
fn void List.ensure_capacity(&self, usz added = 1) @inline @private
fn void List.reserve(&self, usz added)
{
usz new_size = self.size + added;
if (self.capacity >= new_size) return;
@@ -441,7 +438,7 @@ fn void List.ensure_capacity(&self, usz added = 1) @inline @private
assert(new_size < usz.max / 2U);
usz new_capacity = self.capacity ? 2U * self.capacity : 16U;
while (new_capacity < new_size) new_capacity *= 2U;
self.reserve(new_capacity);
self.ensure_capacity(new_capacity);
}
// Functions for equatable types

View File

@@ -37,6 +37,7 @@
- Distinct func type would not accept direct function address assign. #1287
- Distinct inline would not implement protocol if the inlined implemented it. #1292
- Distinct inline can now be called if it is aliasing a function pointer.
- Bug in List add_array when reserving memory.
### Stdlib changes

View File

@@ -99,38 +99,42 @@ int run_subprocess(const char *name, const char **args)
return exit_status;
#else
pid_t cpid = fork();
if (cpid < 0) {
eprintf("Could not fork child process %s: %s", name, strerror(errno));
if (cpid < 0)
{
eprintf("Could not fork child process %s: %s\n", name, strerror(errno));
return -1;
}
if (cpid == 0) {
if (cpid == 0)
{
const char **args_null = NULL;
vec_add(args_null, name);
for (uint32_t i = 0; i < vec_size(args); ++i) {
for (uint32_t i = 0; i < vec_size(args); ++i)
{
vec_add(args_null, args[i]);
}
vec_add(args_null, NULL);
if (execvp(name, (char * const*)args_null) < 0) {
eprintf("Could not exec child process %s: %s", name, strerror(errno));
if (execvp(name, (char *const *)args_null) < 0)
{
eprintf("Could not exec child process %s: %s\n", name, strerror(errno));
exit(1);
}
UNREACHABLE
exit(0);
}
for (;;) {
for (;;)
{
int wstatus = 0;
if (waitpid(cpid, &wstatus, 0) < 0) {
eprintf("Could not wait on %s (pid %d): %s", name, cpid, strerror(errno));
if (waitpid(cpid, &wstatus, 0) < 0)
{
eprintf("Could not wait on %s (pid %d): %s\n", name, cpid, strerror(errno));
return -1;
}
if (WIFEXITED(wstatus)) {
return WEXITSTATUS(wstatus);
}
if (WIFEXITED(wstatus)) return WEXITSTATUS(wstatus);
if (WIFSIGNALED(wstatus)) {
if (WIFSIGNALED(wstatus))
{
eprintf("Program interrupted by signal %d.\n", WTERMSIG(wstatus));
return -1;
}