r/cprogramming • • 8d ago

Three free static analyzers say these small C functions are clean. Running them says otherwise.

4 Upvotes

I wrote four short C functions, each with one deliberate bug, ran clang --analyze, gcc -fanalyzer and cppcheck on them, and then ran the code under AddressSanitizer/UBSan with different kinds of input.

Results (gcc 13.3, clang 18.1.3, cppcheck 2.13, Ubuntu 24.04):

- off-by-one at exactly the buffer size: all three say CLEAN, ASan catches it on the boundary input

- negative length that passes a "too big" check: all three say CLEAN, ASan catches it

- 16-bit size truncation: only gcc -fanalyzer flags it

- use-after-free on an error path: all three find it, but cppcheck also reports a double free that never happens at runtime

The off-by-one one, for example:

if (n > CAP || len < 1 + n) return -1;

for (size_t i = 0; i <= n; i++)

dst[i] = ...;

In every case the normal input runs fine, so a test suite with only "normal" inputs would pass too.

This isn't meant as "these tools are bad". My takeaway is that a CLEAN result only covers what was actually checked: the build settings and the kinds of input. So I wrote a short note on how to report a CLEAN result together with that scope.

Everything runs in about a minute on Linux:

https://github.com/ericyunlinchiang/certify-the-checker

Question for you: which checker/flags would you expect to catch #1 and #2? If you run Clang-Tidy, CodeQL, Infer, PVS or anything else on them, I'd love to see the results. There's an issue template for that in the repo.


r/cprogramming • • 8d ago

Linguagem C Aula 21

Thumbnail
youtube.com
0 Upvotes

Estrutura de Controle Condicional (if de via única) em C | Desafio de Adivinhação


r/cprogramming • • 7d ago

First person to download the game gets to give ideas for two weeks

Thumbnail
adamplpld.itch.io
0 Upvotes

If you download my game first and prove it was you, you'll get to give me ideas for two weeks. Your idea will 100% be added as long as it's realistic and possible to make. If an idea can't be done, I'll let you know where to share ideas/feedback — you can also message me on Reddit or in DMs.

Even if you aren't first, feel free to download and try it — it's not a virus, I promise. If you want, I can even record screenshots or videos showing how the game looks or how downloading works. Please, if you're the first to actually download it, reply so I know who was first (if five people download it at once, I won't be able to tell who clicked first).

Others can still share ideas too — they'll be considered, and I'll pass them to the first person (mentioning who wrote it) so that person can help decide if an idea gets added. If you suggest an idea, please explain how it would work.

If you don't speak Polish and really want something added, message me and I'll explain how the game works in English. Unfortunately, translating the game itself into English isn't planned right now because it would be tough to make. It's not a 3D game — it's a console game, and the visuals/images used are from the internet (not sure if any are AI, I didn't make them). Hope you enjoy! r/cpp r/cprogramming r/game


r/cprogramming • • 7d ago

How C Programming Language Was Born: The $75 Game Behind C #shorts #c_l...

Thumbnail
youtube.com
0 Upvotes

r/cprogramming • • 7d ago

Help me

0 Upvotes

Guys I have question,

How the fuck can I learn c program in 5 days because my exam is coming in 5 days I m absolutely dumb guy and I don't have laptop as well as I m stroke patient since last 2 years where our 9 to 5 schedule is fully pack so I don't go in college and I don't have c language book as well I also want to learn matrix algebra, buisness mathematics, electronics their papers are also coming and I only their chapter

No student contact

No teacher contact

Only me and my fucking phone and you peoples

Please help me

(Btw I m doing graphic course in rehab because they provide us so that means I have 2 course 🥀😭 help meeeee)


r/cprogramming • • 9d ago

I built a zero dependencies complex linear algebra library from scratch on C99.

20 Upvotes

It has zero dependencies and is built entirely from C99 <complex.h> library. Anybody can use my library to write programs utilizing complex vector spaces be it Electrical Engineering, Signal Processing, Quantum Mechanics or even more.

I utilized single pointer flattened arrays for contiguous memory and cache efficiency.

I have included standalone example for every single function I have in my header file under the Examples/ folder.

I also built two pure C applications:

  1. Mesh Analysis on Bilateral Alternating Current Circuits to find mesh currents and power.
  2. von Neumann Quantum Entropy Calculator to measure entropy and uncertainty in qubit systems of pure and mixed probabilities.

Check out my https://github.com/CodeLeverEuler/c-complex-linalg

I'm a freshman and this is my first systems/math library in C. Would love feedback on my API design, memory contracts, or numerical stability edge cases!

