Skip to content

Commit

Permalink
Communicate to llvm/gcc that refcount and index errors are very unlik…
Browse files Browse the repository at this point in the history
…ely.

Make refcount errors fatal.
  • Loading branch information
FeepingCreature committed Jan 3, 2024
1 parent 71e5e42 commit cc81618
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/neat/runtime/array.nt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module neat.runtime.array;

extern(C) void neat_runtime_index_oob(size_t index);

size_t checkIndex(size_t index, size_t length) {
// < 0 overflows to >= length.
if (index >= length) {
import neat.runtime.stdlib : exit, fprintf, stderr;

fprintf(stderr, "Array index out of bounds: %zd\n".ptr, index);
exit(1);
neat_runtime_index_oob(index);
}
return index;
}
11 changes: 10 additions & 1 deletion src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,19 @@ void print_backtrace()
#endif
}

void neat_runtime_refcount_violation(struct String s, ptrdiff_t *ptr)
#define FATALERROR __attribute__((cold)) __attribute__((noinline)) __attribute__((noreturn))

void FATALERROR neat_runtime_refcount_violation(struct String s, ptrdiff_t *ptr)
{
printf("<%.*s: refcount logic violated: %zd at %p\n", (int) s.length, s.ptr, *ptr, ptr);
print_backtrace();
exit(1);
}

void FATALERROR neat_runtime_index_oob(size_t index)
{
fprintf(stderr, "Array index out of bounds: %zd\n", index);
exit(1);
}

void neat_runtime_refcount_inc(struct String s, ptrdiff_t *ptr)
Expand Down

0 comments on commit cc81618

Please sign in to comment.