https://github.com/skullchap/neverflow Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} skullchap / neverflow Public * Notifications * Fork 1 * Star 95 Set of macros that guard against buffer overflows. Based on C99 VLA feature. License MIT license 95 stars 1 fork Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights skullchap/neverflow This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 0 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/s] Use Git or checkout with SVN using the web URL. [gh repo clone skullc] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @skullchap skullchap misc text aligning ... 0b9a317 Jun 2, 2023 misc text aligning 0b9a317 Git stats * 11 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time LICENSE Initial commit June 2, 2023 05:38 README.md Update README.md June 2, 2023 19:01 neverflow.h misc text aligning June 2, 2023 19:46 View code neverflow.h Small set of macros that guard against buffer overflows. Based on C99 VLA feature. Note: keep in mind that Neverflow is not quite "battle" tested, and it's more like a pilot study of a feature at this moment. Changes License README.md [242778042-b7fe10e0-3c05-4716-8a27-94b343f17bf0] neverflow.h Small set of macros that guard against buffer overflows. Based on C99 VLA feature. a little prologue... Such feature of C as a variable length array (VLA) has been discussed more than once, and most of the time in a bad light. Continuous discussions on how it's dangerous and not safe, complete ban from Linux source code, ignore by Microsoft compiler and etc., led VLA as a feature to become optional since C11. And many still miss a very important detail of this feature how it's not just being able to declare runtime arrays on stack that can blow it at any time, no. It's declaration of variably modified types. Long story short, heres Dennnis Ritchie's paper on VLA's and this pretty informative StackOverflow answer to clarify real usage of VLAs more. Note: keep in mind that Neverflow is not quite "battle" tested, and it's more like a pilot study of a feature at this moment. Roughly speaking, there are two main macros to keep in mind: NEW to declare array, and AT to runtime check if index is in bounds and return address to element behind it. // NEW(TYPE, NAME, COUNT) // AT(NAME, IDX) #include "neverflow.h" int main(void) { NEW(int, myarr, 10); // 10 element array declaration int *p = AT(myarr, 4); // pointer to 5th element int v = *AT(myarr, 4); // getting value by dereferencing *AT(myarr, 4) = 56; // changing value directly by dereferencing *AT(myarr, 30) = 56; // here comes the oopsie doopsie // main.c:14: Buffer Overflow. Index [30] is out of range [0-9] // main.c:14: Function: main } To semantically better distinguish getting address of element in array or element itself, GET macro was made and is a shorthand to *AT . It literally defined as #define GET(NAME, IDX) *AT(NAME, IDX) little things... LET is a shorthand for __auto_type and easier type inference while mainly using AT. LET e1 = AT(myarr, 4); // e1 is pointer to an int LET e2 = *AT(myarr, 4); // e2 is int SIZE(myarr) returns size of allocated memory. DON'T USE sizeof directly on VLA without dereferencing! It will return sizeof pointer pointing to array LEN(myarr) returns number of elements in array. By default, Neverflow uses stdlib's calloc as an alloc function and gcc/clang feature for auto free/cleanup when array will be out of scope/block. To disable auto cleanup define NO_AUTOFREE before including neverflow.h. To use own alloc function define ALLOCF. #define NO_AUTOFREE // you will need to free yourself #define ALLOCF malloc #include "neverflow.h" ... Passing array to function while preserving neverflow features done with ARR macro using this way: void func(int count, ARR(int, arr, count)) { int c = LEN(arr); printf("ELEM COUNT: %d\n", c); // 10 *AT(arr, 12) = 42; // fails // main.c:13: Buffer Overflow. Index [12] is out of range [0-9] // main.c:13: Function: func } int main(void) { NEW(int, myarr, 10); int count = LEN(myarr); func(count, myarr); } As a nice sideeffect of ARR, it's also possible to "wrap" raw pointers/arrays this way: void func(int count, ARR(int, arr, count)) { int c = LEN(arr); printf("ELEM COUNT: %d\n", c); printf("6th elem: %d\n", GET(arr, 5)); // 42 } int main(void) { void *p = malloc(10 * sizeof(int)); ARR(int, myarr, 10) = p; *AT(myarr, 5) = 42; func(LEN(myarr), myarr); } Changes [0.0.2] - name mangling removed, added ARR() macro to ease passing arrays to functions. Another neat sideeffect of it, is possibility of wrapping raw pointer and providing runtime bound checking. [0.0.1] - initial release License MIT About Set of macros that guard against buffer overflows. Based on C99 VLA feature. Resources Readme License MIT license Stars 95 stars Watchers 1 watching Forks 1 fork Report repository Releases No releases published Packages 0 No packages published Languages * C 100.0% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.