Thank you
EDIT:
Some are suspicious that this is AI generated. I didn't know that was an issue. I thought I wouldn't make my repository public until it's done.
I can't share the image from my local file here. But if anyone is having second thoughts, I have timestamps on my private Git repository and local files


r/cprogramming • • 8d ago

Added a pure C99, Rust-inspired error handling module (<cerror.h>) to my library arstd

Thumbnail
github.com
0 Upvotes

Just added <cerror.h> to arstd (my C99 arena standard library) to bring Rust-style Result ergonomics to strict ISO C99—with zero compiler extensions (no typeof or statement expressions) so it compiles cleanly across GCC, Clang, and MSVC.

It includes typed Result wrappers, 3-arg and 4-arg TRY propagation, UNWRAP/EXPECT/UNWRAP_OR, and an optional custom error table with built-in CERROR_OK and CERROR_DEFAULT fallbacks:


CERROR_RESULT(int, ResInt);

CERROR_RESULT(const char *, ResStr);

ResInt parse_port(int port) {

if (port < 1 || port > 65535) return ERR(ResInt, cerror("ERR_INVALID_PORT"));

return OK(ResInt, port);

}

ResStr connect_server(int port) {

int valid_port;

TRY(valid_port, ResStr, ResInt, parse_port(port)); // early-returns Err on failure

// work with valid_port...

return OK(ResStr, "Connected");

}

Repo: https://github.com/mainak55512/arstd


r/cprogramming • • 10d ago

What are some neat TUI libraries ?

Thumbnail reddit.com
2 Upvotes

r/cprogramming • • 10d ago

A simple practice(a small terminal hijacker)

Thumbnail
github.com
2 Upvotes

a basic practice, but it works quite well. It's good to attach some vim process to my current terminal. however, reptyr is totally okay though.


r/cprogramming • • 10d ago

how to make a hexdumper in c?

Thumbnail reddit.com
0 Upvotes

r/cprogramming • • 11d ago

forbear: an immediate mode UI library that looks like the web

6 Upvotes

https://github.com/gabrielmfern/forbear

Hello! I've been working on forbear for almost 2 years now, and I've finally come around to implementing a proper C API for it! I would like to share here, and specially ask for some feedback on the API, and on how I wrote the macros that give it the DX I wanted for the library.

This is how it looks like:

void counter(void) {
    fb_s64 *count = FB_STATE(fb_s64);
    FB_ELEMENT(
        .background    = fb_color(fb_rgb(44, 49, 56)),
        .border_radius = 6,
        .padding       = fb_all_sides(8),
    ) {
        char count_text[64];
        snprintf(count_text, sizeof(count_text), "count: %lld", (long long)*count);
        FB_TEXT(count_text);
        if (fb_on_click(FB_MOUSE_BUTTON_LEFT, true, false)) {
            *count += 1;
        }
    }
}

It's a dynamically linked library, with a single forbear.h header. To try it on Linux and macOS:

curl -fsSL https://forbear.dev/quick-start.sh | sh

on Windows:

irm https://www.forbear.dev/quick-start.ps1 | iex

Beware that it's still quite experimental!

The main philosophy is to be what I envision the operating system should have provided us all along: a flexible on design, nice to write, performant, cross-language (in an idiomatic manner) and cross-platform way of making GUI programs.

I've written more about the process of making this C API here for anyone that might like to go more in depth https://www.forbear.dev/log/005-calculator-and-c-api.html


r/cprogramming • • 11d ago

Do you usually use printf or puts?

25 Upvotes

Sorry for the newvie question but Im interested what do you prefer to use between the two?


r/cprogramming • • 11d ago

Starting systems programming on my Fedora with C. Where should I start?

Thumbnail reddit.com
0 Upvotes

r/cprogramming • • 11d ago

Why are LLMs falling back to C? (Or is it?)

0 Upvotes

r/cprogramming • • 11d ago

How should i..?

0 Upvotes

How should i start learning C as my first language? Could anyone share some advice on where to start with C?


r/cprogramming • • 12d ago

New to C

8 Upvotes

I've recently been looking into C and I want to start learning it, mainly because I am interested in how it works as a low-level language after using Python for a while, and eventually plan to take on Gentoo Linux and Linux From Scratch (with prior understanding of C and compilation). I welcome any advice!


r/cprogramming • • 12d ago

Difference to r/C_Programming

8 Upvotes

Hello friends, may I ask what is the difference between this subreddit and r/C_Programming ?

Thanks in advance


r/cprogramming • • 12d ago

At what age did you start programming, and in which language?

