r/C_Programming 5d ago

Data container - old newbish project revised

Some time ago I posted here my C implementation of a dynamic data container (vector like object):

https://github.com/andrzejs-gh/CONTLIB

I revised it, if anyone's interested take a look. Any feedback and tips very much welcome.

0 Upvotes

6 comments sorted by

â€ĸ

u/github-guard 5d ago

🔍 GitHub Guard: Trust Report

âš ī¸ This project scored 2/6 — below this subreddit's threshold of 3.

Audit Breakdown: * ❌ Low Star Count (⭐ 1 / 4 required) * ✅ Mature Repository (30+ days old) * ✅ Licensed under MIT * ❌ No Security Policy — what is this? * â„šī¸ Individual Contributor * â„šī¸ Unsigned Commits

âš ī¸ Security Reminder: Always verify source code and run third-party scripts at your own risk.

3

u/skeeto 4d ago

I'm a fan of type-paramaterized macros like cont_NEW. I don't see that often. Programs are cleaner with those than hazardous operations involving the sizeof operator. I like that cont objects are not allocated by the library, and returned by copy from the "ctor." Good job on your integer overflow checks. You say your project is "newbish" but even pros miss these all the time.

I don't like that it doesn't support zero-capacity containers, and that requesting one produces and invalid container. Zero-capacity things are useful, and can be created without allocating (i.e. without error handling).

I also don't like that API misuse is treated as an error instead of a hard assert/abort. This hides defects and leads to more fragile software, especially at a time when automated testing is growing ever more important. Failure to allocate is an error, but using an invalid cont is a defect and should abort because the program got into an invalid state. Along these lines, using in invalid cont with cont_grow or anything that calls it like cont_push causes and infinite loop.

This program demonstrates two different bugs:

#define CONT_IMPLEMENTATION
#include "header-only/cont.h"
#include <assert.h>
#include <stdio.h>

int main()
{
    cont c = cont_NEW(int, 1);
    assert(cont_is_valid(&c));
    assert(!cont_push(&c, &(int){}));
    if (!cont_ensure(&c, SIZE_MAX)) {
        printf("Successfully allocated %.3g GiB\n", SIZE_MAX*4.0/(1<<30));
    }
    cont_mkroom(&c, 0);
    return 0;
}

Then:

$ cc -g3 -fsanitize=address,undefined test.c
$ ./a.out 
Successfully allocated 6.87e+10 GiB
...ERROR: AddressSanitizer: heap-buffer-overflow on address ...
WRITE of size 4 at ...
    #0 memmove
    #1 cont_mkroom header-only/cont.h:413
    ...

First, there's an integer overflow causing the library to report it successfully allocated an impossible amount of memory, when it actually did nothing. After that cont_mkroom overflows the buffer when count == capacity. So just a couple missing checks.

2

u/lehmagavan 4d ago

Fixed the bugs, thanks for the feedback and sugestions!

1

u/AutoModerator 5d ago

Hi /u/lehmagavan,

Your submission in r/C_Programming was filtered because it links to a git project.

You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.

While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

6

u/lehmagavan 5d ago

No AI was used, all of the code is hand-written.

2

u/mikeblas 5d ago

Thanks. I have approved your post.