# \[C\] #XK3 Getting a "double free or corruption (out)" error

**URL:** <https://forum.codecrafters.io/t/c-xk3-getting-a-double-free-or-corruption-out-error/15857>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [January 30, 2026, 2:14pm UTC](https://forum.codecrafters.io/t/c-xk3-getting-a-double-free-or-corruption-out-error/15857 "2026-01-30T14:14:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![retop56](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/retop56/32/2910_2.png) [@retop56](https://forum.codecrafters.io/u/retop56)\
**Post date:** [January 30, 2026, 2:14pm UTC](https://forum.codecrafters.io/t/c-xk3-getting-a-double-free-or-corruption-out-error/15857/1 "2026-01-30T14:14:42Z")

</div>

[Link to my GitHub repo](https://github.com/retop56/codecrafters-shell-c_take_two/tree/main)

This is the output I’m getting:

```bash
[tester::#XK3] Running tests for Stage #XK3 (Pipelines - Multi-command pipelines)
[tester::#XK3] [setup] export PATH=/tmp/blueberry/raspberry/pineapple:$PATH
[tester::#XK3] [setup] echo -e "mango\nblueberry\npineapple\npear\norange" > "/tmp/dog/file-73"
[tester::#XK3] Running ./your_program.sh
[your-program] $ cat /tmp/dog/file-73 | head -n 3 | wc
[your-program] 3 3 26
[tester::#XK3] ✓ Received expected output
[tester::#XK3] [setup] echo -n "orange" > "/tmp/pig/f-37"
[tester::#XK3] [setup] echo -n "grape" > "/tmp/pig/f-14"
[tester::#XK3] [setup] echo -n "pineapple" > "/tmp/pig/f-39"
[tester::#XK3] [setup] echo -n "mango" > "/tmp/pig/f-20"
[tester::#XK3] [setup] echo -n "banana" > "/tmp/pig/f-77"
[tester::#XK3] [setup] echo -n "pear" > "/tmp/pig/f-79"
[your-program] $ ls /tmp/pig | tail -n 5 | head -n 3 | grep "f-39"
[your-program] f-39
[your-program] double free or corruption (out)
[tester::#XK3] ^ Expected prompt ("$ ") but received "double free or corruption (out)"
[tester::#XK3] Assertion failed.
[tester::#XK3] Test failed

```

---

<div class="post-metadata">

**Author:** ![retop56](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/retop56/32/2910_2.png) [@retop56](https://forum.codecrafters.io/u/retop56)\
**Post date:** [January 31, 2026, 11:19am UTC](https://forum.codecrafters.io/t/c-xk3-getting-a-double-free-or-corruption-out-error/15857/4 "2026-01-31T11:19:50Z")

</div>

After taking a break from this challenge and coming back a day later, the error went away. I was able to complete the rest of the stages, but this error is now intermittently coming back and I’m not sure what is causing it.

---

<div class="post-metadata">

**Author:** ![retop56](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/retop56/32/2910_2.png) [@retop56](https://forum.codecrafters.io/u/retop56)\
**Post date:** [January 31, 2026, 12:34pm UTC](https://forum.codecrafters.io/t/c-xk3-getting-a-double-free-or-corruption-out-error/15857/5 "2026-01-31T12:34:34Z")

</div>

K another update:

I ran the program through Valgrind and it helped me diagnose the issue and solve it. I’ll give a quick summary in case anyone else has a similar issue.

I followed [Valgrind’s Quick Start Guide](https://valgrind.org/docs/manual/quick-start.html). Here is the error Valgrind gave me after running my program with it:

```shell
==9875== Invalid write of size 8
==9875== at 0x10AA26: add_cmd_args (cmd_arg_parser.c:71)
==9875== by 0x10D815: main (main.c:33)
==9875== Address 0x4b0a0f0 is 0 bytes after a block of size 80 alloc'd
==9875== at 0x484D953: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==9875== by 0x10A74E: create_args_obj (cmd_arg_parser.c:16)
==9875== by 0x10D7B2: main (main.c:25)

```

I created a struct to hold the arguments I parsed from the command-line (create\_args\_obj), but I set the initial size of the args array to hold 10 arguments:

```c
Args *ao = (Args *)malloc(sizeof(Args));
ao->size = 0;
ao->capacity = 10;
ao->args = (char **)calloc(ao->capacity, sizeof(char *));

```

In add\_cmd\_args, I wasn’t doing a bounds-check to make sure that ao→size wasn’t about to reach the capacity, and if so, resize ao→args. As a result, when my program tried parsing the following line from the test:

```bash
ls /tmp/pig | tail -n 5 | head -n 3 | grep "f-39"
 | | | | | | | | | | | | |
 | | | | | | | | | | | | |
 * * * * * * * * * * | | *
 1 2 3 4 5 6 7 8 9 10 \ * 13
                                      * 12
                                      11  

```

it was parsing more than 10 arguments without resizing ao→args and would corrupt memory by writing out-of-bounds. I added the following bounds-check to add\_cmd\_args:

```c
if (ao->size + 1 >= ao->capacity) {
      expand_args_array(ao);
    }
ao->args[ao->size++] = received_arg;

```

and it worked out.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [February 5, 2026, 12:42pm UTC](https://forum.codecrafters.io/t/c-xk3-getting-a-double-free-or-corruption-out-error/15857/7 "2026-02-05T12:42:56Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