Thumbnail reddit.com
0 Upvotes

r/cprogramming • • 13d ago

Reading "Write a Shell in C" (lsh) got me down a rabbit hole: how did the OG UNIX devs actually design shells from scratch?

104 Upvotes

Hey folks,

I'm currently halfway through Stephen Brennan's classic "Write a Shell in C" (lsh) tutorial. It's a fun read so far, though it feels a bit too hand-wavy—it shows how to glue fork(), exec(), and wait() together, and leaves me itching for the actual official specs and deeper design docs.

It got me thinking about two things:

First, how did the OG legends like Ken Thompson or Stephen Bourne actually architect the early UNIX shells from a blank slate? I want to read the real foundational papers, POSIX docs, or historical design breakdowns instead of just toy tutorials.

Second—linking the classic Read -> Parse -> Execute loop to modern AI Agents—it feels remarkably similar to how agents parse context and dispatch tools.

Looking for recommendations on historical specs, papers, or minimal shell source repos. Drop them below!


r/cprogramming • • 13d ago

Signal handlers, how to have them (or the process) behave differently, based on when the process receives the signal

2 Upvotes

Hello, everyone. I'm supposed to be doing a project for school (hence why I can't share the code), but I'll try to describe my problem to the best of my abilities.

My main parent process forks several children, who will execute 2 other programs, depending on the role assigned to them. Once the parent process receives a SIGALRM, it sends a SIGTERM to all its kids, and they, already having changed their signal handlers for SIGTERM, have to do whatever is specified in their signal handler.

Let's consider child A executes program A, and child B executes program B. These 2 processes have to communicate with each other through a series of msgsnd/msgrcv (we can only use System V IPCs in this project, so the msgops don't restart once interruped by a signal). A single process of type B will communicate with several processes of type A (but not with all at the same time: once its communication is finished with one, it goes on to the other). The last msgop A does, is a msgsnd and B does a msgrcv. I'd like to also add that, between the msgrcv and msgsnd on A's part, A is also supposed to go to sleep. A's almost entire program is in a while (it never terminates unless it receives the signal from its parent), whereas B terminates successfully once it's finished communicating with all the necessary A processes.

Whether B receives the SIGTERM signal or not, it has to modify sth in a shared memory section, but what it changes, depends on whether it has finished communicating with all the processes or not.

Ideally, I would like for B to terminate successfully, even if it receives SIGTERM, after having done all it had to do, and I thought of blocking SIGTERM with sigprocmask. That said, couldn't it still receive the signal between the mast msgrcv operation and sigprocmask? Is there any way whatsoever to make a set of instructions atomic?

I considered only having a sig_atomic_t volatile variable in the signal handler, which would increment if the the sign handler is executed, but then, not only would I have to continuously check in case it changed its value, but (and this could be me being wrong, please correct me if that's the case), B could still receive SIGTERM right after the condition of the variable being 0 is checked, and before the following instruction is executed (like a nanosleep - which I'd like to avoid if SIGTERM has been received - OR before a msgrcv, leaving B to wait for a message that will never come, because A could have received the same signal and will not be sending a message).

I thought of having the signal handler check a variable and act differently based on its value, but, again, maybe B receives the signal right before said variable's value changes. I also considered having an if where the conditions would be the variable==0 && msgrv(...)!=-1, but the variable can change after its value has been verified.

Disclaimer: we haven't studied sig_atomic_t volatile variables, so my knowledge might be lacking, although I did try to read up on them and research them. Also, the parent won't be deleting the IPCs until all kids have terminated.

Am I supposed to just hope for the best here and just proceed with sigprocmask? Is there another way I'm not seeing? Am I misunderstanding something? Any help offered is invaluable, so thank you in advance. I'm happy to answer any clarifying questions if I was unclear at all in the post.


r/cprogramming • • 15d ago

why does sizeof an array parameter give the pointer size?

21 Upvotes

i know a function parameter like int a[10] is really adjusted to int *a, so sizeof(a) inside the function gives 8 on my machine instead of 40. what i'm fuzzy on is why the language allows writing the size in the parameter at all if the compiler just throws it away.

is it purely documentation, or does it do anything with something like int a[static 10]? i've seen that form but never seen anyone actually use it.


r/cprogramming • • 14d ago

Looking for 1st yr buddy to build low level systems projects with and share daily progress. anyone interested in memory management and doing c/c++ or rust in welcomed!

Thumbnail reddit.com
1 Upvotes

r/cprogramming • • 15d ago

Best way to get a CRC-48

Thumbnail reddit.com
1 Upvotes