A man may well bring a horse to the water, but he cannot make him drink with he will. -- John Heywood

Wednesday, 25 February 2026

Trying to patch Open Source - a guide for newbies

I decided to try submitting a simple patch to FreeBSD. I did not want to change the world we live in at once, but rather get familiar with a process of submitting a patch to a nice open source project. Let's roll!

What would be a good starting point which aligned with my interests? I had some ideas:

I decided to follow what I think is lowest resistance track: submitting some trivial patches to FreeBSD.

Identifying a problem to solve

Rev is a simple tool which reverses its input. I started by locating the source: /usr/src/usr.bin/rev. Simple as that.

Then I went ahead to read lines from rev.c and identify places, where fix might be suitable. I ran static analyzer by

make clean
make CFLAGS="-Weverything"
make

This output gave me:

/usr/src/usr.bin/rev/rev.c:86:7: note: Access to field '_flags' results in a dereference of a null pointer (loaded from variable 'fp') 86 | if (ferror(fp)) { | ^ ~~

Looks like we have something fixable. I believe that clang thinks that at some point in the program fp might be null (hint: null pointer dereference), so let's check if fp really becomes null.

Solution

Let's have a look at what we can see in rev.c and let's add null pointer check, like in the following listing:

index 4bb113d4b532..b4a0ef2791ab 100644
--- a/usr.bin/rev/rev.c
+++ b/usr.bin/rev/rev.c
@@ -83,7 +83,7 @@ main(int argc, char *argv[])
                                putwchar(*t);
                        putwchar('\n');
                }
-               if (ferror(fp)) {
+               if (fp != NULL && ferror(fp)) {
                        warn("%s", filename);
                        clearerr(fp);
                        rval = 1;

Then I made the solution by make clean && make CFLAGS="-Weverything". The warning is gone. Let's see if the FreeBSD community looks nice at this patch.

Submitting a patch

I created an account on [reviews.freebsd.org]. After confirming e-mail message, I followed the form for creating a differential, which is how merge requests are called in FreeBSD. Then I wait...