r/cprogramming • u/sadvadan • Jun 01 '26
memory safe C
https://github.com/sadvadan/memstruct
C is powerful enough to have the best performing memory safety suite for itself!
memstruct is a single header file C library (<400 LoC) that provides complete spatial & temporal safety to the caller program. performance: near native speed.
memory checks are compile time / hoisted / elided / pipelined. checks are opt-in and can be switched off in production if needed. its macro based API extends the language a bit to position C as the leading option for large scale projects.
memstruct is currently in advanced stages of testing. contributions and comments are welcome. have an early look!
P.S.: the project is 100% human crafted and contributions are also reqd to comply
edit; end note: memstruct has now become even better (at 320 LoC) by incorporating MCU programming & de/allocator indirection, thanks to some valuable feedback on here. if you've more to add you may respond here or participate on git.
42
u/unkindle_blue Jun 01 '26
You should take off the emojis of the readme bro, it's a good work but people see that and they say vibecoding, I mean it's ok but these days people really hate it, anyway it's a good job
7
u/WittyStick Jun 02 '26 edited Jun 02 '26
One thing to note on the use of embedded assembly is someone using this library may also use embedded assembly, but may be using intel syntax - so if they compile with
-masm=intelit would break your code.You should probably either add a pragma in the C code to control the assembly syntax, or include
.att_syntaxin your embedded assembly to control it for those specific regions.Alternatively (I prefer) to use GCC's combined assembly syntax. Eg:
The combined version works with both
-masm=attand-masm=intel. Anything not inside{}is included in both versions - and within{}, anything before|is included only forattand anything after|is included only forintelsyntax.More generally this extends to
{syntax1|syntax2|syntax3|...|syntaxN}, where the order is defined for a specific architecture if multiple syntaxes are available. For x86 we only have the two:{att|intel}18
u/non-existing-person Jun 01 '26
300 commits in a month, each commit is just
., da faq? Are AI tokens that expensive now that they don't generate commit messages to save on money? XD4
u/resin-sniffer Jun 02 '26
This is very cool!
But it would be even cooler, if you can make it usable in the baremetal world.
* Use plain C everywhere, do not use assembler. So that it can run on every MCU.
* Do not use OS calls directly (like mmap()/munmap()), call the implementation-specific function/macro.
* Support non-dynamic memory usage. For example, I have an area which has a fixed address and a length. I want to use only this area for memstruct. Perhaps, I have some other such areas with different addresses and lengths. I want to use them all and want to stay safe.
* Support memcpy() and memset() so that we can fast and safely copy and fill the buffers.
3
u/simmepi Jun 01 '26
I canāt see any potential issues with including a header file which redefines free(arg)!
3
u/yiyufromthe216 Jun 03 '26
Great work!Ā I recommend that you switch the license to LGPL or MPL or you can dual license under both.Ā I think they would encourage faster adoption as copyleft licenses.
1
u/lovelacedeconstruct Jun 01 '26
frymimori is back
1
u/Western_Guitar_9007 Jun 02 '26 edited Jun 03 '26
Edit: OP admitted itās vibe coded slop, go home everyone.
Cool idea, vibe code was hidden OK except for the tests. 7_hardening.c and 9_arena.c for example are clearly vibe coded so OP just did better hiding it in other places.
Letās see how it plays out. Writing this from my iPhone:
include āmstrct.hā
int main(void) { M(int*, foo,); M(malloc(4), foo, 12); m(foo, 11) = 123; free(foo); }
Heap overflow? Letās give it a try
Edit: confirmed lol
1
u/telionn Jun 03 '26
Nobody's gonna even think about using this if dynamic variables are all in global scope. Right off the bat, it means the number of memory allocations is constrained to a compile-time fixed amount unless you work around the problem with array allocation, a fast track to memory leaks and/or higher-order memory unsafeness.
But I imagine that the move to dynamic handles would kill your compile-time optimizations.
0
u/swdee Jun 01 '26
I have no idea why you would license that vibe code as AGPL.
-3
u/Willsxyz Jun 01 '26
Personally I donāt understand why we want āmemory safeā C
0
u/NutEmitter Jun 02 '26
AI slop
2
u/[deleted] Jun 02 '26
[removed] ā view removed comment