Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b89579a5e7 | ||
|
|
fbd97f7112 | ||
|
|
6b7a560156 |
@@ -0,0 +1,8 @@
|
|||||||
|
Checks: >
|
||||||
|
-*,
|
||||||
|
clang-analyzer-*,
|
||||||
|
bugprone-*,
|
||||||
|
-bugprone-easily-swappable-parameters,
|
||||||
|
-bugprone-reserved-identifier,
|
||||||
|
performance-*,
|
||||||
|
security-*
|
||||||
@@ -1,2 +1,5 @@
|
|||||||
build/
|
build/
|
||||||
.cache/
|
.cache/
|
||||||
|
tests/cache/
|
||||||
|
tests/bin/
|
||||||
|
coverage.html
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
# AGENTS.md
|
|
||||||
|
|
||||||
## Identity & Core Philosophy
|
|
||||||
You are an expert bug-hunter agent.
|
|
||||||
Speak and reason in **Caveman style** (why use many token when few token do trick).
|
|
||||||
|
|
||||||
### Caveman Communication Rules
|
|
||||||
- Drop articles (a/an/the), filler (just/really/basically/actually/simply), pleasantries, and greetings.
|
|
||||||
- Short, dense, direct phrases. Fragment sentences okay.
|
|
||||||
- Never summarize files read. Report only findings.
|
|
||||||
- **No tool-call narration:** Fire tools directly. Never explain what you are about to inspect or have just inspected.
|
|
||||||
- **Tokenizer rules:** NEVER invent abbreviations (cfg/impl/req/res/fn) or use arrows (→); tokenizers split them. Keep full words.
|
|
||||||
- **Safety rules:** NEVER drop `not`, `never`, `no`, `only`, `except` (flips meaning).
|
|
||||||
- **Exceptions (Do NOT caveman):** Code snippets, file paths, line numbers, command names, and error traces remain exact and intact.
|
|
||||||
- **Output pattern:** `[thing] [action] [reason]. [next step].`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Strict Permissions & Boundaries
|
|
||||||
1. **Target Task:** Read codebase, hunt bugs, identify security flaws, edge-case failures, or performance bottlenecks.
|
|
||||||
2. **File Mutation Constraint:**
|
|
||||||
- **YOU MAY ONLY CREATE OR EDIT:** `BUGS.md`
|
|
||||||
- **ABSOLUTE BAN:** You are strictly forbidden from modifying, patching, refactoring, deleting, or creating ANY other files in the workspace (no source files, configs, tests, or documentation).
|
|
||||||
- If user asks you to fix code directly, REFUSE in caveman: "No touch code. Rule say only log to BUGS.md."
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Operating Instructions
|
|
||||||
1. Inspect files in read-only mode using available search/read tools. Call them immediately without introductory text.
|
|
||||||
2. Read existing `BUGS.md` first:
|
|
||||||
- Identify existing style, headers, keys, table columns, or list formats.
|
|
||||||
- You MUST mirror existing format, structure, and naming conventions exactly.
|
|
||||||
- If `BUGS.md` is empty or missing, use a minimal markdown list: `File:Line | Severity | Issue | Recommended fix`.
|
|
||||||
3. For every confirmed issue found:
|
|
||||||
- Append or edit entry in `BUGS.md` following matched format.
|
|
||||||
- Keep bug descriptions in `BUGS.md` terse and caveman-style.
|
|
||||||
4. Do not patch or edit source code under any circumstances.
|
|
||||||
@@ -1,325 +1,270 @@
|
|||||||
# libvec bug tracker
|
# libvec bug tracker
|
||||||
|
|
||||||
> **Verified** `char.c` @ **2026-09-28 12:48:04**, 3 independent passes.
|
**Pass 12** — verified **2026-10-12** — `char.c` @ 957 lines · `char.h` @ 108
|
||||||
> `char.h` 108 lines, `utils.h` 31, `utils.c` 12, `CMakeLists.txt` 32, `main.c` 55.
|
|
||||||
> Line numbers are the *only* field that drifts. Re-verify before trusting them.
|
|
||||||
>
|
|
||||||
> **Pass 4** @ **2026-09-28**, `push_back` / `pop_back` (char.c:67-157), ASan.
|
|
||||||
> **Pass 5** @ **2026-09-28**, whole file re-read plus 10 harness runs, ASan.
|
|
||||||
> Line count moved 804 to 800 with the `create` rewrite; re-measured per pass.
|
|
||||||
|
|
||||||
**Open 79** — 11 S1, 27 S2, 11 S3, 29 C
|
| Open | S1 | S2 | S3 | C | DECIDE | Fixed |
|
||||||
**1 DECIDE** · **Fixed 12** (listed at the bottom, no rows) · 79 rows
|
|:----:|:--:|:--:|:--:|:-:|:------:|:-----:|
|
||||||
|
| **42** | 8 | 15 | 7 | 12 | 0 | 63 |
|
||||||
|
|
||||||
**Severity** `S1` memory safety · `S2` wrong result / broken contract ·
|
`S1` memory safety · `S2` wrong result / broken contract · `S3` edge case · `C` cleanup · `DECIDE` open design question
|
||||||
`S3` edge case · `C` cleanup · `DECIDE` open design question
|
|
||||||
|
|
||||||
## How to read this
|
> **Line numbers are the only field that drifts.** Re-verify before trusting them.
|
||||||
|
|
||||||
Each function is a `###` with one table. Rows are sorted by severity (S1 first),
|
|
||||||
so the worst thing in each function is on top. Fixed findings are **not** rows —
|
|
||||||
they are a flat ID list at the bottom of the file.
|
|
||||||
|
|
||||||
Tracking state is a **column**, not a checkbox: a `State` cell carries more than
|
|
||||||
"done or not done" — `DECIDE` has no checkbox equivalent, and it is load-bearing
|
|
||||||
here. And task-list syntax inside a table cell is renderer-dependent, so a
|
|
||||||
`State` column is the form that survives any markdown preview. If your preview
|
|
||||||
*does* render checkboxes in tables and you want click-to-toggle, a leading `Done`
|
|
||||||
column can be added without disturbing anything else.
|
|
||||||
|
|
||||||
## Rules
|
|
||||||
|
|
||||||
1. **IDs are permanent.** Never reused, never renumbered. An ID leaves the file
|
|
||||||
only when its row is deleted outright, and only under rule 3.
|
|
||||||
2. **Locations drift freely.** The only field updated in place.
|
|
||||||
3. **False findings are deleted, not archived.** If a row turns out to be my own
|
|
||||||
error, the row is removed. A new ID carries the accurate finding if there is
|
|
||||||
one. No `DISPUTED` state, no corrections log — a tracker is not a diary.
|
|
||||||
4. **State** is `OPEN` / `DECIDE`. `FIXED` means the row is gone and its ID is
|
|
||||||
in the bottom list.
|
|
||||||
5. **Fixed findings live in the ID list at the bottom**, nothing else.
|
|
||||||
|
|
||||||
## Triage — the 11 open S1s, in suggested order
|
|
||||||
|
|
||||||
| # | ID | Location | Summary |
|
|
||||||
|----|---|---|---|
|
|
||||||
| 1 | <code><nobr>RS-7</nobr></code> | char.c:175, 200 | `reserve` on a blank vector allocates 1024 but claims 5000. Ordinary `size`, live overflow |
|
|
||||||
| 3 | <code><nobr>PB-8</nobr></code> | char.c:89, 131 | live heap overflow, 2-line repro |
|
|
||||||
| 4 | <code><nobr>PB-14</nobr></code> | char.c:131, 154 | write through the freed buffer after `delete`, 2-line repro |
|
|
||||||
| 5 | <code><nobr>RS-1</nobr></code> | char.c:180-195 | use-after-free in `reserve`'s truncation loop, ASan confirmed |
|
|
||||||
| 6 | <code><nobr>IN-1</nobr></code> | char.c:783 | inverted `iter_end > iter_start` |
|
|
||||||
| 7 | <code><nobr>IN-2</nobr></code> | char.c:783 | dead bounds check sitting behind IN-1 |
|
|
||||||
| 8 | <code><nobr>DL-1</nobr></code> | char.c:293, 297 | `const` delete, double free on second call |
|
|
||||||
| 9 | <code><nobr>DL-2</nobr></code> | char.h:11-17 | no ownership marker, struct copy double-frees |
|
|
||||||
| 8 | <code><nobr>UT-2</nobr></code> | utils.c:3-12 | `error_messages[0]` / `[1]` are `NULL` |
|
|
||||||
| 9 | <code><nobr>PB-3</nobr></code> | char.c:106, 116 | capacity committed before the realloc check |
|
|
||||||
| 10 | <code><nobr>PB-15</nobr></code> | char.c:87, 128-131 | null-buffer path has no `else`, so `arr == NULL && size > 0` writes to NULL. Defensive |
|
|
||||||
|
|
||||||
> **Two of the eleven are the same class.** RS-7 and PB-8 both end in a
|
|
||||||
> write past a small block, and both come from a struct field claiming more
|
|
||||||
> bytes than were allocated: `capacity` set from the request rather than the
|
|
||||||
> block (RS-7, char.c:200), `calloc(1)` with `capacity` untouched (PB-8,
|
|
||||||
> char.c:89). One rule kills both: **never write `_internal_capacity` without
|
|
||||||
> writing the allocation it describes.** Verify with a single postcondition —
|
|
||||||
> after any function that touches capacity, the block size and the field must
|
|
||||||
> agree. CR-8 was a third member of this class; it is fixed as of Pass 5, and
|
|
||||||
> `create` now carries the same postcondition in one place (CR-9, char.c:44).
|
|
||||||
|
|
||||||
> **Fix IN-1 and IN-2 in the same commit.** The raw read in the 3-arg overload
|
|
||||||
> is unreachable *only because* IN-1's inversion empties the loop. Fixing IN-1
|
|
||||||
> alone converts a clean rejection into a live out-of-bounds read, because IN-2's
|
|
||||||
> bound check stays dead behind the short-circuit.
|
|
||||||
|
|
||||||
> **PB-8 and PB-14 are the only two rows in this file with a sanitizer trace
|
|
||||||
> attached.** Both reproduce in three lines from `main`. `delete` should null
|
|
||||||
> the struct's fields (DL-1) and push should size the materialization from
|
|
||||||
> `_internal_capacity` (PB-8); those two fixes close the whole S1 set for this
|
|
||||||
> function pair.
|
|
||||||
|
|
||||||
> **Reproducer harness**: `clang -std=c2x -g -fsanitize=address -Isrc/lib
|
|
||||||
> -Isrc/lib/char t.c src/lib/char/char.c src/lib/utils/utils.c`. `-std=c2x` is
|
|
||||||
> required — `char.c` uses `nullptr`, so the project does not compile as C17.
|
|
||||||
> Reproducers that ask for a multi-exabyte `calloc` additionally need
|
|
||||||
> `ASAN_OPTIONS=allocator_may_return_null=1`, or ASan aborts on the 2^61-byte
|
|
||||||
> `calloc` before the interesting line runs.
|
|
||||||
|
|
||||||
> **Defensive-only rows.** RZ-2, RS-2, PB-15 and PB-18 are not reachable
|
|
||||||
> without an allocation of ~2^61 bytes, or a hand-built struct. Kept as
|
|
||||||
> hardening because the guards are cheap and the invariant is worth stating.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Bugs
|
## Contents
|
||||||
|
|
||||||
### `_vec_char_push_back` / `_vec_char_pop_back` — char.c:71, 136
|
- [Triage — the 8 open S1s](#triage)
|
||||||
|
- [Findings](#findings)
|
||||||
|
- [Core storage](#core-storage)
|
||||||
|
- [Validation & accessors](#validation--accessors)
|
||||||
|
- [Algorithms](#algorithms)
|
||||||
|
- [Support](#support)
|
||||||
|
- [Fixed](#fixed)
|
||||||
|
- [ID map](#id-map)
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
---
|
||||||
|-----|---|---|---|---|
|
|
||||||
| `PB-8` | S1 | char.c:89, 131 | `calloc(1)` on the null-buffer path leaves `_internal_capacity` at 1024, so the 2nd `push_back` writes `arr[1]` one byte past a 1-byte block. **ASan: `heap-buffer-overflow` WRITE of size 1 at char.c:131**, block allocated at char.c:89 | OPEN |
|
|
||||||
| <code><nobr>PB-14</nobr></code> | S1 | char.c:75, 131, 154 | neither function checks liveness. `_vec_char_delete` frees without clearing the struct (DL-1), so `delete(&v); push_back(&v,'z')` writes through the freed pointer. **ASan: `heap-use-after-free` WRITE of size 1 at char.c:131**, freed at char.c:297. Same for `pop_back`, which writes `'\0'` at char.c:154 | OPEN |
|
|
||||||
| <code><nobr>PB-15</nobr></code> | S1 | char.c:87, 128-131 | the materialize branch is guarded by `status == ERR_BUF_NULL && _internal_size == 0` and has no `else`. Any state with `arr == NULL && _internal_size > 0` falls through to char.c:126, takes `res = ptr->_internal_arr` (NULL) and writes `arr[size]`. No in-file producer, but nothing checks the invariant either | OPEN |
|
|
||||||
| <code><nobr>PB-3</nobr></code> | S1 | char.c:106, 116 | `_internal_capacity` assigned before the `!res` check; on OOM the struct overstates its capacity and the next call writes past the real block. Both branches | OPEN |
|
|
||||||
| <code><nobr>PB-9</nobr></code> | S2 | char.c:85-99| `push_back` materializes a null vector, making it a second path alongside `reserve`; the two disagree on allocation size (1 byte vs 1024) | OPEN |
|
|
||||||
| <code><nobr>PB-16</nobr></code> | S2 | char.c:116, 131 vs 154 | the growth `realloc` leaves the new tail uninitialized and `push_back` never writes a terminator at `_internal_size`, so `_vec_char_data` hands back a non-NUL-terminated buffer — `strlen(data(&v))` runs off the end. **ASan: `heap-buffer-overflow` READ of 2049 bytes past a 2048-byte region** grown at char.c:116. The lone `'\0'` write in either function is `pop_back`'s vacated slot, so termination is accidental | OPEN |
|
|
||||||
| <code><nobr>PB-21</nobr></code> | S2 | char.c:81, 104 | after any `reserve` the vector is permanently full: reserve sets `max_size = capacity`, so char.c:81 fails for good and `push_back` can only return `ERR_MAX_PASSED`. Duplicate view of AS-4, kept so this table stands alone | OPEN |
|
|
||||||
| <code><nobr>PB-17</nobr></code> | S3 | char.c:144-147 vs 87 | `pop_back` refuses to materialize while `push_back` does. A blank vector reports `ERR_BUF_NULL` (21) and logs a spurious "Buffer :- NULL" instead of `ERR_SIZE_ZERO` (22), so a `while (pop_back(&v) == SUCCESS)` drain reports the wrong reason | OPEN |
|
|
||||||
| <code><nobr>PB-18</nobr></code> | S3 | char.c:103, 116, 120-124 | `_internal_capacity == 0` makes `dst_cap` 0, and `realloc(arr, 0)` frees the block. The `!res` check then returns `ERR_ALLOC_FAIL` with `_internal_arr` dangling and capacity already committed as 0; the next push writes to freed memory. Capacity 0 is unreachable from this file today, so defensive | OPEN |
|
|
||||||
| <code><nobr>PB-20</nobr></code> | S3 | char.c:154 | only the vacated slot is zeroed, so bytes past `_internal_size` keep their values and are readable out of band through `data()`. `clear` zeroes the whole range and `erase` zeroes the tail; `pop_back` is the inconsistent one | OPEN |
|
|
||||||
|
|
||||||
> PB-1 and PB-2 were originally described as firing "on the very next call" and
|
<a id="triage"></a>
|
||||||
> as "the clamp silently does nothing". Both claims were wrong in detail: PB-1
|
|
||||||
> needed `capacity < size+1 <= max_size < 2*capacity`, a band today's `VEC_MAX`
|
|
||||||
> never reaches, and PB-2's cause was `cap` being recomputed from the
|
|
||||||
> already-clobbered `capacity`, not the branch being skipped.
|
|
||||||
|
|
||||||
> **Pass-4 note.** Four pre-existing rows here were my own errors and were
|
## Triage — the 8 open S1s
|
||||||
> deleted: a "dead else" that is the common path, two `is_valid` double-call
|
|
||||||
> claims in a function that hoists, and an overflow that `VEC_MAX` makes
|
|
||||||
> impossible. Found by diffing the wording against the source, not by testing.
|
|
||||||
> The three new S1/S2 rows *were* tested; the reproducer command is above.
|
|
||||||
|
|
||||||
### `_vec_char_create` — char.c:23
|
In suggested fixing order.
|
||||||
|
|
||||||
> **Pass-5 note.** `create`'s growth block was rewritten this pass and the
|
| # | ID | Location | Summary |
|
||||||
> rewrite is correct. The invariant it now enforces in one place is
|
|:-:|:---|:---------|:--------|
|
||||||
> `size <= _internal_capacity <= _internal_max_size` on return, held by the
|
| 1 | `IV-5` | char.c:40-44 | `__null_check`'s NULL-`status` branch derefs the very NULL it just tested, and seven call sites pass the caller's `err` through here → `SEGV` at 42. **ASan Pass 12: SEGV in `__null_check`** |
|
||||||
> commit-back at char.c:44. Verified under ASan at `size` 0, 1, 1023, 1024,
|
| 2 | `FB-1` | char.c:457-462, 475-480 | `front`/`back` guard inverted: populated vectors return 0 + `ERR_OUT_OF_BOUNDS`; drained vectors fall through to `*nullptr` → SEGV at 462/480. `back` probes index 0 |
|
||||||
> 1025, 2047, 2048, 2049, 2999, 5000 and `VEC_MAX - 1`: `capacity >= size` on
|
| 3 | `IN-10` | char.c:886-893 | amount-insert never validates: guard at 887 reads a fresh local (always 0), NULL `ptr` derefs at 893 → SEGV |
|
||||||
> every call, the `calloc` block matches the reported capacity, no overflow.
|
| 4 | `ER-4` | char.c:541-542 | range-erase zeroing `memset` length is the whole `_internal_size`, not `diff`, so it writes past the buffer → `heap-buffer-overflow` WRITE at 541 |
|
||||||
> One case to know about: `create(NULL, VEC_MAX - 1)` still returns
|
| 5 | `RZ-10` | char.c:626-629, 712-715 | resize-shrink `memset` length is the whole old `_internal_size`, not `old - new` → `heap-buffer-overflow` WRITE at 628/714 |
|
||||||
> `capacity=4611686018427387902` over `arr == NULL`, because growth is correct
|
| 6 | `RZ-11` | char.c:630-634, 716-720 | resize-growth `memset` length is `_internal_arr[size]` (an element value) instead of `size - _internal_size`; reads one past the block when `size == capacity` → `heap-buffer-overflow` READ at 633/719 |
|
||||||
> and the `calloc` then fails. That is CR-3 below, and it is unchanged.
|
| 7 | `RS-1` | char.c:264-269 | truncation zeroed through the post-realloc pointer: `memset(&arr[capacity], 0, size - capacity)` (267) writes past the shrunk block → `heap-buffer-overflow` WRITE at 267 |
|
||||||
> `dst_cap * 2` at char.c:42 cannot wrap, because the loop only doubles while
|
| 8 | `DL-2` | char.h:8-14 | no ownership marker, by-value struct copy double-frees |
|
||||||
> the product stays under `_internal_max_size`. An intermediate revision of
|
|
||||||
> this fix omitted the commit-back and overflowed every `size > CAPACITY`; it
|
|
||||||
> never reached a build, so no ID was spent on it. Two `create` findings left
|
|
||||||
> the table this pass and their IDs are in the bottom list: **CR-7**, the
|
|
||||||
> second growth branch, which tested the ceiling (`capacity*2 > max_size`)
|
|
||||||
> instead of the loop's own postcondition — a superset that also fired after a
|
|
||||||
> successful grow, overwriting `capacity` with `size` and forcing the next
|
|
||||||
> `push_back` to reallocate for one byte; its `>` test also disagreed with the
|
|
||||||
> loop's `>=` exit on exactly one value, reachable only because `VEC_MAX` is
|
|
||||||
> odd. **CR-8**, the `>=` guard at char.c:32, which left `size == VEC_MAX`
|
|
||||||
> allocating 1024 bytes and copying `VEC_MAX` bytes into it. Both are gone
|
|
||||||
> because the branch they described no longer exists, not because either was
|
|
||||||
> re-filed under a new ID.
|
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
---
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>CR-6</nobr></code> | S2 | char.c:29, 39-45| null vector's `_internal_capacity` is set to `CAPACITY` (1024) while nothing is allocated. Currently **masked** by AC-1: `capacity()` returns `ERR_BUF_NULL` first, so the lie is not observable through the public API. The observable form of this same lie is PB-8, after `push_back` materializes the buffer | OPEN |
|
|
||||||
| <code><nobr>CR-3</nobr></code> | S3 | char.c:48-53 | on `calloc` failure returns a struct with an already-grown `capacity` and `arr == NULL`. Defensive. **Now reachable with an ordinary `size`, not just a huge one**: `create(NULL, 5000)` grows `capacity` to 8192 at char.c:44, then the 8192-byte `calloc` fails and the struct survives claiming 8192 over a null buffer | OPEN |
|
|
||||||
| <code><nobr>CR-4</nobr></code> | DECIDE | char.c:58-65 | no NUL terminator; is a `const char* src, size_t size` pair a byte range or a C string? Affects whether a `from_cstr` helper is needed | DECIDE |
|
|
||||||
|
|
||||||
### `_vec_char_is_valid` — char.c:6
|
<a id="findings"></a>
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
## Findings
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>IV-1</nobr></code> | S2 | char.c:6 | always returns non-zero, so `if (_vec_char_is_valid(&v))` is true for every input including NULL. Every call site compares codes by luck | OPEN |
|
|
||||||
| <code><nobr>IV-3</nobr></code> | S2 | char.c:6-17| `is_valid` is the only validation primitive in the file and it requires a non-null **buffer**, not just a non-null vector. Every accessor that only needs to validate the vector pointer therefore inherits `ERR_BUF_NULL`. This is the root cause of AC-1, AC-2, SW-1 and ST-2 | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_reserve` — char.c:156
|
<a id="core-storage"></a>
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
### Core storage
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>RS-7</nobr></code> | S1 | char.c:175, 200 | the `ERR_BUF_NULL` path allocates `ptr->_internal_capacity` bytes, not the requested `capacity`, then line 200 sets `_internal_capacity = capacity` regardless. So `reserve(&v, 5000)` on a blank vector `calloc`s **1024** bytes and the struct claims 5000. **ASan: `heap-buffer-overflow` WRITE at char.c:131, 0 bytes after a 1024-byte region allocated at char.c:175**, reached by 1026 `push_back`s. The `SUCCESS` path at char.c:184 sizes the `realloc` correctly; only the null-buffer path is wrong. Second producer of PB-8, and unlike CR-7 this one needs no huge `size` — 5000 is ordinary | OPEN |
|
|
||||||
| <code><nobr>RS-1</nobr></code> | S1 | char.c:180, 186-193, 195| realloc result held in `res`, but the truncation loop writes `ptr->_internal_arr`, which is still the pre-realloc pointer. Use-after-free when a shrinking `reserve` moves the block. Introduced by the FX-3 fix. **ASan confirmed**: `create("abcdefgh",8)` then `reserve(3)` gives `heap-use-after-free WRITE` at char.c:194 into a block freed at char.c:184 | OPEN |
|
|
||||||
| <code><nobr>RS-2</nobr></code> | S3 | char.c:160-164 | `capacity == 0` checked before the NULL-vector check, so a NULL vector with `capacity == 0` reports `ERR_SIZE_ZERO` instead of `ERR_VECTOR_NULL`. Defensive | OPEN |
|
|
||||||
| <code><nobr>RS-6</nobr></code> | C | char.c:158, 178, 195| clang `-Wsometimes-uninitialized`: `res` used uninitialized at 195 if the `if`/`else if` chain falls through. Not reachable today since `is_valid` returns exactly 3 values, but real fragility | OPEN |
|
|
||||||
| <code><nobr>RS-3</nobr></code> | C | char.c:197 | `max_size = capacity` is correct under the hard-cap semantics but is documented nowhere. **This is also the cause of ST-1 and AS-4** | OPEN |
|
|
||||||
| <code><nobr>RS-4</nobr></code> | C | char.c:188-191 | element-at-a-time loop, should be `memset` | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_shrink_to_fit` — char.c:202
|
#### `_vec_char_push_back` / `_vec_char_pop_back` — char.c:141, 202
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
*1 S2*
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>ST-1</nobr></code> | S2 | char.c:202, 217 -> 197| calls `reserve(ptr, _internal_size)`, and `reserve` sets `max_size = capacity`. So `max_size` is permanently capped to the current size and **the vector can never grow again** — every later `push_back` trips `size+1 > max_size` and returns `ERR_MAX_PASSED`. Silent brick. No save/restore, unlike `resize` (RZ-3) | OPEN |
|
|
||||||
| <code><nobr>ST-2</nobr></code> | S3 | char.c:204-211| returns `ERR_SIZE_ZERO` on an empty vector; shrinking an empty vector should be a successful no-op. Two unhoisted `is_valid` calls at 204 and 208 | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_swap` — char.c:221
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `PB-9` | S2 | char.c:161-173 vs 242-254 vs 839-853 | three blank-materialize paths, no shared shape. `push_back`: `malloc(1)`, `capacity=1`, `size=0`, writes `arr[0]`. `reserve`: `calloc(capacity)` and commits `capacity`. `insert` one-arg: `malloc(1)`, `capacity=1`, `size=1`, writes `arr[0]`. Same input state, three allocations, three struct results; `insert` and `resize` disagree with `push_back` on which blank states are accepted |
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
#### `_vec_char_cpy` — char.c:103 — *Clear*
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>SW-1</nobr></code> | S2 | char.c:229-233 | refuses NULL-buffer operands, so a blank vector can never be swapped against a populated one. Blocks the `create(nullptr, 0)` workflow outright | OPEN |
|
|
||||||
| <code><nobr>SW-2</nobr></code> | C | char.c:223 | temp built via `_vec_char_create(nullptr, 0)`; a plain initializer states the intent | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_delete` — char.c:289
|
#### `_vec_char_reserve` — char.c:221
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
*1 S1*
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>DL-1</nobr></code> | S1 | char.c:289, 293 | takes `const *` so it cannot reset the vector; a second call is a double free. Only function still taking `const` in a mutable API | OPEN |
|
|
||||||
| <code><nobr>DL-2</nobr></code> | S1 | char.h:11-17 | no ownership marker on the struct, so a by-value copy yields two owners of one buffer and `delete` cannot detect it | OPEN |
|
|
||||||
| <code><nobr>DL-3</nobr></code> | C | char.c:293 | unnecessary `(void*)` cast; `const` on the struct does not propagate to the pointee | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_capacity` / `_vec_char_max_size` / `_vec_char_data` — char.c:261, 275, 252
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `RS-1` | S1 | char.c:264-269 | `ptr->_internal_arr = res` (264) runs before the truncation, so the discarded tail is zeroed through the NEW pointer — but `realloc` already shrunk the block to `capacity` bytes, and `memset(&ptr->_internal_arr[capacity], 0, ptr->_internal_size - capacity)` (267-268) writes past it. **ASan Pass 12: `heap-buffer-overflow` WRITE at 267** — push 4, `reserve(&v, 2)`. The tail to zero no longer exists after a shrinking realloc: the `memset` must run on the old buffer before `realloc`, or be dropped — spare slots are uninitialized by contract |
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
#### `_vec_char_shrink_to_fit` — char.c:277 — *Clear*
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>AC-1</nobr></code> | S2 | char.c:254, 267, 281 | all three return `ERR_BUF_NULL` for a null vector, though none of them needs a buffer. `capacity()` should report capacity, not an error | OPEN |
|
|
||||||
| <code><nobr>DA-1</nobr></code> | S2 | char.c:251, 257| `char*` returned from a `const _VecCharInternal*`, laundering the const-correctness the mutable refactor was meant to establish | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_clear` / `_vec_char_print_vec` — char.c:298, 341
|
#### `_vec_char_swap` — char.c:295 — *Clear*
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
#### `_vec_char_delete` — char.c:364
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>CL-1</nobr></code> | C | char.c:310-313 | element-at-a-time zeroing loop, should be `memset` | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_at` — char.c:320
|
*1 S1*
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
| ID | Sev | Location | Finding |
|
||||||
|---|---|---|---|---|
|
|:---|:--:|:---------|:--------|
|
||||||
| <code><nobr>AT-1</nobr></code> | S2 | char.c:323, 328, 335| returns error codes (23/25, and 21) as a `char`; `assign` at char.c:642 pushes such a value into the vector as data | OPEN |
|
| `DL-2` | S1 | char.h:8-14 | no ownership marker on the struct, so a by-value copy yields two owners of one buffer; each `delete` clears only its own struct's pointer, so the second free hits the first's freed block. **ASan Pass 12: `attempting double-free` at char.c:376** — `w = v; delete(&v); delete(&w)` |
|
||||||
| <code><nobr>AT-2</nobr></code> | C | char.c:332 | raw `fprintf` bypasses `LOGERROR` | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_begin` / `_vec_char_end` — char.c:352, 359
|
#### `_vec_char_clear` / `_vec_char_print_vec` — char.c:383, 89 — *Clear*
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
<a id="validation--accessors"></a>
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>BE-1</nobr></code> | S2 | char.c:355, 365, 367, 630, 668 | `FAILURE` (`== 1`) returned as an index, indistinguishable from index 1. Single root cause of the `+ 1` at char.c:630 and char.c:668, of the assign-on-empty failures AS-1, and of the erase-on-empty failure ER-1 | OPEN |
|
|
||||||
| <code><nobr>BE-2</nobr></code> | C | char.c:364 | `(int)` cast on a `size_t` return | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_front` / `_vec_char_back` / `_vec_char_empty` — char.c:371, 379, 387
|
### Validation & accessors
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
#### `__null_check` / `__origin_override` — char.c:37, 69
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>AC-2</nobr></code> | S2 | char.c:392, 393 | inverted: `SUCCESS` (0) when empty, `FAILURE` (1) when not, so `if (empty(v))` is false for an empty vector | OPEN |
|
|
||||||
| <code><nobr>FB-1</nobr></code> | S2 | char.c:373, 374, 381, 382 | returns `FAILURE` (1) as a `char`; `1` is valid data | OPEN |
|
|
||||||
| <code><nobr>FB-2</nobr></code> | C | char.c:373, 381 | validates only `ERR_VECTOR_NULL`, unlike every other accessor | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_erase` (both overloads) — char.c:398, 412
|
*1 S1*
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
| ID | Sev | Location | Finding |
|
||||||
|---|---|---|---|---|
|
|:---|:--:|:---------|:--------|
|
||||||
| <code><nobr>ER-1</nobr></code> | S2 | char.c:417, 418 | `iter_start >= iter_end` returns `ERR_OUT_OF_BOUNDS`, so `erase(begin(), end())` on an empty vector always errors. This is what breaks both `assign` overloads. On an empty vector the actual trip is char.c:417 (`iter_end > size`) because `end()` returned 1 and the caller added 1 — see BE-1 | OPEN |
|
| `IV-5` | S1 | char.c:40-44 | the `status == nullptr` branch exists to catch a NULL `err_t*` but dereferences `status` on the next line (`status->flags = ...`, 42; `status->origin = ...`, 43). Seven call sites hand `__null_check` the caller's `err` verbatim (print_vec 92, pop_back 205, begin 425, end 438, front 451, back 469, empty 487), so any of those with a NULL `err` is a live null-deref. **ASan Pass 12: SEGV in `__null_check` at char.c:42.** Either return before the deref or drop the branch |
|
||||||
| <code><nobr>ER-2</nobr></code> | C | char.c:396, 410, 422-426 | zeroing loop should be `memset`; two stale `// TODO: Fix return value` comments | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_resize` (both overloads) — char.c:432, 518
|
#### `_vec_char_capacity` / `_vec_char_max_size` / `_vec_char_data` / `_vec_char_size` — char.c:338, 351, 312, 325
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
*1 C*
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>RZ-8</nobr></code> | S2 | char.c:434-444, 520-530 | both overloads reject a null-buffer vector with `ERR_BUF_NULL` before the `_internal_size == 0` branch that would have materialized one, so `resize` cannot be the first call on a blank vector. `push_back` can. **ASan: `create(NULL,0)` then `resize(0,4)` returns 21** and the vector is still empty, where `push_back` four times would have worked. Same shape as PB-17, opposite direction | OPEN |
|
|
||||||
| <code><nobr>RZ-3</nobr></code> | S2 | char.c:456, 471, 478, 497, 540, 557, 569, 579| the `max_size` save/restore is load-bearing (it undoes `reserve`'s cap) but looks removable. Nothing documents that | OPEN |
|
|
||||||
| <code><nobr>RZ-1</nobr></code> | S2 | char.c:442-446, 528-532| `size == _internal_size` returns `ERR_OUT_OF_BOUNDS` for a legitimate no-op; the code is also semantically wrong | OPEN |
|
|
||||||
| <code><nobr>RZ-4</nobr></code> | S3 | char.c:501-504 vs 583-586| 3-arg fills the discarded tail with `val`, 2-arg with `'\0'`. `data()` exposes the difference | OPEN |
|
|
||||||
| <code><nobr>RZ-2</nobr></code> | S3 | char.c:481, 567 | unguarded `tmpcp *= 2` wraps on overflow. Defensive | OPEN |
|
|
||||||
| <code><nobr>RZ-5</nobr></code> | C | char.c:462, 492, 548 | dead post-`reserve` capacity checks; `reserve` always assigns `capacity` to its argument exactly | OPEN |
|
|
||||||
| <code><nobr>RZ-6</nobr></code> | C | char.c:432-515 vs 518-597 | two ~84-line near-identical bodies, with the growth blocks copy-pasted between them | OPEN |
|
|
||||||
| <code><nobr>RZ-7</nobr></code> | C | char.c:444, 467, 501, 508, 530, 553, 583, 590| fill loops should be `memset`; misleading `LOG("ERR :- ...")` on what is now a no-op | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_assign` (both overloads) — char.c:600, 652
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `AC-3` | C | char.c:326-335 | new `size` accessor records its failure origin as `"CAPACITY"` (331), copied from `_vec_char_capacity`. It also takes a non-`const` `_VecCharInternal*` (326) while only reading, unlike its `const` siblings |
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
#### `_vec_char_at` — char.c:400
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>AS-1</nobr></code> | S2 | char.c:630, 668 | fails on an empty destination, via ER-1. Fatal now because status is propagated; the immutable version discarded it and continued | OPEN |
|
|
||||||
| <code><nobr>AS-2</nobr></code> | S2 | char.c:624-647, 661-687| not atomic: `erase` → `reserve` → `push_back` loop. Reserve failure leaves the destination wiped; a `push_back` failure mid-loop leaves it partially filled | OPEN |
|
|
||||||
| <code><nobr>AS-3</nobr></code> | S2 | char.c:624, 662, 679| no `> _internal_max_size` guard on the assign count | OPEN |
|
|
||||||
| <code><nobr>AS-4</nobr></code> | S2 | char.c:635, 673 | the internal `reserve` caps `max_size` at the assign count with no save/restore, which `resize` does have | OPEN |
|
|
||||||
| <code><nobr>AS-6</nobr></code> | C | char.c:600-649 vs 652-688 | near-duplicate overloads differing only in the value source | OPEN |
|
|
||||||
| <code><nobr>AS-7</nobr></code> | S3 | char.c:642 | no check that `at()` succeeded before pushing its result. Currently correct **only** because the guards at char.c:611-618 and char.c:624 happen to make every index in range and the buffer non-null. Removing or loosening either guard turns this into AT-1's data-corruption case | OPEN |
|
|
||||||
|
|
||||||
### `_vec_char_insert` (all three overloads) — char.c:691, 726, 759
|
*1 S3 · 1 C*
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
| ID | Sev | Location | Finding |
|
||||||
|---|---|---|---|---|
|
|:---|:--:|:---------|:--------|
|
||||||
| <code><nobr>IN-1</nobr></code> | S1 | char.c:779 | inverted check: `iter_end > iter_start` should be `iter_end <= iter_start`, so every valid non-empty range is rejected and only an empty range is accepted | OPEN |
|
| `AT-2` | S3 | char.c:410-417 | the OOB path sets `err` (flags + origin AT) and returns `nullptr`, but still prints a raw line to **stdout** via `printf` (412-413), polluting program output and bypassing the error channel. The residual hazard moved to the callers: `front`/`back` dereference the `nullptr` (462, 480) → SEGV (FB-1) |
|
||||||
| <code><nobr>IN-2</nobr></code> | S1 | char.c:779 | that clause short-circuits first, so the `iter_end > iterator->_internal_size` bound never fires | OPEN |
|
| `AT-3` | C | char.c:400, 418 | `at()` returns a mutable `char*` into the buffer of a `const _VecCharInternal*` — const-laundering callers can write elements behind the API's back, bypassing `push_back`/`resize` bookkeeping; the pointer also invalidates on any realloc |
|
||||||
| <code><nobr>IN-4</nobr></code> | S2 | char.c:742, 747-754 | no `max_size` pre-check, so an oversized `amount` partially inserts and then fails, leaving the vector half-modified | OPEN |
|
|
||||||
| <code><nobr>IN-6</nobr></code> | S3 | char.c:747 | `iter + amount` can overflow `size_t` | OPEN |
|
|
||||||
| <code><nobr>IN-5</nobr></code> | S3 | char.c:742 | `amount <= 0` on a `size_t` is always just `== 0` | OPEN |
|
|
||||||
| <code><nobr>IN-8</nobr></code> | S2 | char.c:747-754, 793 | the loop copies via the raw `iterator->_internal_arr[i]` rather than a bounds-checked accessor. It is **unreachable today** only because IN-1's inversion makes `iter_end <= iter_start` always true, so the loop never runs. It becomes a live out-of-bounds read the moment IN-1 is fixed, unless IN-2 is fixed in the same change | OPEN |
|
|
||||||
| <code><nobr>IN-9</nobr></code> | C | char.c:747-754 | O(amount²) loop of single inserts where one grow + `memmove` would do | OPEN |
|
|
||||||
|
|
||||||
### `utils.h` / `utils.c`
|
#### `_vec_char_begin` / `_vec_char_end` — char.c:421, 434
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
*1 S2*
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>UT-2</nobr></code> | S1 | utils.c:3-12 | indices 0 and 1 are never assigned, so `error_messages[0]` and `[1]` are `NULL`. `LOGERROR(SUCCESS)` prints `%s` of `NULL`, and both are values functions return. Latent only because no caller exists yet | OPEN |
|
|
||||||
| <code><nobr>UT-1</nobr></code> | S2 | utils.h:29 | `SIZE_MAX / sizeof(int)` is what the `char` vector uses, capping it at 1/4 of the usable address space | OPEN |
|
|
||||||
| <code><nobr>UT-3</nobr></code> | S2 | utils.c:3 | `SUCCESS`/`FAILURE` overlap the error-index namespace, so `0` is both "success" and a table slot | OPEN |
|
|
||||||
| <code><nobr>UT-5</nobr></code> | C | utils.h:23 | `DEBUG` prints the literal `DEBUB: `, so char.c:109 — the only `DEBUG` call in the tree, on the clamp path in `push_back` — emits a misspelled prefix | OPEN |
|
|
||||||
| <code><nobr>UT-4</nobr></code> | C | utils.h:7, 17, 20, 22-25, 27, 29 · utils.c:6, 10 | `static const` in a header gives every TU its own copy; `ERR_NULL` is now dead (defined and given a message, never referenced from `char.c`); `ERR_SIZE_ZERO` message mentions `< 0` on a `size_t`; `extern const char*` should be `const char* const[]`; `LOG`/`LOGERROR` are hardwired to stdout and single-arg; `find_len` is UB for pointers; `CAPACITY` is a magic number in a shared header | OPEN |
|
|
||||||
|
|
||||||
### `char.h`
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `BE-1` | S2 | char.c:421-432, 434-445, 749-750, 799-800 | `begin` returns raw `status.flags` (bitmask 2/4) as an index on failure (429), and `end`'s `(size_t)(size - 1)` (444) is **reachable for a drained vector** — `size == 0` wraps to `SIZE_MAX` (harness Pass 12: end(drained) = 18446744073709551615, flags=0). Flags-as-index and SIZE_MAX-as-end are indistinguishable from real positions; `SIZE_MAX + 1` wraps to 0 at 749-750, 799-800, so `assign` into a drained destination erases `[0, 0)` and fails with `ERR_OUT_OF_BOUNDS` (AS-1) |
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
#### `_vec_char_front` / `_vec_char_back` / `_vec_char_empty` — char.c:447, 465, 483
|
||||||
|---|---|---|---|---|
|
|
||||||
| <code><nobr>CH-9</nobr></code> | S2 | char.h:11-106 (29 identifiers)| every public identifier is reserved by the C standard: `_VecCharInternal` and all 28 `_vec_char_*` functions begin with `_` at file scope, which C11 7.1.3 reserves for the implementation. Undefined behaviour on collision with any libc or third-party name. `clang -Weverything` reports all 28 | OPEN |
|
|
||||||
| <code><nobr>CH-4</nobr></code> | C | char.h:6-8 | `<stdio.h>`/`<stdlib.h>`/`<string.h>` in the header, only needed by the `.c` | OPEN |
|
|
||||||
| <code><nobr>CH-5</nobr></code> | C | char.h | no doc comments on any function, despite the `/**< */` style in utils.h | OPEN |
|
|
||||||
| <code><nobr>CH-7</nobr></code> | C | char.h:22-23 | no `from_cstr` / NUL-termination path for the `char` specialization. See CR-4 | OPEN |
|
|
||||||
| <code><nobr>CH-8</nobr></code> | C | char.h:11-17 | the public struct is named with a `_` prefix and its four fields are named `_internal_*` instead of being `PRIVATE`, so the "internal" contract is expressed only by spelling. Combined with CH-9 this is the whole naming problem | OPEN |
|
|
||||||
| <code><nobr>CH-1</nobr></code> | C | char.h:32 vs char.c:156 | param named `max_size` in the header, `capacity` in the definition | OPEN |
|
|
||||||
| <code><nobr>CH-2</nobr></code> | C | char.h:23-106 | `const` on by-value params throughout (`const size_t size`, `const char val`, `const size_t idx`, …) | OPEN |
|
|
||||||
| <code><nobr>CH-3</nobr></code> | C | char.h | no `size()` accessor, so callers reach into `_internal_size` directly | OPEN |
|
|
||||||
|
|
||||||
### Project
|
*1 S1 · 1 S2*
|
||||||
|
|
||||||
| ID | Sev | Location | Finding | State |
|
| ID | Sev | Location | Finding |
|
||||||
|---|---|---|---|---|
|
|:---|:--:|:---------|:--------|
|
||||||
| <code><nobr>PR-2</nobr></code> | C | char.c:1-3 | mixed brace style (single-line `if` in `erase`/`front`/`back`/`begin`/`empty` vs expanded blocks); `.clang-format` exists and is not run | OPEN |
|
| `FB-1` | S1 | char.c:457-462, 475-480 | the guard meant to kill the drained SEGV is **inverted and probes the wrong index**. `at()` returns `nullptr` on failure, so `if (_vec_char_at(...) != nullptr)` fires for *valid* vectors: **`front`/`back` on a populated vector return 0 with `ERR_OUT_OF_BOUNDS`** (harness Pass 12: front('ab')=0/flags=16, back=0/flags=16 — expected 97/98). For a drained vector `at` returns nullptr, the condition is false, control falls through to the deref — `front` → `*_vec_char_at(ptr, 0, err)` (462), `back` → `*_vec_char_at(ptr, size - 1, err)` (480) — **SEGV** (ASan Pass 12: SEGV at 462 and 480). `back` also probes index 0 (475) instead of `size - 1`. Condition must be `== nullptr`, and `back` must probe the last index |
|
||||||
| <code><nobr>PR-3</nobr></code> | C | char.c:204, 208, 224, 225, 229, 230, 254, 255, 263, 267, 277, 281, 291, 300, 304, 322, 326, 343, 344, 354, 361, 373, 381, 389, 390, 400, 401, 415, 416, 434, 438, 520, 524, 603, 607, 611, 615, 654, 658, 693, 697, 763, 767, 771, 775| `is_valid` called 2-3× per function and logging each time, in every function except `push_back`, `pop_back` and `reserve` — those three hoist | OPEN |
|
| `AC-2` | S2 | char.c:483-498 | `empty()` still returns 0 in *both* directions: a drained vector takes the `size == 0` branch and `return SUCCESS` (493-496), a populated one falls through to `return status.flags`, also 0 (497) — so `if (empty(&v))` is never true for a valid vector. **Harness Pass 12: drained → 0/err 0, populated → 0/err 0** |
|
||||||
| <code><nobr>PR-7</nobr></code> | C | CMakeLists.txt:28, main.c:20-54 | the `test_runner` target exists but `main()` is entirely commented out, so nothing in the tree calls a single function in this tracker. This is the only place a regression in the 10 open S1s could be caught, and it is dead | OPEN |
|
|
||||||
| <code><nobr>PR-4</nobr></code> | C | main.c:23-53 | 100% commented out, references removed API `Vec8_t` and `v1.size` | OPEN |
|
<a id="algorithms"></a>
|
||||||
| <code><nobr>PR-5</nobr></code> | C | include/libvec.h:4, CMakeLists.txt:12-14 | empty placeholder; `int`/`float`/`string` are empty dirs already listed as comments in CMakeLists.txt | OPEN |
|
|
||||||
|
### Algorithms
|
||||||
|
|
||||||
|
#### `_vec_char_erase` (both overloads) — char.c:501, 520
|
||||||
|
|
||||||
|
*1 S1 · 1 S2 · 1 S3 · 1 C*
|
||||||
|
|
||||||
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `ER-4` | S1 | char.c:541-542 | the trailing-zero `memset` starts at `_internal_size - diff` but its length is `sizeof(char) * ptr->_internal_size` (the *whole* old size, though `_internal_size` is not decremented until 543), so it writes `[size - diff, 2*size - diff)` — past the block whenever more than `diff` elements remain. Should be `diff` bytes. **ASan Pass 12: `heap-buffer-overflow` WRITE at 541** — 10 elements in a 13-byte block, `erase(&v, 0, 1)` |
|
||||||
|
| `ER-1` | S2 | char.c:532-537 | `erase` on an empty vector never no-ops: a drained vector passes the `__null_check` guard now, then the range check (`size < iter_end \|\| iter_end <= iter_start`) fires — `[0, 1)` because `0 < 1`, and even `[0, 0)` because `0 <= 0`. **Harness Pass 12: erase(v, 0, 1) on drained → err=16, origin ERASE** |
|
||||||
|
| `ER-3` | S3 | char.c:511 | the single-arg overload silently returns on `iter >= size` with `err` untouched (harness Pass 12: erase index 99 → flags=0), while the range overload reports `ERR_OUT_OF_BOUNDS` for the same condition. A typo'd index is invisible to an err-driven caller |
|
||||||
|
| `ER-2` | C | char.c:500, 519 | two stale `// TODO: Fix return value` comments remain (the second doubled as `// // TODO`) |
|
||||||
|
|
||||||
|
#### `_vec_char_resize` (both overloads) — char.c:547, 639
|
||||||
|
|
||||||
|
*2 S1 · 2 S2 · 2 S3 · 3 C*
|
||||||
|
|
||||||
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `RZ-10` | S1 | char.c:626-629, 712-715 | shrink zeroing `memset(&arr[size], 0, ptr->_internal_size)` uses the *whole old* `_internal_size` as the length instead of `old - new`, so it writes past the block whenever `0 < size < old`. Should be `_internal_size - size` bytes. **ASan Pass 12: `heap-buffer-overflow` WRITE at 628** — 5 elements in a 6-byte block, `resize(&v, 2)` (also 714 for the 3-arg overload) |
|
||||||
|
| `RZ-11` | S1 | char.c:630-634, 716-720 | growth fill `memset(&arr[_internal_size], 0, ptr->_internal_arr[size])` passes `_internal_arr[size]` — an *element value* (or 0xbe uninitialized) — as the byte count instead of `size - _internal_size`, and when `size == _internal_capacity` that read is one past the block. **ASan Pass 12: `heap-buffer-overflow` READ at 633** — 5 elements in a 6-byte block, `resize(&v, 6)` (also 719 for the 3-arg overload) |
|
||||||
|
| `RZ-3` | S2 | char.c:573-596, 665-688 | the `max_size` save/restore (575/594, 667/686) is pure belt-and-braces now that `reserve` leaves `max_size` alone, and looks removable. Nothing documents that |
|
||||||
|
| `RZ-1` | S2 | char.c:559-564, 651-656 | `size == _internal_size` returns `ERR_OUT_OF_BOUNDS` for a legitimate no-op; the code is also semantically wrong |
|
||||||
|
| `RZ-4` | S3 | char.c:594-596 vs 686-688 | the 3-arg blank branch fills with `val`, the 2-arg with `0`. `data()` exposes the difference |
|
||||||
|
| `RZ-2` | S3 | char.c:605, 695 | unguarded `tmpcp *= 2` wraps on overflow. Defensive |
|
||||||
|
| `RZ-5` | C | char.c:584-589, 607-611, 618-623, 676-681, 699-703 | dead post-`reserve` capacity checks in the blank branches (584, 676) and the 2-arg growth branch (618); the 3-arg growth branch has no check at all, relying on `reserve` to be exact |
|
||||||
|
| `RZ-6` | C | char.c:547-637 vs 639-723 | two ~90-line near-identical bodies, with the growth blocks copy-pasted between them |
|
||||||
|
| `RZ-7` | C | char.c:590-593, 682-685 | the blank-branch fill loops should be `memset` |
|
||||||
|
|
||||||
|
#### `_vec_char_assign` (both overloads) — char.c:725, 783
|
||||||
|
|
||||||
|
*4 S2 · 1 S3 · 1 C*
|
||||||
|
|
||||||
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `AS-1` | S2 | char.c:730-735, 749-750, 799-800 | fails on any empty destination. A **blank** destination trips the `__null_check` guard (730-735) → err=4, origin ASSIGN. A **drained** one passes the guard, then `end()` returns `SIZE_MAX` (BE-1) and `end + 1` wraps to 0 (749, 799), so `erase(0, 0)` fails the range check → err=16. Two paths, same refusal |
|
||||||
|
| `AS-2` | S2 | char.c:749-778, 799-823 | not atomic: `erase` → `reserve` → `push_back` loop. Reserve failure leaves the destination wiped; a `push_back` failure mid-loop leaves it partially filled. Still no rollback |
|
||||||
|
| `AS-3` | S2 | char.c:757, 807 | no `> _internal_max_size` guard on the assign count |
|
||||||
|
| `AS-7` | S2 | char.c:764-770 | the `at()` guard in assign1 is **inverted**: `at()` returns `nullptr` on failure, so the `!= nullptr` branch fires for *valid* iterators — assign1 fails with `ERR_OUT_OF_BOUNDS` on the first element of **every non-empty source**, after erase already wiped the destination (harness Pass 12: dest `{x,y}` ← src `{a,b}` → flags=16, size=0). Condition must be `== nullptr` |
|
||||||
|
| `AS-9` | S3 | char.c:788-792 | the null guard in the amount overload has **no `return`**, unlike assign1 — control falls through with `err` already flagged. Masked today only because the `erase` at 799 rejects the same state downstream |
|
||||||
|
| `AS-6` | C | char.c:725-781 vs 783-825 | near-duplicate overloads differing only in the value source |
|
||||||
|
|
||||||
|
#### `_vec_char_insert` (all three overloads) — char.c:827, 882, 918
|
||||||
|
|
||||||
|
*1 S1 · 2 S2 · 2 S3 · 2 C*
|
||||||
|
|
||||||
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `IN-10` | S1 | char.c:886-893 | the amount overload never calls `__null_check`: the guard at 887-892 tests a freshly-initialized local `status`, which is always 0, so it can never fire; `ptr` is then dereferenced raw at 893. Every other entry point validates first. **ASan Pass 12: SEGV at char.c:893** — `_vec_char_insert(nullptr, 0, 2, 'x', &e)` |
|
||||||
|
| `IN-1` | S2 | char.c:932-938 | inverted range check: `iter_end > iter_start` rejects every non-empty range with `ERR_OUT_OF_BOUNDS` and accepts only empty ranges, which then silently do nothing (loop at 945 never runs). Valid insert-range never inserts. **Harness Pass 12: dest `{a,b}`, src `{Z}`, insert at 1 range [0,1) → err=16, size stays 2** |
|
||||||
|
| `IN-4` | S2 | char.c:854-859, 884-893 | no `max_size` pre-check in the amount overload: the loop pushes until one-arg insert trips `size+1 > max_size` (854) and then returns, but everything already inserted stays — an oversized `amount` half-inserts and leaves the vector half-modified |
|
||||||
|
| `IN-5` | S3 | char.c:899-904 | `amount <= 0` on a `size_t` is always just `== 0` |
|
||||||
|
| `IN-6` | S3 | char.c:905 | `iter + amount` can overflow `size_t`, wrapping the loop bound and exiting early — a silent partial insert |
|
||||||
|
| `IN-8` | C | char.c:945-955 | the loop reads `iterator->_internal_arr[i]` raw (948) instead of a bounds-checked accessor. **Unreachable today**: IN-1's inversion rejects every non-empty range, so the body never runs. The `iter_end > iterator->_internal_size` clause at 932 does bound the read once IN-1 is fixed, so this stays a style finding |
|
||||||
|
| `IN-9` | C | char.c:905-914 | O(amount²) loop of single inserts where one grow + `memmove` would do |
|
||||||
|
|
||||||
|
<a id="support"></a>
|
||||||
|
|
||||||
|
### Support
|
||||||
|
|
||||||
|
#### `utils.h` / `utils.c`
|
||||||
|
|
||||||
|
*3 S2 · 1 C*
|
||||||
|
|
||||||
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `UT-1` | S2 | utils.h:9 | `SIZE_MAX / sizeof(int)` is what the `char` vector uses as `_internal_max_size`, capping it at 1/4 of the usable address space |
|
||||||
|
| `UT-6` | S2 | utils.c:14-16 | `vec_get_error_desc`'s default case is an expression statement — `"Unknown";` with no `return` — so an out-of-range code falls off the end of a non-void function: UB, and the caller printf's a garbage `char*`. Current call sites only pass constants, so it is one cast away |
|
||||||
|
| `UT-7` | S2 | utils.c:22-35 | `vec_print_errs` prints `err.origin` only on the SUCCESS branch (24), where it is always NULL on an untouched `err_t` — `%s` of NULL, UB — and the failure branch (27-35) never prints origin at all. The root-cause feature surfaces exactly backwards |
|
||||||
|
| `UT-4` | C | utils.h:7-9, 63-65 | `static const` in a header gives every TU its own copy; `ERR_SIZE_ZERO` message mentions `< 0` on a `size_t`; `LOG`/`DEBUG` are hardwired to stdout and single-arg; `find_len` is UB for pointers; `CAPACITY` (7) and `VEC_FAULT_BITS` (8) are now referenced by nothing in the tree |
|
||||||
|
|
||||||
|
#### `char.h` — *Clear*
|
||||||
|
|
||||||
|
#### Project
|
||||||
|
|
||||||
|
*2 C*
|
||||||
|
|
||||||
|
| ID | Sev | Location | Finding |
|
||||||
|
|:---|:--:|:---------|:--------|
|
||||||
|
| `PR-8` | C | char.c:72, 112 (and every guard) | no function checks `err` for NULL before the first error path derefs it; a NULL `err_t*` segfaults on the first failure. Now documented (char.c:27-30) — a cheap defensive check would still harden the API. IV-5 is the same hazard at the one site that *tries* to check |
|
||||||
|
| `PR-2` | C | char.c:179, 500, 519 | mixed brace style (single-line `if` at 179 vs expanded blocks) and stale `TODO` comments at 500/519; `.clang-format` exists and is not run |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
<a id="fixed"></a>
|
||||||
|
|
||||||
|
## Fixed
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>63 fixed IDs — no rows</b></summary>
|
||||||
|
|
||||||
|
```
|
||||||
|
PB-1 PB-2 PB-3 PB-4 PB-6 PB-7 PB-8 PB-14 PB-15 PB-16 PB-17 PB-18 PB-20 PB-21
|
||||||
|
RS-2 RS-3 RS-4 RS-6 RS-7 RS-8
|
||||||
|
IV-1 IV-3 IV-4
|
||||||
|
CH-1 CH-3 CH-7
|
||||||
|
CR-3 CR-4 CR-6 CR-7 CR-8 CR-10 CR-11
|
||||||
|
SW-1 SW-2
|
||||||
|
FB-2
|
||||||
|
AC-1 DA-1
|
||||||
|
AT-1
|
||||||
|
DL-1 DL-3
|
||||||
|
UT-2 UT-3 UT-5
|
||||||
|
PR-1 PR-3 PR-4
|
||||||
|
CL-1 CL-2
|
||||||
|
RZ-8 RZ-9
|
||||||
|
AS-4 AS-8
|
||||||
|
ST-1 ST-2
|
||||||
|
IN-2 IN-11 IN-12
|
||||||
|
FX-2 FX-3 FX-4 FX-5
|
||||||
|
BE-2
|
||||||
|
```
|
||||||
|
|
||||||
|
Closed by owner decision (not counted fixed): `CH-9` (leading `_` intended private), `PR-7` (main not the test harness), `CH-4` (header includes), `CH-5` (doc comments), `CH-8` (struct naming), `PR-5` (placeholder comments), `CH-2` (const on by-value params for LSP/clarity).
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
<a id="id-map"></a>
|
||||||
|
|
||||||
## ID map
|
## ID map
|
||||||
|
|
||||||
`FX-1` … `FX-5` are first-pass IDs. FX-1 was a duplicate of PB-6 and was never
|
| Prefix | Scope | Prefix | Scope |
|
||||||
given a row.
|
|:------:|:------|:------:|:------|
|
||||||
|
| `PB-` | push_back, pop_back | `AT-` | at |
|
||||||
| Prefix | Scope | | Prefix | Scope |
|
| `RS-` | reserve | `FB-` | front, back |
|
||||||
|---|---|---|---|---|
|
| `ST-` | shrink_to_fit | `BE-` | begin, end |
|
||||||
| `PB-` | push_back, pop_back | | `AT-` | at |
|
| `CR-` | create → cpy | `SW-` | swap |
|
||||||
| `RS-` | reserve | | `FB-` | front, back |
|
| `AC-` | accessors | `CL-` | clear, print_vec |
|
||||||
| `ST-` | shrink_to_fit | | `BE-` | begin, end |
|
| `DA-` | data | `IV-` | is_valid → __null_check |
|
||||||
| `CR-` | create | | `SW-` | swap |
|
| `DL-` | delete | `UT-` | utils.h, utils.c |
|
||||||
| `AC-` | accessors | | `CL-` | clear, print_vec |
|
| `ER-` | erase | `CH-` | char.h |
|
||||||
| `DA-` | data | | `IV-` | is_valid |
|
| `RZ-` | resize | `PR-` | project |
|
||||||
| `DL-` | delete | | `UT-` | utils.h, utils.c |
|
| `AS-` | assign | `FX-` | retired first-pass IDs |
|
||||||
| `ER-` | erase | | `CH-` | char.h |
|
| `IN-` | insert | | |
|
||||||
| `RZ-` | resize | | `PR-` | project |
|
|
||||||
| `AS-` | assign | | `FX-` | retired first-pass IDs |
|
|
||||||
| `IN-` | insert | | | |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
`PB-1` `PB-2` `PB-4` `PB-6` `PB-7` `FX-2` `FX-3` `FX-4` `FX-5` `PR-1` `CR-7` `CR-8`
|
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
# ==============================================================================
|
||||||
|
# Toolchain & Executables
|
||||||
|
# ==============================================================================
|
||||||
|
LLVM_DIR ?= /opt/homebrew/opt/llvm/bin
|
||||||
|
CC := $(LLVM_DIR)/clang
|
||||||
|
LLVM_PROFDATA := $(LLVM_DIR)/llvm-profdata
|
||||||
|
LLVM_COV := $(LLVM_DIR)/llvm-cov
|
||||||
|
CPPCHECK := cppcheck
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# Flags & Directories
|
||||||
|
# ==============================================================================
|
||||||
|
CFLAGS := -std=c23 -Wall -Wextra -Wpedantic -Wshadow \
|
||||||
|
-Wconversion -Wsign-conversion \
|
||||||
|
-g -O1 -fno-omit-frame-pointer
|
||||||
|
SAN_FLAGS := -fsanitize=address,undefined
|
||||||
|
INCLUDES := -Iinclude -Isrc/lib -Isrc/lib/char -Isrc/lib/utils
|
||||||
|
|
||||||
|
ASAN_ENV := ASAN_OPTIONS="allocator_may_return_null=1:detect_stack_use_after_return=1" \
|
||||||
|
UBSAN_OPTIONS="halt_on_error=1:print_stacktrace=1"
|
||||||
|
|
||||||
|
CPPCHECK_FLAGS := --enable=all \
|
||||||
|
--inconclusive \
|
||||||
|
--error-exitcode=1 \
|
||||||
|
--check-level=exhaustive \
|
||||||
|
--suppress=missingIncludeSystem \
|
||||||
|
--suppress=unmatchedSuppression \
|
||||||
|
--suppress=unusedFunction \
|
||||||
|
--suppress=staticFunction \
|
||||||
|
--inline-suppr \
|
||||||
|
$(INCLUDES)
|
||||||
|
|
||||||
|
# Sources & Build Directories
|
||||||
|
SRCS := src/lib/char/char.c src/lib/utils/utils.c
|
||||||
|
TEST_SRC := tests/test_learn.c
|
||||||
|
FUZZ_SRC := tests/fuzz_char.c
|
||||||
|
|
||||||
|
BIN_DIR := tests/bin
|
||||||
|
CACHE_DIR := tests/cache
|
||||||
|
|
||||||
|
TEST_BIN := $(BIN_DIR)/test_learn_runner
|
||||||
|
TSAN_BIN := $(BIN_DIR)/test_tsan_runner
|
||||||
|
FUZZ_BIN := $(BIN_DIR)/fuzz_char_runner
|
||||||
|
COV_BIN := $(BIN_DIR)/test_cov_runner
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# Targets & Workflow Rules
|
||||||
|
# ==============================================================================
|
||||||
|
.PHONY: all test sanitize check check-sources analyze cppcheck fuzz cov-build cov clean
|
||||||
|
|
||||||
|
all: test
|
||||||
|
|
||||||
|
$(BIN_DIR) $(CACHE_DIR):
|
||||||
|
@mkdir -p $@
|
||||||
|
|
||||||
|
# --- Compilation Targets ---
|
||||||
|
|
||||||
|
$(TEST_BIN): $(SRCS) $(TEST_SRC) | $(BIN_DIR)
|
||||||
|
$(CC) $(CFLAGS) $(SAN_FLAGS) $(INCLUDES) $^ -o $@
|
||||||
|
|
||||||
|
$(TSAN_BIN): $(SRCS) $(TEST_SRC) | $(BIN_DIR)
|
||||||
|
$(CC) $(CFLAGS) -fsanitize=thread $(INCLUDES) $^ -o $@
|
||||||
|
|
||||||
|
$(FUZZ_BIN): $(SRCS) $(FUZZ_SRC) | $(BIN_DIR)
|
||||||
|
$(CC) $(CFLAGS) -fsanitize=fuzzer,address,undefined $(INCLUDES) $^ -o $@
|
||||||
|
|
||||||
|
# --- Testing & Sanitizer Suites ---
|
||||||
|
|
||||||
|
test: $(TEST_BIN)
|
||||||
|
@echo "=== Running Unit Tests (ASan + UBSan) ==="
|
||||||
|
@$(ASAN_ENV) ./$(TEST_BIN)
|
||||||
|
|
||||||
|
sanitize: $(TEST_BIN) $(TSAN_BIN)
|
||||||
|
@echo "\n=== [1/2] Running Address + UndefinedBehavior Sanitizers ==="
|
||||||
|
@$(ASAN_ENV) ./$(TEST_BIN)
|
||||||
|
@echo "\n=== [2/2] Running ThreadSanitizer ==="
|
||||||
|
@./$(TSAN_BIN)
|
||||||
|
@echo "\n>>> Sanitizers passed with zero defects."
|
||||||
|
|
||||||
|
# Unified master check: runtime sanitizers, static analyzer, and cppcheck
|
||||||
|
check: sanitize analyze cppcheck
|
||||||
|
@echo "\n=============================================="
|
||||||
|
@echo " ALL TESTS, SANITIZERS & STATIC CHECKS PASSED "
|
||||||
|
@echo "=============================================="
|
||||||
|
|
||||||
|
fuzz: $(FUZZ_BIN)
|
||||||
|
@echo "=== Starting Fuzzer (Press Ctrl+C to stop) ==="
|
||||||
|
@$(ASAN_ENV) ./$(FUZZ_BIN) -max_total_time=60
|
||||||
|
|
||||||
|
# --- Coverage Profiling ---
|
||||||
|
|
||||||
|
cov-build: $(SRCS) $(TEST_SRC) | $(BIN_DIR)
|
||||||
|
$(CC) $(CFLAGS) $(SAN_FLAGS) -fprofile-instr-generate -fcoverage-mapping $(INCLUDES) $^ -o $(COV_BIN)
|
||||||
|
|
||||||
|
cov: cov-build | $(CACHE_DIR)
|
||||||
|
@echo "=== Recording Coverage Profile Data ==="
|
||||||
|
@$(ASAN_ENV) LLVM_PROFILE_FILE="$(CACHE_DIR)/test_char.profdata_raw" ./$(COV_BIN)
|
||||||
|
@$(LLVM_PROFDATA) merge -sparse $(CACHE_DIR)/test_char.profdata_raw -o $(CACHE_DIR)/test_char.profdata
|
||||||
|
@echo "\n=== Line & Branch Coverage Report: src/lib/char/char.c ==="
|
||||||
|
@$(LLVM_COV) report $(COV_BIN) -instr-profile=$(CACHE_DIR)/test_char.profdata src/lib/char/char.c
|
||||||
|
@$(LLVM_COV) show $(COV_BIN) -instr-profile=$(CACHE_DIR)/test_char.profdata src/lib/char/char.c --format=html > coverage.html
|
||||||
|
@echo "\nDetailed HTML report written to coverage.html."
|
||||||
|
|
||||||
|
# --- Static Analysis & Object Checking ---
|
||||||
|
|
||||||
|
check-sources: | $(BIN_DIR)
|
||||||
|
@echo "=== Compiling Sources to Object Files (ASan + UBSan) ==="
|
||||||
|
$(CC) $(CFLAGS) $(SAN_FLAGS) $(INCLUDES) -c src/lib/char/char.c -o $(BIN_DIR)/char.o
|
||||||
|
$(CC) $(CFLAGS) $(SAN_FLAGS) $(INCLUDES) -c src/lib/utils/utils.c -o $(BIN_DIR)/utils.o
|
||||||
|
@echo "Library sources compiled cleanly."
|
||||||
|
|
||||||
|
analyze:
|
||||||
|
@echo "=== Running Clang Static Analyzer ==="
|
||||||
|
@$(CC) $(CFLAGS) $(INCLUDES) \
|
||||||
|
--analyze \
|
||||||
|
-Xanalyzer -analyzer-output=text \
|
||||||
|
-Xanalyzer -analyzer-checker=core,unix,deadcode,security \
|
||||||
|
$(SRCS)
|
||||||
|
@echo "Clang Static Analyzer complete: zero defects."
|
||||||
|
|
||||||
|
cppcheck:
|
||||||
|
@echo "=== Running Cppcheck Static Analysis ==="
|
||||||
|
@$(CPPCHECK) $(CPPCHECK_FLAGS) include/ src/
|
||||||
|
@echo "Cppcheck passed with zero defects."
|
||||||
|
|
||||||
|
# --- Housekeeping ---
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BIN_DIR)/* $(CACHE_DIR)/* coverage.html
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
```mermaid
|
|
||||||
flowchart TD
|
|
||||||
Start([Function Called]) --> CheckErr{Did an error occur?}
|
|
||||||
|
|
||||||
%% Error path
|
|
||||||
CheckErr -->|Yes| PushBit["Bitwise OR error code into err<br/>err |= ERROR_BIT"]
|
|
||||||
PushBit --> CheckOrigin{Is origin == NULL?}
|
|
||||||
CheckOrigin -->|Yes| SetOrigin["Record location in origin<br/>origin = __func__ / __FILE__"]
|
|
||||||
CheckOrigin -->|No| KeepOrigin[Preserve original root cause]
|
|
||||||
|
|
||||||
SetOrigin --> ReturnStruct
|
|
||||||
KeepOrigin --> ReturnStruct
|
|
||||||
|
|
||||||
%% Success / Normal path
|
|
||||||
CheckErr -->|No| ComputeValue["Write computation result<br/>value = result"]
|
|
||||||
ComputeValue --> ReturnStruct
|
|
||||||
|
|
||||||
ReturnStruct(["Return / Pass Out _err_t"]) --> Caller{Caller Inspection}
|
|
||||||
|
|
||||||
Caller -->|Check errors| InspectBits[Inspect bit flags in err]
|
|
||||||
Caller -->|Read payload| ReadValue[Access value data]
|
|
||||||
Caller -->|Debug / Log| PrintOrigin[Print root cause origin]
|
|
||||||
```
|
|
||||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
+196
-132
@@ -1,33 +1,53 @@
|
|||||||
#include "char.h"
|
#include "char.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
|
||||||
__attribute__((overloadable)) size_t
|
__attribute__((overloadable)) size_t
|
||||||
__null_check(const _VecCharInternal* ptr, err_t* status)
|
__null_check(const _VecCharInternal* ptr, err_t* status)
|
||||||
{
|
{
|
||||||
unsigned int res = 0;
|
if (status == nullptr)
|
||||||
|
{
|
||||||
|
return ERR_ERR_T_NULL;
|
||||||
|
}
|
||||||
if (ptr == nullptr)
|
if (ptr == nullptr)
|
||||||
{
|
{
|
||||||
status->flags |= ERR_VEC_NULL;
|
status->flags |= ERR_VEC_NULL;
|
||||||
return ERR_VEC_NULL;
|
return ERR_VEC_NULL;
|
||||||
}
|
}
|
||||||
if (ptr->_internal_arr == nullptr) status->flags |= ERR_BUF_NULL;
|
if (ptr->_internal_arr == nullptr)
|
||||||
if (ptr->_internal_size == 0) status->flags |= ERR_SIZE_ZERO;
|
{
|
||||||
|
status->flags |= ERR_BUF_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return status->flags;
|
return status->flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((overloadable)) size_t
|
__attribute__((overloadable)) size_t
|
||||||
__null_check(const char* ptr, err_t* status)
|
__null_check(const char* ptr, err_t* status)
|
||||||
{
|
{
|
||||||
if (ptr == nullptr) status->flags |= ERR_BUF_NULL;
|
if (status == nullptr)
|
||||||
|
{
|
||||||
|
return ERR_ERR_T_NULL;
|
||||||
|
}
|
||||||
|
if (ptr == nullptr)
|
||||||
|
{
|
||||||
|
status->flags |= ERR_BUF_NULL;
|
||||||
|
}
|
||||||
return status->flags;
|
return status->flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
__origin_override(err_t* status, char* str)
|
__origin_override(err_t* status, char* str)
|
||||||
{
|
{
|
||||||
|
if (status == nullptr)
|
||||||
|
{
|
||||||
|
return ERR_ERR_T_NULL;
|
||||||
|
}
|
||||||
if (status->origin != nullptr)
|
if (status->origin != nullptr)
|
||||||
{
|
{
|
||||||
status->flags |= ERR_ORIGIN_UNALTERABLE;
|
status->flags |= ERR_ORIGIN_UNALTERABLE;
|
||||||
@@ -48,9 +68,14 @@ __origin_override(err_t* status, char* str)
|
|||||||
void
|
void
|
||||||
_vec_char_print_vec(const _VecCharInternal* ptr, err_t* err)
|
_vec_char_print_vec(const _VecCharInternal* ptr, err_t* err)
|
||||||
{
|
{
|
||||||
if (__null_check(ptr, err) != 0) return;
|
if (__null_check(ptr, err) != 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (size_t i = 0; i < ptr->_internal_size; i++)
|
for (size_t i = 0; i < ptr->_internal_size; i++)
|
||||||
|
{
|
||||||
printf("%c ", ptr->_internal_arr[i]);
|
printf("%c ", ptr->_internal_arr[i]);
|
||||||
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +85,13 @@ _vec_char_cpy(const char* src, const size_t src_size, err_t* err)
|
|||||||
_VecCharInternal self = VEC_INIT;
|
_VecCharInternal self = VEC_INIT;
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
|
|
||||||
if (__null_check(src, &status) != 0)
|
size_t null = __null_check(src, err);
|
||||||
|
if (null != 0)
|
||||||
{
|
{
|
||||||
|
if (null & ERR_ERR_T_NULL)
|
||||||
|
{
|
||||||
|
return self;
|
||||||
|
}
|
||||||
__origin_override(err, "CPY");
|
__origin_override(err, "CPY");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return self;
|
return self;
|
||||||
@@ -73,14 +103,17 @@ _vec_char_cpy(const char* src, const size_t src_size, err_t* err)
|
|||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
self._internal_arr = malloc(src_size * sizeof(char));
|
if (src_size > self._internal_max_size)
|
||||||
__null_check(self._internal_arr, &status);
|
|
||||||
if (status.flags != 0)
|
|
||||||
{
|
{
|
||||||
__origin_override(err, "CPY");
|
__origin_override(err, "CPY");
|
||||||
err->flags |= status.flags;
|
err->flags |= ERR_SIZE_OVERFLOW;
|
||||||
free(self._internal_arr);
|
return self;
|
||||||
self._internal_arr = NULL;
|
}
|
||||||
|
self._internal_arr = malloc(src_size * sizeof(char));
|
||||||
|
if (self._internal_arr == nullptr)
|
||||||
|
{
|
||||||
|
__origin_override(err, "CPY");
|
||||||
|
err->flags |= ERR_ALLOC_FAIL;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
memcpy(self._internal_arr, src, src_size * sizeof(char));
|
memcpy(self._internal_arr, src, src_size * sizeof(char));
|
||||||
@@ -115,18 +148,22 @@ _vec_char_push_back(_VecCharInternal* ptr, const char val, err_t* err)
|
|||||||
if (res == nullptr)
|
if (res == nullptr)
|
||||||
{
|
{
|
||||||
__origin_override(err, "PUSH_BACK");
|
__origin_override(err, "PUSH_BACK");
|
||||||
err->flags |= ERR_BUF_NULL;
|
err->flags |= ERR_ALLOC_FAIL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ptr->_internal_arr = res;
|
ptr->_internal_arr = res;
|
||||||
ptr->_internal_capacity = 1;
|
ptr->_internal_capacity = 1;
|
||||||
|
ptr->_internal_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (ptr->_internal_size + 1 > ptr->_internal_capacity)
|
else if (ptr->_internal_size + 1 > ptr->_internal_capacity)
|
||||||
{
|
{
|
||||||
size_t dst_cap =
|
size_t dst_cap =
|
||||||
(ptr->_internal_capacity / 2) + ptr->_internal_capacity;
|
(ptr->_internal_capacity / 2) + ptr->_internal_capacity;
|
||||||
if (dst_cap == ptr->_internal_capacity) dst_cap++;
|
if (dst_cap == ptr->_internal_capacity)
|
||||||
|
{
|
||||||
|
dst_cap++;
|
||||||
|
}
|
||||||
if (dst_cap > ptr->_internal_max_size)
|
if (dst_cap > ptr->_internal_max_size)
|
||||||
{
|
{
|
||||||
dst_cap = ptr->_internal_max_size;
|
dst_cap = ptr->_internal_max_size;
|
||||||
@@ -158,7 +195,14 @@ _vec_char_pop_back(_VecCharInternal* ptr, err_t* err)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr->_internal_arr[ptr->_internal_size - 1] = '\0';
|
if (ptr->_internal_size == 0)
|
||||||
|
{
|
||||||
|
__origin_override(err, "POP_BACK");
|
||||||
|
err->flags |= ERR_SIZE_ZERO;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr->_internal_arr[ptr->_internal_size - 1] = 0;
|
||||||
ptr->_internal_size--;
|
ptr->_internal_size--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,55 +210,41 @@ void
|
|||||||
_vec_char_reserve(_VecCharInternal* ptr, const size_t capacity, err_t* err)
|
_vec_char_reserve(_VecCharInternal* ptr, const size_t capacity, err_t* err)
|
||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
char* res;
|
if (__null_check(ptr, &status) != 0)
|
||||||
|
{
|
||||||
|
if ((status.flags & ERR_VEC_NULL) != 0)
|
||||||
|
{
|
||||||
|
__origin_override(err, "RESERVE");
|
||||||
|
err->flags |= ERR_VEC_NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ptr->_internal_max_size < capacity)
|
||||||
|
{
|
||||||
|
__origin_override(err, "RESERVE");
|
||||||
|
err->flags |= ERR_SIZE_OVERFLOW;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (capacity == 0)
|
if (capacity == 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "RESERVE");
|
__origin_override(err, "RESERVE");
|
||||||
err->flags |= ERR_SIZE_ZERO;
|
err->flags |= ERR_SIZE_ZERO;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (__null_check(ptr, &status) != 0)
|
|
||||||
{
|
char* res = realloc(ptr->_internal_arr, capacity * sizeof(char));
|
||||||
if ((status.flags & ERR_BUF_NULL) != 0)
|
|
||||||
{
|
|
||||||
res = calloc(ptr->_internal_capacity, sizeof(char));
|
|
||||||
if (res == nullptr)
|
if (res == nullptr)
|
||||||
{
|
{
|
||||||
__origin_override(err, "RESERVE");
|
__origin_override(err, "RESERVE");
|
||||||
err->flags |= ERR_ALLOC_FAIL;
|
err->flags |= ERR_ALLOC_FAIL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
__origin_override(err, "RESERVE");
|
|
||||||
err->flags |= (status.flags ^ ERR_BUF_NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res = realloc(ptr->_internal_arr, capacity * sizeof(char));
|
|
||||||
if (res == nullptr)
|
|
||||||
{
|
|
||||||
__origin_override(err, "RESERVE");
|
|
||||||
err->flags |= ERR_ALLOC_FAIL;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ptr->_internal_size > capacity)
|
|
||||||
{
|
|
||||||
for (size_t i = capacity; i < ptr->_internal_size; i++)
|
|
||||||
{
|
|
||||||
ptr->_internal_arr[i] = '\0';
|
|
||||||
}
|
|
||||||
ptr->_internal_size = capacity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ptr->_internal_arr = res;
|
ptr->_internal_arr = res;
|
||||||
ptr->_internal_capacity = capacity;
|
ptr->_internal_capacity = capacity;
|
||||||
ptr->_internal_max_size = capacity;
|
if (ptr->_internal_size > capacity)
|
||||||
|
{
|
||||||
|
ptr->_internal_size = capacity;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,46 +252,39 @@ void
|
|||||||
_vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err)
|
_vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err)
|
||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
__null_check(ptr, &status);
|
|
||||||
if (__null_check(ptr, &status) != 0)
|
if (__null_check(ptr, &status) != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "SHRINK_TO_FIT");
|
__origin_override(err, "SHRINK_TO_FIT");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return _vec_char_reserve(ptr, ptr->_internal_size, err);
|
if (ptr->_internal_size == 0)
|
||||||
|
{
|
||||||
|
__origin_override(err, "SHRINK_TO_FIT");
|
||||||
|
err->flags |= ERR_SIZE_ZERO;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_vec_char_reserve(ptr, ptr->_internal_size, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
_vec_char_swap(_VecCharInternal* ptr1, _VecCharInternal* ptr2, err_t* err)
|
_vec_char_swap(_VecCharInternal* ptr_a, _VecCharInternal* ptr_b, err_t* err)
|
||||||
{
|
{
|
||||||
_VecCharInternal self = VEC_INIT;
|
|
||||||
err_t status_1 = ERR_T_INIT, status_2 = ERR_T_INIT;
|
err_t status_1 = ERR_T_INIT, status_2 = ERR_T_INIT;
|
||||||
if (__null_check(ptr1, &status_1) != 0 ||
|
if ((__null_check(ptr_a, &status_1) & ERR_VEC_NULL) != 0 ||
|
||||||
__null_check(ptr2, &status_2) != 0)
|
(__null_check(ptr_b, &status_2) & ERR_VEC_NULL) != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "SWAP");
|
__origin_override(err, "SWAP");
|
||||||
err->flags |= status_1.flags | status_2.flags;
|
err->flags |= ERR_VEC_NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self._internal_arr = ptr1->_internal_arr;
|
_VecCharInternal self = *ptr_a;
|
||||||
ptr1->_internal_arr = ptr2->_internal_arr;
|
*ptr_a = *ptr_b;
|
||||||
ptr2->_internal_arr = self._internal_arr;
|
*ptr_b = self;
|
||||||
|
|
||||||
self._internal_capacity = ptr1->_internal_capacity;
|
|
||||||
self._internal_size = ptr1->_internal_size;
|
|
||||||
self._internal_max_size = ptr1->_internal_max_size;
|
|
||||||
ptr1->_internal_capacity = ptr2->_internal_capacity;
|
|
||||||
ptr1->_internal_size = ptr2->_internal_size;
|
|
||||||
ptr1->_internal_max_size = ptr2->_internal_max_size;
|
|
||||||
|
|
||||||
ptr2->_internal_capacity = self._internal_capacity;
|
|
||||||
ptr2->_internal_size = self._internal_size;
|
|
||||||
ptr2->_internal_max_size = self._internal_max_size;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
const char*
|
||||||
_vec_char_data(const _VecCharInternal* ptr, err_t* err)
|
_vec_char_data(const _VecCharInternal* ptr, err_t* err)
|
||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
@@ -274,6 +297,19 @@ _vec_char_data(const _VecCharInternal* ptr, err_t* err)
|
|||||||
return &ptr->_internal_arr[0];
|
return &ptr->_internal_arr[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
_vec_char_size(const _VecCharInternal* ptr, err_t* err)
|
||||||
|
{
|
||||||
|
err_t status = ERR_T_INIT;
|
||||||
|
if (__null_check(ptr, &status) != 0)
|
||||||
|
{
|
||||||
|
__origin_override(err, "SIZE");
|
||||||
|
err->flags |= status.flags;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return ptr->_internal_size;
|
||||||
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err)
|
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err)
|
||||||
{
|
{
|
||||||
@@ -301,7 +337,7 @@ _vec_char_max_size(const _VecCharInternal* ptr, err_t* err)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
_vec_char_delete(const _VecCharInternal* ptr, err_t* err)
|
_vec_char_delete(_VecCharInternal* ptr, err_t* err)
|
||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
if (__null_check(ptr, &status) != 0)
|
if (__null_check(ptr, &status) != 0)
|
||||||
@@ -310,7 +346,13 @@ _vec_char_delete(const _VecCharInternal* ptr, err_t* err)
|
|||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
free((void*)ptr->_internal_arr);
|
if (ptr->_internal_arr != nullptr)
|
||||||
|
{
|
||||||
|
free(ptr->_internal_arr);
|
||||||
|
ptr->_internal_arr = nullptr;
|
||||||
|
}
|
||||||
|
ptr->_internal_size = 0;
|
||||||
|
ptr->_internal_capacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -325,15 +367,12 @@ _vec_char_clear(_VecCharInternal* ptr, err_t* err)
|
|||||||
}
|
}
|
||||||
if (ptr->_internal_size > 0)
|
if (ptr->_internal_size > 0)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < ptr->_internal_size; i++)
|
memset(ptr->_internal_arr, 0, ptr->_internal_size);
|
||||||
{
|
|
||||||
ptr->_internal_arr[i] = 0;
|
|
||||||
}
|
|
||||||
ptr->_internal_size = 0;
|
ptr->_internal_size = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char
|
char*
|
||||||
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err)
|
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err)
|
||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
@@ -341,16 +380,15 @@ _vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err)
|
|||||||
{
|
{
|
||||||
__origin_override(err, "AT");
|
__origin_override(err, "AT");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return status.flags;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if (idx >= ptr->_internal_size)
|
if (idx >= ptr->_internal_size)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
__origin_override(err, "AT");
|
||||||
"At Index: [%zu] is out of bounds for size: [%zu]\n",
|
err->flags |= ERR_OUT_OF_BOUNDS;
|
||||||
idx, ptr->_internal_size);
|
return nullptr;
|
||||||
return ERR_OUT_OF_BOUNDS;
|
|
||||||
}
|
}
|
||||||
return ptr->_internal_arr[idx];
|
return &ptr->_internal_arr[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
@@ -376,7 +414,13 @@ _vec_char_end(const _VecCharInternal* ptr, err_t* err)
|
|||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return status.flags;
|
return status.flags;
|
||||||
}
|
}
|
||||||
return (int)(ptr->_internal_size - 1);
|
if (ptr->_internal_size == 0)
|
||||||
|
{
|
||||||
|
__origin_override(err, "END");
|
||||||
|
err->flags |= ERR_SIZE_ZERO;
|
||||||
|
return status.flags;
|
||||||
|
}
|
||||||
|
return (size_t)(ptr->_internal_size - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char
|
char
|
||||||
@@ -387,9 +431,14 @@ _vec_char_front(const _VecCharInternal* ptr, err_t* err)
|
|||||||
{
|
{
|
||||||
__origin_override(err, "FRONT");
|
__origin_override(err, "FRONT");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return status.flags;
|
return -1;
|
||||||
}
|
}
|
||||||
return _vec_char_at(ptr, 0, err);
|
if (_vec_char_at(ptr, 0, err) == nullptr)
|
||||||
|
{
|
||||||
|
err->flags |= ERR_OUT_OF_BOUNDS;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return *_vec_char_at(ptr, 0, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
char
|
char
|
||||||
@@ -400,9 +449,14 @@ _vec_char_back(const _VecCharInternal* ptr, err_t* err)
|
|||||||
{
|
{
|
||||||
__origin_override(err, "BACK");
|
__origin_override(err, "BACK");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return status.flags;
|
return -1;
|
||||||
}
|
}
|
||||||
return _vec_char_at(ptr, ptr->_internal_size - 1, err);
|
if (_vec_char_at(ptr, 0, err) == nullptr)
|
||||||
|
{
|
||||||
|
err->flags |= ERR_OUT_OF_BOUNDS;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return *_vec_char_at(ptr, ptr->_internal_size - 1, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
@@ -411,19 +465,17 @@ _vec_char_empty(const _VecCharInternal* ptr, err_t* err)
|
|||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
if (__null_check(ptr, &status) != 0)
|
if (__null_check(ptr, &status) != 0)
|
||||||
{
|
{
|
||||||
if ((status.flags & ERR_SIZE_ZERO) != 0)
|
|
||||||
{
|
|
||||||
err->flags ^= ERR_SIZE_ZERO;
|
|
||||||
return SUCCESS;
|
|
||||||
}
|
|
||||||
__origin_override(err, "EMPTY");
|
__origin_override(err, "EMPTY");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return status.flags;
|
return status.flags;
|
||||||
}
|
}
|
||||||
|
if (ptr->_internal_size == 0)
|
||||||
|
{
|
||||||
|
return ptr->_internal_size;
|
||||||
|
}
|
||||||
return status.flags;
|
return status.flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Fix return value
|
|
||||||
__attribute__((overloadable)) void
|
__attribute__((overloadable)) void
|
||||||
_vec_char_erase(_VecCharInternal* ptr, const size_t iter, err_t* err)
|
_vec_char_erase(_VecCharInternal* ptr, const size_t iter, err_t* err)
|
||||||
{
|
{
|
||||||
@@ -434,7 +486,10 @@ _vec_char_erase(_VecCharInternal* ptr, const size_t iter, err_t* err)
|
|||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (iter >= ptr->_internal_size) return;
|
if (iter >= ptr->_internal_size)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
memmove(&ptr->_internal_arr[iter], &ptr->_internal_arr[iter + 1],
|
memmove(&ptr->_internal_arr[iter], &ptr->_internal_arr[iter + 1],
|
||||||
(ptr->_internal_size - iter - 1) * sizeof(char));
|
(ptr->_internal_size - iter - 1) * sizeof(char));
|
||||||
ptr->_internal_arr[ptr->_internal_size - 1] = 0;
|
ptr->_internal_arr[ptr->_internal_size - 1] = 0;
|
||||||
@@ -442,7 +497,6 @@ _vec_char_erase(_VecCharInternal* ptr, const size_t iter, err_t* err)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// // TODO: Fix return value
|
|
||||||
__attribute__((overloadable)) void
|
__attribute__((overloadable)) void
|
||||||
_vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
|
_vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
|
||||||
const size_t iter_end, err_t* err)
|
const size_t iter_end, err_t* err)
|
||||||
@@ -464,11 +518,8 @@ _vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
|
|||||||
size_t diff = iter_end - iter_start;
|
size_t diff = iter_end - iter_start;
|
||||||
memmove(&ptr->_internal_arr[iter_start], &ptr->_internal_arr[iter_end],
|
memmove(&ptr->_internal_arr[iter_start], &ptr->_internal_arr[iter_end],
|
||||||
(ptr->_internal_size - iter_end) * sizeof(char));
|
(ptr->_internal_size - iter_end) * sizeof(char));
|
||||||
for (size_t i = ptr->_internal_size - diff; i < ptr->_internal_size;
|
memset(&ptr->_internal_arr[ptr->_internal_size - diff], 0,
|
||||||
i++)
|
sizeof(char) * diff);
|
||||||
{
|
|
||||||
ptr->_internal_arr[i] = 0;
|
|
||||||
}
|
|
||||||
ptr->_internal_size -= diff;
|
ptr->_internal_size -= diff;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -478,10 +529,10 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
|
|||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
__null_check(ptr, &status);
|
__null_check(ptr, &status);
|
||||||
if ((status.flags & (ERR_BUF_NULL | ERR_VEC_NULL)) != 0)
|
if ((status.flags & ERR_VEC_NULL) != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "RESIZE");
|
__origin_override(err, "RESIZE");
|
||||||
err->flags |= ERR_BUF_NULL | ERR_VEC_NULL;
|
err->flags |= ERR_VEC_NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -499,7 +550,7 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ((status.flags & ERR_SIZE_ZERO) != 0)
|
else if ((status.flags & ERR_BUF_NULL) != 0 || ptr->_internal_size == 0)
|
||||||
{
|
{
|
||||||
size_t max = ptr->_internal_max_size;
|
size_t max = ptr->_internal_max_size;
|
||||||
err_t verr = ERR_T_INIT;
|
err_t verr = ERR_T_INIT;
|
||||||
@@ -518,7 +569,7 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
|
|||||||
}
|
}
|
||||||
for (size_t i = 0; i < size; i++)
|
for (size_t i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
ptr->_internal_arr[i] = '\0';
|
ptr->_internal_arr[i] = 0;
|
||||||
}
|
}
|
||||||
ptr->_internal_max_size = max;
|
ptr->_internal_max_size = max;
|
||||||
ptr->_internal_size = size;
|
ptr->_internal_size = size;
|
||||||
@@ -554,17 +605,13 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err)
|
|||||||
}
|
}
|
||||||
if (size < ptr->_internal_size)
|
if (size < ptr->_internal_size)
|
||||||
{
|
{
|
||||||
for (size_t i = size; i < ptr->_internal_size; i++)
|
memset(&ptr->_internal_arr[size], 0,
|
||||||
{
|
ptr->_internal_size - size);
|
||||||
ptr->_internal_arr[i] = '\0';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
|
if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
|
||||||
{
|
{
|
||||||
for (size_t i = ptr->_internal_size; i < size; i++)
|
memset(&ptr->_internal_arr[ptr->_internal_size], 0,
|
||||||
{
|
size - ptr->_internal_size);
|
||||||
ptr->_internal_arr[i] = '\0';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ptr->_internal_size = size;
|
ptr->_internal_size = size;
|
||||||
return;
|
return;
|
||||||
@@ -576,10 +623,10 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
|
|||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
__null_check(ptr, &status);
|
__null_check(ptr, &status);
|
||||||
if ((status.flags & (ERR_BUF_NULL | ERR_VEC_NULL)) != 0)
|
if ((status.flags & ERR_VEC_NULL) != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "RESIZE");
|
__origin_override(err, "RESIZE");
|
||||||
err->flags |= ERR_BUF_NULL | ERR_VEC_NULL;
|
err->flags |= ERR_VEC_NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ptr->_internal_size == size)
|
if (ptr->_internal_size == size)
|
||||||
@@ -596,7 +643,7 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ((status.flags & ERR_SIZE_ZERO) != 0)
|
else if ((status.flags & ERR_BUF_NULL) != 0 || ptr->_internal_size == 0)
|
||||||
{
|
{
|
||||||
size_t max = ptr->_internal_max_size;
|
size_t max = ptr->_internal_max_size;
|
||||||
err_t verr = ERR_T_INIT;
|
err_t verr = ERR_T_INIT;
|
||||||
@@ -645,17 +692,13 @@ _vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
|
|||||||
}
|
}
|
||||||
if (size < ptr->_internal_size)
|
if (size < ptr->_internal_size)
|
||||||
{
|
{
|
||||||
for (size_t i = size; i < ptr->_internal_size; i++)
|
memset(&ptr->_internal_arr[size], val,
|
||||||
{
|
ptr->_internal_size - size);
|
||||||
ptr->_internal_arr[i] = val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
|
if (ptr->_internal_size < size && size <= ptr->_internal_capacity)
|
||||||
{
|
{
|
||||||
for (size_t i = ptr->_internal_size; i < size; i++)
|
memset(&ptr->_internal_arr[ptr->_internal_size], val,
|
||||||
{
|
size - ptr->_internal_size);
|
||||||
ptr->_internal_arr[i] = val;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ptr->_internal_size = size;
|
ptr->_internal_size = size;
|
||||||
return;
|
return;
|
||||||
@@ -702,8 +745,13 @@ _vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator,
|
|||||||
}
|
}
|
||||||
for (size_t i = iter_start; i < iter_end; i++)
|
for (size_t i = iter_start; i < iter_end; i++)
|
||||||
{
|
{
|
||||||
_vec_char_push_back(ptr, _vec_char_at(iterator, i, &status),
|
char* res = _vec_char_at(iterator, i, &status);
|
||||||
&status);
|
if (res == nullptr)
|
||||||
|
{
|
||||||
|
err->flags |= ERR_OUT_OF_BOUNDS;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_vec_char_push_back(ptr, *res, &status);
|
||||||
if (status.flags != 0)
|
if (status.flags != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "ASSIGN");
|
__origin_override(err, "ASSIGN");
|
||||||
@@ -723,6 +771,7 @@ _vec_char_assign(_VecCharInternal* ptr, const size_t amount, const char val,
|
|||||||
{
|
{
|
||||||
__origin_override(err, "ASSIGN");
|
__origin_override(err, "ASSIGN");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (amount == 0)
|
if (amount == 0)
|
||||||
{
|
{
|
||||||
@@ -764,12 +813,27 @@ _vec_char_insert(_VecCharInternal* ptr, const size_t iter, const char val,
|
|||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
__null_check(ptr, &status);
|
__null_check(ptr, &status);
|
||||||
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
|
if ((status.flags & ERR_VEC_NULL) != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "INSERT");
|
__origin_override(err, "INSERT");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if ((status.flags & ERR_BUF_NULL) != 0)
|
||||||
|
{
|
||||||
|
char* res = malloc(1 * sizeof(char));
|
||||||
|
if (res == nullptr)
|
||||||
|
{
|
||||||
|
__origin_override(err, "INSERT");
|
||||||
|
err->flags |= ERR_BUF_NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ptr->_internal_arr = res;
|
||||||
|
ptr->_internal_capacity = 1;
|
||||||
|
ptr->_internal_arr[0] = val;
|
||||||
|
ptr->_internal_size = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (ptr->_internal_max_size < ptr->_internal_size + 1)
|
if (ptr->_internal_max_size < ptr->_internal_size + 1)
|
||||||
{
|
{
|
||||||
__origin_override(err, "INSERT");
|
__origin_override(err, "INSERT");
|
||||||
@@ -803,6 +867,7 @@ _vec_char_insert(_VecCharInternal* ptr, const size_t iter, const size_t amount,
|
|||||||
const char val, err_t* err)
|
const char val, err_t* err)
|
||||||
{
|
{
|
||||||
err_t status = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
|
__null_check(ptr, &status);
|
||||||
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
|
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "INSERT");
|
__origin_override(err, "INSERT");
|
||||||
@@ -815,7 +880,7 @@ _vec_char_insert(_VecCharInternal* ptr, const size_t iter, const size_t amount,
|
|||||||
err->flags |= ERR_OUT_OF_BOUNDS;
|
err->flags |= ERR_OUT_OF_BOUNDS;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (amount <= 0)
|
if (amount == 0)
|
||||||
{
|
{
|
||||||
__origin_override(err, "INSERT");
|
__origin_override(err, "INSERT");
|
||||||
err->flags |= ERR_SIZE_ZERO;
|
err->flags |= ERR_SIZE_ZERO;
|
||||||
@@ -843,13 +908,12 @@ _vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator,
|
|||||||
__null_check(ptr, &status);
|
__null_check(ptr, &status);
|
||||||
__null_check(iterator, &status);
|
__null_check(iterator, &status);
|
||||||
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
|
if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0)
|
||||||
|
|
||||||
{
|
{
|
||||||
__origin_override(err, "INSERT");
|
__origin_override(err, "INSERT");
|
||||||
err->flags |= status.flags;
|
err->flags |= status.flags;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (iter_end > iter_start || iter_end > iterator->_internal_size ||
|
if (iter_end <= iter_start || iter_end > iterator->_internal_size ||
|
||||||
iter > ptr->_internal_size)
|
iter > ptr->_internal_size)
|
||||||
{
|
{
|
||||||
__origin_override(err, "INSERT");
|
__origin_override(err, "INSERT");
|
||||||
|
|||||||
+18
-9
@@ -1,13 +1,10 @@
|
|||||||
#ifndef CHAR_H
|
#ifndef CHAR_H
|
||||||
#define CHAR_H
|
#define CHAR_H
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <utils/utils.h>
|
#include <utils/utils.h>
|
||||||
|
|
||||||
|
// Will add DOCS later
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char* _internal_arr;
|
char* _internal_arr;
|
||||||
@@ -16,6 +13,15 @@ typedef struct
|
|||||||
size_t _internal_max_size;
|
size_t _internal_max_size;
|
||||||
} _VecCharInternal;
|
} _VecCharInternal;
|
||||||
|
|
||||||
|
__attribute__((overloadable)) size_t
|
||||||
|
__null_check(const _VecCharInternal* ptr, err_t* status);
|
||||||
|
|
||||||
|
__attribute__((overloadable)) size_t
|
||||||
|
__null_check(const char* ptr, err_t* status);
|
||||||
|
|
||||||
|
size_t
|
||||||
|
__origin_override(err_t* status, char* str);
|
||||||
|
|
||||||
void
|
void
|
||||||
_vec_char_print_vec(const _VecCharInternal* ptr, err_t* err);
|
_vec_char_print_vec(const _VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
@@ -35,11 +41,14 @@ void
|
|||||||
_vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err);
|
_vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
void
|
void
|
||||||
_vec_char_swap(_VecCharInternal* ptr, _VecCharInternal* ptr_other, err_t* err);
|
_vec_char_swap(_VecCharInternal* ptr_a, _VecCharInternal* ptr_b, err_t* err);
|
||||||
|
|
||||||
char*
|
const char*
|
||||||
_vec_char_data(const _VecCharInternal* ptr, err_t* err);
|
_vec_char_data(const _VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
|
size_t
|
||||||
|
_vec_char_size(const _VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err);
|
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
@@ -47,12 +56,12 @@ size_t
|
|||||||
_vec_char_max_size(const _VecCharInternal* ptr, err_t* err);
|
_vec_char_max_size(const _VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
void
|
void
|
||||||
_vec_char_delete(const _VecCharInternal* ptr, err_t* err);
|
_vec_char_delete(_VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
void
|
void
|
||||||
_vec_char_clear(_VecCharInternal* ptr, err_t* err);
|
_vec_char_clear(_VecCharInternal* ptr, err_t* err);
|
||||||
|
|
||||||
char
|
char*
|
||||||
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err);
|
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err);
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ vec_get_error_desc(ERROR_CODES err)
|
|||||||
ERROR_LIST(X)
|
ERROR_LIST(X)
|
||||||
#undef X
|
#undef X
|
||||||
default:
|
default:
|
||||||
"Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ static const size_t VEC_MAX = SIZE_MAX / sizeof(int);
|
|||||||
"Max :- Vector Max value is overloaded") \
|
"Max :- Vector Max value is overloaded") \
|
||||||
X(ERR_ORIGIN_UNALTERABLE, (1u << 7), \
|
X(ERR_ORIGIN_UNALTERABLE, (1u << 7), \
|
||||||
"Origin :- Origin is non-null and cannot be overridden") \
|
"Origin :- Origin is non-null and cannot be overridden") \
|
||||||
X(ERR_STR_NULL, (1u << 8), "String :- String is null value")
|
X(ERR_STR_NULL, (1u << 8), "String :- String is null value") \
|
||||||
|
X(ERR_ERR_T_NULL, (1u << 9), "ERR_T :- Passed error is null")
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
@@ -56,18 +57,11 @@ vec_get_error_desc(ERROR_CODES err);
|
|||||||
void
|
void
|
||||||
vec_print_errs(const err_t err);
|
vec_print_errs(const err_t err);
|
||||||
|
|
||||||
// void
|
|
||||||
// _vec_update_origin(err_t* err, char* str);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
print_bits(unsigned int num);
|
print_bits(unsigned int num);
|
||||||
|
|
||||||
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
||||||
#define DEBUG(msg) printf("DEBUB: %s\n", msg) /**< Print debug message*/
|
#define DEBUG(msg) printf("DEBUG: %s\n", msg) /**< Print debug message*/
|
||||||
#define LOGERROR(error_code) \
|
|
||||||
printf("ERROR: %s\n", \
|
|
||||||
error_messages[error_code]) /**< Print log error message*/
|
|
||||||
|
|
||||||
#define find_len(arr) sizeof(arr) / sizeof(arr[0])
|
#define find_len(arr) sizeof(arr) / sizeof(arr[0])
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+2
-60
@@ -12,13 +12,11 @@
|
|||||||
* String 32 Bits with suffix of 's'
|
* String 32 Bits with suffix of 's'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// #include <libvec.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lib/char/char.h"
|
|
||||||
#include "lib/utils/utils.h"
|
#include "lib/utils/utils.h"
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -36,70 +34,14 @@ main()
|
|||||||
//
|
//
|
||||||
// Your pop back is a great example of how to handle it and is a clean
|
// Your pop back is a great example of how to handle it and is a clean
|
||||||
// if statement as a header to all functions.
|
// if statement as a header to all functions.
|
||||||
char res[4] = { 'a', 'b', 'c', 'd' };
|
// char res[4] = { 'a', 'b', 'c', 'd' };
|
||||||
err_t status = ERR_T_INIT, err = ERR_T_INIT;
|
err_t status = ERR_T_INIT;
|
||||||
status.flags |= ERR_BUF_NULL | ERR_VEC_NULL | ERR_STR_NULL;
|
status.flags |= ERR_BUF_NULL | ERR_VEC_NULL | ERR_STR_NULL;
|
||||||
// _VecCharInternal v1 = VEC_INIT;
|
|
||||||
// _vec_char_push_back(&v1, 'c', &status);
|
|
||||||
// _vec_char_print_vec(&v1, &status);
|
|
||||||
|
|
||||||
unsigned int mask = 0;
|
unsigned int mask = 0;
|
||||||
mask = ERR_BUF_NULL ^ status.flags;
|
mask = ERR_BUF_NULL ^ status.flags;
|
||||||
|
|
||||||
print_bits(mask);
|
print_bits(mask);
|
||||||
print_bits(status.flags);
|
print_bits(status.flags);
|
||||||
|
|
||||||
// if (status.origin)
|
|
||||||
// {
|
|
||||||
// printf("Error started in: %s\n", status.origin);
|
|
||||||
// }
|
|
||||||
// printf("%s: %s", err.origin, vec_print_errs(err));
|
|
||||||
// vec_print_errs(status);
|
|
||||||
// printf("%u\n", status.err);
|
|
||||||
// _vec_update_origin(&err, "Test");
|
|
||||||
// printf("%s", err.origin);
|
|
||||||
// for (int i = 0; i < VEC_FAULT_BITS; i++)
|
|
||||||
// {
|
|
||||||
// print_bits(mark);
|
|
||||||
// mark <<= 1;
|
|
||||||
// }
|
|
||||||
// const char* str = vec_get_error_desc(ERR_ALLOC_FAIL);
|
|
||||||
// const unsigned int error = ERR_BUF_NULL;
|
|
||||||
// if (!(ERR_ALLOC_FAIL & SUCCESS))
|
|
||||||
// {
|
|
||||||
// printf("Hello\n");
|
|
||||||
// }
|
|
||||||
// printf("%s", str);
|
|
||||||
// char ar1[] = { '1', '2', '3', '4' };
|
|
||||||
// char ar2[] = { 'a', 'b', 'c', 'd' };
|
|
||||||
// Vec8_t v1 = create(ar2, find_len(ar2));
|
|
||||||
// v1 = reserve(&v1, 9);
|
|
||||||
// print_vec(&v1);
|
|
||||||
// v1 = insert(&v1, 1, 'x');
|
|
||||||
// v1 = resize(&v1, 12);
|
|
||||||
// print_vec(&v1);
|
|
||||||
// v2 = resize(&v2, 3);
|
|
||||||
|
|
||||||
// print_vec(&v2);
|
|
||||||
|
|
||||||
// if (swap(&v1, &v2))
|
|
||||||
// {
|
|
||||||
// print_vec(&v1);
|
|
||||||
// print_vec(&v2);
|
|
||||||
// }
|
|
||||||
// Vec8_t v1 = create(nullptr, 0);
|
|
||||||
// v1 = reserve(&v1, 10);
|
|
||||||
// v1 = pop_back(&v1);
|
|
||||||
// for (int i = 0; i < 10; i++)
|
|
||||||
// {
|
|
||||||
// v1 = push_back(&v1, 'a');
|
|
||||||
// }
|
|
||||||
// print_vec(&v1);
|
|
||||||
// printf("SIZE: %zu\n", v1.size);
|
|
||||||
// v1 = push_back(&v1, 'a');
|
|
||||||
// print_vec(&v1);
|
|
||||||
// printf("SIZE: %zu\n", v1.size);
|
|
||||||
// delete(&v1);
|
|
||||||
// delete(&v2);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "char/char.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||||
|
{
|
||||||
|
if (size < 2) return 0;
|
||||||
|
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
|
||||||
|
// Use input bytes to drive arbitrary vector operations
|
||||||
|
for (size_t i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
uint8_t op = data[i] % 5;
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
_vec_char_push_back(&vec, (char)data[i], &err);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
_vec_char_pop_back(&vec, &err);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (vec._internal_size > 0)
|
||||||
|
{
|
||||||
|
size_t idx = (size_t)data[i] %
|
||||||
|
vec._internal_size;
|
||||||
|
_vec_char_erase(&vec, idx, &err);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
_vec_char_reserve(&vec, (size_t)data[i], &err);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
_vec_char_shrink_to_fit(&vec, &err);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "char/char.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_empty_vector_underflows(void)
|
||||||
|
{
|
||||||
|
printf("[RUN] Testing empty vector boundaries & underflows...\n");
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
|
||||||
|
// 1. Pop back on an empty container must not underflow size_t (0 - 1)
|
||||||
|
_vec_char_pop_back(&vec, &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0 || err.flags != 0);
|
||||||
|
|
||||||
|
// 2. Reading back/end of an empty container
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_back(&vec, &err);
|
||||||
|
assert(err.flags != 0);
|
||||||
|
|
||||||
|
// 3. Reserving 0 capacity
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_reserve(&vec, 0, &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("[PASS] Empty vector boundaries handled.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_push_growth_and_shrink(void)
|
||||||
|
{
|
||||||
|
printf("[RUN] Testing push, capacity growth, and indexing...\n");
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
|
||||||
|
// Push elements to trigger initial malloc then realloc
|
||||||
|
for (char c = 'a'; c <= 'z'; c++)
|
||||||
|
{
|
||||||
|
_vec_char_push_back(&vec, c, &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
}
|
||||||
|
assert(vec._internal_size == 26);
|
||||||
|
assert(vec._internal_capacity >= 26);
|
||||||
|
|
||||||
|
// Verify boundary indexing
|
||||||
|
assert(_vec_char_front(&vec, &err) == 'a');
|
||||||
|
assert(_vec_char_back(&vec, &err) == 'z');
|
||||||
|
|
||||||
|
// Index out of bounds check
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_at(&vec, 26, &err);
|
||||||
|
assert(err.flags != 0);
|
||||||
|
|
||||||
|
// Shrink to fit verification
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_shrink_to_fit(&vec, &err);
|
||||||
|
assert(vec._internal_capacity == 26);
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("[PASS] Growth and indexing passed.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_range_insert_and_erase(void)
|
||||||
|
{
|
||||||
|
printf("[RUN] Testing range insertions and erasures...\n");
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
|
||||||
|
_vec_char_push_back(&vec, 'A', &err);
|
||||||
|
_vec_char_push_back(&vec, 'B', &err);
|
||||||
|
_vec_char_push_back(&vec, 'E', &err);
|
||||||
|
|
||||||
|
// Single insert between 'B' and 'E' at index 2
|
||||||
|
_vec_char_insert(&vec, 2, 'C', &err);
|
||||||
|
assert(vec._internal_size == 4);
|
||||||
|
assert(*_vec_char_at(&vec, 2, &err) == 'C');
|
||||||
|
|
||||||
|
// Erase test
|
||||||
|
_vec_char_erase(&vec, 2, &err);
|
||||||
|
assert(vec._internal_size == 3);
|
||||||
|
assert(*_vec_char_at(&vec, 2, &err) == 'E');
|
||||||
|
|
||||||
|
// Inverted range check: iter_end <= iter_start must report error
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_erase(&vec, 2, 1, &err);
|
||||||
|
assert(err.flags != 0);
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("[PASS] Range insert and erase passed.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
test_empty_vector_underflows();
|
||||||
|
test_push_growth_and_shrink();
|
||||||
|
test_range_insert_and_erase();
|
||||||
|
printf("\nAll char vector edge-case tests passed cleanly!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,686 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <char/char.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <utils/utils.h>
|
||||||
|
|
||||||
|
#if defined(__has_feature)
|
||||||
|
#if __has_feature(address_sanitizer)
|
||||||
|
const char*
|
||||||
|
__asan_default_options(void)
|
||||||
|
{
|
||||||
|
return "allocator_may_return_null=1";
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* =========================================================================
|
||||||
|
* 1. BASIC LIFECYCLE & NULL GUARDS
|
||||||
|
* ========================================================================= */
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_push_back_single(void)
|
||||||
|
{
|
||||||
|
printf("[1] Testing push_back with a single character...\n");
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'A', &err);
|
||||||
|
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(vec._internal_size == 1);
|
||||||
|
assert(vec._internal_arr[0] == 'A');
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_null_guards(void)
|
||||||
|
{
|
||||||
|
printf("[2] Testing null pointer checks in __null_check...\n");
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
size_t res1 = __null_check(&vec, nullptr);
|
||||||
|
assert(res1 == ERR_ERR_T_NULL);
|
||||||
|
|
||||||
|
size_t res2 = __null_check((const _VecCharInternal*)nullptr, &err);
|
||||||
|
assert(res2 == ERR_VEC_NULL);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
size_t res3 = __null_check(&vec, &err);
|
||||||
|
assert(res3 == ERR_BUF_NULL);
|
||||||
|
assert((err.flags & ERR_BUF_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
size_t res4 = __null_check(vec._internal_arr, &err);
|
||||||
|
assert(res4 == ERR_BUF_NULL);
|
||||||
|
assert((err.flags & ERR_BUF_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
size_t res5 = __null_check((const char*)nullptr, nullptr);
|
||||||
|
assert(res5 == ERR_ERR_T_NULL);
|
||||||
|
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_origin_override(void)
|
||||||
|
{
|
||||||
|
printf("[3] Testing __origin_override...\n");
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
size_t res_null_status = __origin_override(nullptr, "TEST");
|
||||||
|
assert(res_null_status == ERR_ERR_T_NULL);
|
||||||
|
|
||||||
|
size_t res = __origin_override(&err, nullptr);
|
||||||
|
assert(res == ERR_STR_NULL);
|
||||||
|
assert((err.flags & ERR_STR_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
__origin_override(&err, "INITIAL");
|
||||||
|
assert(strcmp(err.origin, "INITIAL") == 0);
|
||||||
|
|
||||||
|
// Override existing origin -> ERR_ORIGIN_UNALTERABLE
|
||||||
|
__origin_override(&err, "REPLACED");
|
||||||
|
assert((err.flags & ERR_ORIGIN_UNALTERABLE) != 0);
|
||||||
|
assert(strcmp(err.origin, "INITIAL") == 0);
|
||||||
|
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================================
|
||||||
|
* 2. PRINT & COPY
|
||||||
|
* ========================================================================= */
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_print_vec(void)
|
||||||
|
{
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
printf("[4] Testing print_vec with null vector\n");
|
||||||
|
_vec_char_print_vec(nullptr, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[5] Testing print_vec with null array\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_print_vec(&vec, &err);
|
||||||
|
assert((err.flags & ERR_BUF_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[6] Testing print_vec with null err\n");
|
||||||
|
_vec_char_print_vec(&vec, nullptr);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[7] Testing print_vec with valid parameters\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'a', &err);
|
||||||
|
_vec_char_push_back(&vec, 'b', &err);
|
||||||
|
_vec_char_print_vec(&vec, &err);
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_cpy(void)
|
||||||
|
{
|
||||||
|
char arr[3] = { 'a', 'b', 'c' };
|
||||||
|
_VecCharInternal blank = VEC_INIT;
|
||||||
|
|
||||||
|
printf("[8] Testing _vec_char_cpy with null source arr\n");
|
||||||
|
err_t err1 = ERR_T_INIT;
|
||||||
|
_VecCharInternal tmp = _vec_char_cpy(nullptr, 3, &err1);
|
||||||
|
assert(blank._internal_arr == tmp._internal_arr &&
|
||||||
|
blank._internal_capacity == tmp._internal_capacity &&
|
||||||
|
blank._internal_size == tmp._internal_size &&
|
||||||
|
blank._internal_max_size == tmp._internal_max_size);
|
||||||
|
assert((err1.flags & ERR_BUF_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[9] Testing _vec_char_cpy with null err\n");
|
||||||
|
_VecCharInternal res2 = _vec_char_cpy(arr, 3, nullptr);
|
||||||
|
assert(blank._internal_arr == res2._internal_arr &&
|
||||||
|
blank._internal_capacity == res2._internal_capacity &&
|
||||||
|
blank._internal_size == res2._internal_size &&
|
||||||
|
blank._internal_max_size == res2._internal_max_size);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[10] Testing _vec_char_cpy with size 0\n");
|
||||||
|
err_t err3 = ERR_T_INIT;
|
||||||
|
_VecCharInternal res3 = _vec_char_cpy(arr, 0, &err3);
|
||||||
|
assert(blank._internal_arr == res3._internal_arr &&
|
||||||
|
blank._internal_capacity == res3._internal_capacity &&
|
||||||
|
blank._internal_size == res3._internal_size &&
|
||||||
|
blank._internal_max_size == res3._internal_max_size);
|
||||||
|
assert((err3.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[11] Testing _vec_char_cpy with size exceeding max_size\n");
|
||||||
|
err_t err4 = ERR_T_INIT;
|
||||||
|
_VecCharInternal res4 =
|
||||||
|
_vec_char_cpy(arr, res3._internal_max_size + 1, &err4);
|
||||||
|
assert(blank._internal_arr == res4._internal_arr &&
|
||||||
|
blank._internal_capacity == res4._internal_capacity &&
|
||||||
|
blank._internal_size == res4._internal_size &&
|
||||||
|
blank._internal_max_size == res4._internal_max_size);
|
||||||
|
assert((err4.flags & ERR_SIZE_OVERFLOW) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[12] Testing _vec_char_cpy allocation failure\n");
|
||||||
|
err_t err5 = ERR_T_INIT;
|
||||||
|
size_t huge_size = ((size_t)1 << 40);
|
||||||
|
_VecCharInternal res5 = _vec_char_cpy(arr, huge_size, &err5);
|
||||||
|
assert((err5.flags & ERR_ALLOC_FAIL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[13] Testing _vec_char_cpy with valid inputs\n");
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
char res[4] = { 'a', 'b', 'c', 'd' };
|
||||||
|
_VecCharInternal vec = _vec_char_cpy(res, 4, &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(vec._internal_size == 4);
|
||||||
|
assert(vec._internal_capacity == 4);
|
||||||
|
assert(memcmp(vec._internal_arr, res, 4) == 0);
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================================
|
||||||
|
* 3. PUSH, POP, CAPACITY & SIZING
|
||||||
|
* ========================================================================= */
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_push_back(void)
|
||||||
|
{
|
||||||
|
printf("[14] Testing _vec_char_push_back null vec\n");
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
_vec_char_push_back(nullptr, 'a', &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[15] Testing _vec_char_push_back null err\n");
|
||||||
|
_VecCharInternal vec1 = VEC_INIT;
|
||||||
|
_vec_char_push_back(&vec1, 'a', nullptr);
|
||||||
|
assert(vec1._internal_size == 1);
|
||||||
|
_vec_char_delete(&vec1, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[16] Testing _vec_char_push_back size overflow\n");
|
||||||
|
_VecCharInternal vec2 = VEC_INIT;
|
||||||
|
err_t err2 = ERR_T_INIT;
|
||||||
|
vec2._internal_size = vec2._internal_max_size;
|
||||||
|
_vec_char_push_back(&vec2, 'a', &err2);
|
||||||
|
assert((err2.flags & ERR_SIZE_OVERFLOW) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[17] Testing push_back capacity growth realloc\n");
|
||||||
|
err_t err3 = ERR_T_INIT;
|
||||||
|
_VecCharInternal vec3 = VEC_INIT;
|
||||||
|
for (char c = '0'; c <= '9'; c++)
|
||||||
|
{
|
||||||
|
_vec_char_push_back(&vec3, c, &err3);
|
||||||
|
assert(err3.flags == 0);
|
||||||
|
}
|
||||||
|
assert(vec3._internal_size == 10);
|
||||||
|
assert(vec3._internal_capacity >= 10);
|
||||||
|
_vec_char_delete(&vec3, &err3);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_pop_back(void)
|
||||||
|
{
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
printf("[18] Testing _vec_char_pop_back null vector\n");
|
||||||
|
_vec_char_pop_back(nullptr, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[19] Testing _vec_char_pop_back size 0\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'x', &err);
|
||||||
|
_vec_char_pop_back(&vec,
|
||||||
|
&err); // size is now 0, but buffer is allocated
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_pop_back(&vec, &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[20] Testing _vec_char_pop_back valid pop\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'a', &err);
|
||||||
|
_vec_char_push_back(&vec, 'b', &err);
|
||||||
|
assert(vec._internal_size == 2);
|
||||||
|
_vec_char_pop_back(&vec, &err);
|
||||||
|
assert(vec._internal_size == 1);
|
||||||
|
assert(vec._internal_arr[0] == 'a');
|
||||||
|
assert(vec._internal_arr[1] == 0);
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_reserve(void)
|
||||||
|
{
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
printf("[21] Testing _vec_char_reserve null vector\n");
|
||||||
|
_vec_char_reserve(nullptr, 1, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[22] Testing _vec_char_reserve capacity 0\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_reserve(&vec, 0, &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[23] Testing _vec_char_reserve calloc on unallocated buffer\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_reserve(&vec, 32, &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(vec._internal_capacity == 32);
|
||||||
|
assert(vec._internal_arr != nullptr);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[24] Testing _vec_char_reserve realloc to larger capacity\n");
|
||||||
|
_vec_char_reserve(&vec, 64, &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(vec._internal_capacity == 64);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[25] Testing _vec_char_reserve truncating size\n");
|
||||||
|
vec._internal_size = 50;
|
||||||
|
_vec_char_reserve(&vec, 20, &err);
|
||||||
|
assert(vec._internal_size == 20);
|
||||||
|
assert(vec._internal_capacity == 20);
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_shrink_to_fit(void)
|
||||||
|
{
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
printf("[26] Testing _vec_char_shrink_to_fit null vector\n");
|
||||||
|
_vec_char_shrink_to_fit(nullptr, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf(
|
||||||
|
"[27] Testing _vec_char_shrink_to_fit size 0 with allocated "
|
||||||
|
"buffer\n");
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'a', &err);
|
||||||
|
_vec_char_pop_back(&vec, &err);
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_shrink_to_fit(&vec, &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
printf("Passed\n");
|
||||||
|
|
||||||
|
printf("[28] Testing _vec_char_shrink_to_fit normal shrinkage\n");
|
||||||
|
_vec_char_reserve(&vec, 128, &err);
|
||||||
|
for (char c = '0'; c < '5'; c++)
|
||||||
|
{
|
||||||
|
_vec_char_push_back(&vec, c, &err);
|
||||||
|
}
|
||||||
|
assert(vec._internal_capacity == 128);
|
||||||
|
assert(vec._internal_size == 5);
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_shrink_to_fit(&vec, &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(vec._internal_capacity == 5);
|
||||||
|
assert(vec._internal_size == 5);
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================================
|
||||||
|
* 4. GETTERS, ELEMENT ACCESS & CLEAR
|
||||||
|
* ========================================================================= */
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_getters_and_access(void)
|
||||||
|
{
|
||||||
|
printf("[29] Testing getters, clear, and boundary element access...\n");
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
// 1. Null pointer check
|
||||||
|
assert(_vec_char_data(nullptr, &err) == nullptr);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
assert(_vec_char_size(nullptr, &err) == 0);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
assert(_vec_char_capacity(nullptr, &err) == 0);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
assert(_vec_char_max_size(nullptr, &err) == 0);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
// 2. Unallocated buffer check for empty()
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
size_t unalloc_empty = _vec_char_empty(&vec, &err);
|
||||||
|
assert((err.flags & ERR_BUF_NULL) != 0);
|
||||||
|
assert(unalloc_empty != 0);
|
||||||
|
|
||||||
|
// 3. Allocate the buffer and pop so size is 0 with valid arr
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'x', &err);
|
||||||
|
_vec_char_pop_back(&vec, &err);
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
|
||||||
|
// Now _internal_arr is non-null and size is 0:
|
||||||
|
assert(_vec_char_empty(&vec, &err) == 0);
|
||||||
|
|
||||||
|
// Boundary element access on size 0
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
assert(_vec_char_front(&vec, &err) == 0 ||
|
||||||
|
(err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
assert(_vec_char_back(&vec, &err) == 0 ||
|
||||||
|
(err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
// 4. Populate container
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&vec, 'x', &err);
|
||||||
|
_vec_char_push_back(&vec, 'y', &err);
|
||||||
|
_vec_char_push_back(&vec, 'z', &err);
|
||||||
|
|
||||||
|
assert(_vec_char_size(&vec, &err) == 3);
|
||||||
|
assert(_vec_char_capacity(&vec, &err) >= 3);
|
||||||
|
assert(_vec_char_data(&vec, &err) == vec._internal_arr);
|
||||||
|
assert(_vec_char_front(&vec, &err) == 'x');
|
||||||
|
assert(_vec_char_back(&vec, &err) == 'z');
|
||||||
|
assert(_vec_char_begin(&vec, &err) == 0);
|
||||||
|
assert(_vec_char_end(&vec, &err) == 2);
|
||||||
|
|
||||||
|
// Out of bounds at()
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
assert(_vec_char_at(&vec, 10, &err) == nullptr);
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
// Clear container
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_clear(&vec, &err);
|
||||||
|
assert(vec._internal_size == 0);
|
||||||
|
assert(vec._internal_arr[0] == 0);
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
assert(vec._internal_arr == nullptr);
|
||||||
|
assert(vec._internal_size == 0);
|
||||||
|
assert(vec._internal_capacity == 0);
|
||||||
|
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================================
|
||||||
|
* 5. SWAP, ERASE, RESIZE, ASSIGN & INSERT
|
||||||
|
* ========================================================================= */
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_swap(void)
|
||||||
|
{
|
||||||
|
printf("[30] Testing _vec_char_swap...\n");
|
||||||
|
_VecCharInternal a = VEC_INIT;
|
||||||
|
_VecCharInternal b = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
// Null vector checks
|
||||||
|
_vec_char_swap(nullptr, &b, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_swap(&a, nullptr, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
// Normal swap
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_push_back(&a, 'A', &err);
|
||||||
|
_vec_char_push_back(&b, 'B', &err);
|
||||||
|
_vec_char_push_back(&b, 'C', &err);
|
||||||
|
|
||||||
|
_vec_char_swap(&a, &b, &err);
|
||||||
|
assert(a._internal_size == 2);
|
||||||
|
assert(a._internal_arr[0] == 'B');
|
||||||
|
assert(b._internal_size == 1);
|
||||||
|
assert(b._internal_arr[0] == 'A');
|
||||||
|
|
||||||
|
_vec_char_delete(&a, &err);
|
||||||
|
_vec_char_delete(&b, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_erase_variants(void)
|
||||||
|
{
|
||||||
|
printf("[31] Testing _vec_char_erase single & range overloads...\n");
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
for (char c = '0'; c <= '5'; c++)
|
||||||
|
{
|
||||||
|
_vec_char_push_back(&vec, c, &err);
|
||||||
|
} // "012345"
|
||||||
|
|
||||||
|
// Single element erase out of bounds (noop)
|
||||||
|
_vec_char_erase(&vec, 10, &err);
|
||||||
|
assert(vec._internal_size == 6);
|
||||||
|
|
||||||
|
// Erase index 2 ('2') -> "01345"
|
||||||
|
_vec_char_erase(&vec, 2, &err);
|
||||||
|
assert(vec._internal_size == 5);
|
||||||
|
assert(vec._internal_arr[2] == '3');
|
||||||
|
|
||||||
|
// Range erase invalid bounds
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_erase(&vec, 4, 2, &err); // iter_end <= iter_start
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_erase(&vec, 0, 10, &err); // iter_end > size
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
// Range erase middle: index 1 through 3 ("0[13]45" -> "045")
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_erase(&vec, 1, 3, &err);
|
||||||
|
assert(vec._internal_size == 3);
|
||||||
|
assert(vec._internal_arr[0] == '0');
|
||||||
|
assert(vec._internal_arr[1] == '4');
|
||||||
|
assert(vec._internal_arr[2] == '5');
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_resize(void)
|
||||||
|
{
|
||||||
|
printf("[32] Testing _vec_char_resize (zero-fill & value-fill)...\n");
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
// Null vector
|
||||||
|
_vec_char_resize((_VecCharInternal*)nullptr, 10, &err);
|
||||||
|
assert((err.flags & ERR_VEC_NULL) != 0);
|
||||||
|
|
||||||
|
// Resize unallocated buffer to size 5
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_resize(&vec, 5, &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(vec._internal_size == 5);
|
||||||
|
assert(vec._internal_arr[0] == 0);
|
||||||
|
|
||||||
|
// Same size requested
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_resize(&vec, 5, &err);
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
// Exceeds max size
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_resize(&vec, vec._internal_max_size + 1, &err);
|
||||||
|
assert((err.flags & ERR_SIZE_OVERFLOW) != 0);
|
||||||
|
|
||||||
|
// Grow with value fill
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_resize(&vec, 8, 'Z', &err);
|
||||||
|
assert(vec._internal_size == 8);
|
||||||
|
assert(vec._internal_arr[5] == 'Z');
|
||||||
|
assert(vec._internal_arr[7] == 'Z');
|
||||||
|
|
||||||
|
// Shrink with value fill
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_resize(&vec, 3, 'A', &err);
|
||||||
|
assert(vec._internal_size == 3);
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_assign(void)
|
||||||
|
{
|
||||||
|
printf("[33] Testing _vec_char_assign overloads...\n");
|
||||||
|
_VecCharInternal dst = VEC_INIT;
|
||||||
|
_VecCharInternal src = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
// Populate src: 'A', 'B', 'C', 'D', 'E'
|
||||||
|
for (char c = 'A'; c <= 'E'; c++)
|
||||||
|
{
|
||||||
|
_vec_char_push_back(&src, c, &err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate dst buffer first so it is not unallocated
|
||||||
|
_vec_char_push_back(&dst, 'z', &err);
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
|
||||||
|
// 1. Fill assign: 4 'X's
|
||||||
|
_vec_char_assign(&dst, 4, 'X', &err);
|
||||||
|
assert(err.flags == 0);
|
||||||
|
assert(dst._internal_size == 4);
|
||||||
|
assert(dst._internal_arr[0] == 'X');
|
||||||
|
assert(dst._internal_arr[3] == 'X');
|
||||||
|
|
||||||
|
// 2. Fill assign with 0 amount
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_assign(&dst, 0, 'Y', &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
|
||||||
|
// 3. Range assign self-buffer check
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_assign(&dst, &dst, 0, 2, &err);
|
||||||
|
assert((err.flags & ERR_DOUBLE_BUF) != 0);
|
||||||
|
|
||||||
|
// 4. Range assign invalid sub-range (start >= end)
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_assign(&dst, &src, 3, 2, &err);
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
// 5. Range assign out of bounds end
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_assign(&dst, &src, 0, 10, &err);
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
_vec_char_delete(&dst, &err);
|
||||||
|
_vec_char_delete(&src, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_insert(void)
|
||||||
|
{
|
||||||
|
printf("[34] Testing _vec_char_insert overloads...\n");
|
||||||
|
_VecCharInternal vec = VEC_INIT;
|
||||||
|
_VecCharInternal other = VEC_INIT;
|
||||||
|
err_t err = ERR_T_INIT;
|
||||||
|
|
||||||
|
// Single insert on unallocated buffer
|
||||||
|
_vec_char_insert(&vec, 0, 'M', &err);
|
||||||
|
assert(vec._internal_size == 1);
|
||||||
|
assert(vec._internal_arr[0] == 'M');
|
||||||
|
|
||||||
|
// Single insert out of bounds
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_insert(&vec, 5, 'O', &err);
|
||||||
|
assert((err.flags & ERR_OUT_OF_BOUNDS) != 0);
|
||||||
|
|
||||||
|
// Single insert at middle: index 0 ('A', then 'M' shifts to 1)
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_insert(&vec, 0, 'A', &err);
|
||||||
|
assert(vec._internal_size == 2);
|
||||||
|
assert(vec._internal_arr[0] == 'A');
|
||||||
|
assert(vec._internal_arr[1] == 'M');
|
||||||
|
|
||||||
|
// Repeated insert: insert 3 'B's at index 1 -> 'A', 'B', 'B', 'B', 'M'
|
||||||
|
_vec_char_insert(&vec, 1, 3, 'B', &err);
|
||||||
|
assert(vec._internal_size == 5);
|
||||||
|
assert(vec._internal_arr[1] == 'B');
|
||||||
|
assert(vec._internal_arr[3] == 'B');
|
||||||
|
assert(vec._internal_arr[4] == 'M');
|
||||||
|
|
||||||
|
// Repeated insert amount 0
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_insert(&vec, 0, 0, 'Z', &err);
|
||||||
|
assert((err.flags & ERR_SIZE_ZERO) != 0);
|
||||||
|
|
||||||
|
// Vector-from-vector range insert
|
||||||
|
_vec_char_push_back(&other, '1', &err);
|
||||||
|
_vec_char_push_back(&other, '2', &err);
|
||||||
|
|
||||||
|
// Double buffer check
|
||||||
|
err = (err_t)ERR_T_INIT;
|
||||||
|
_vec_char_insert(&vec, &vec, 0, 0, 1, &err);
|
||||||
|
assert((err.flags & ERR_DOUBLE_BUF) != 0);
|
||||||
|
|
||||||
|
_vec_char_delete(&vec, &err);
|
||||||
|
_vec_char_delete(&other, &err);
|
||||||
|
printf("Passed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =========================================================================
|
||||||
|
* MAIN TEST RUNNER
|
||||||
|
* ========================================================================= */
|
||||||
|
|
||||||
|
int
|
||||||
|
main(void)
|
||||||
|
{
|
||||||
|
printf("=========================================\n");
|
||||||
|
printf(" Starting Complete libvec Unit Test Suite\n");
|
||||||
|
printf("=========================================\n");
|
||||||
|
|
||||||
|
test_push_back_single();
|
||||||
|
test_null_guards();
|
||||||
|
test_origin_override();
|
||||||
|
test_print_vec();
|
||||||
|
test_cpy();
|
||||||
|
test_push_back();
|
||||||
|
test_pop_back();
|
||||||
|
test_reserve();
|
||||||
|
test_shrink_to_fit();
|
||||||
|
test_getters_and_access();
|
||||||
|
test_swap();
|
||||||
|
test_erase_variants();
|
||||||
|
test_resize();
|
||||||
|
test_assign();
|
||||||
|
test_insert();
|
||||||
|
|
||||||
|
printf("=========================================\n");
|
||||||
|
printf(" All 34 Test Suites Executed Successfully!\n");
|
||||||
|
printf("=========================================\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user