$ cat userwelcome.c
/*
A simple program to demonstrate how unsafe C is if you are a careless programmer,
This is just an example and I dont expect anyone to make mistakes like this.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct machine {
char username[12];
char password[12];
};
int main() {
struct machine *home = malloc(sizeof(struct machine));
scanf("%s", home->username);
/* assume this program is part of PID 1, which is started as a root process
and assume the password was opened from a root-only file /etc/passwd
and from there we strcpy the password into home->password */
strcpy(home->password, "1337");
printf("hello %s\n", home->username);
}
$ gcc userwelcome.c -Wall -pedantic -std=c11 -Werror -O3
$ ./a.out
kalekalethegreatest
hello kalekaletheg1337
$ cat notes.txt
These small silly mistakes have caused many a CVE, and it is mostly due the shotgun
that the compiler gives to the programmer. Every programmer, smart or not, should
consider using a safer language for their usecase, is what I believe.
The following program demonstrates a buffer overflow. Anyone who knows how to program in C can tell you what exactly is wrong in this program,
and will also defend the C programming language shifting the blame on the programmer. The C language is about as close as you can get to machine while also enjoying the
high level programming syntax. I would aruge this ability to shoot yourself on the foot is a feature of the C language. Programming in C can be very fun but it should very much be limited to what it does the best, machine level programming. Using a language like C for high level purpose is a bad idea for one single reason and that being userspace level programmer really do not understand how the program is being operated at the lower level and nor they should. It should not be the duty of a game developer to bother himself with the nuances of low level programming.
So am I going to shill rust? not really. But yes, I would love to see Rust replace C as it really does solve alot of the problems. The difficulty curve is also good! it keeps away potentally stupid programmers from making low quality programs. However in case of rust stupid programmers are already making low quality grabage
due to the hype around the language. But I assume slowly the hype will fade away and rust will find its place in systems and performance critical spaces only.
I will not be shilling rust, but instead of it I will be shilling some better alternatives for userland programs, which are fast, secure and easy to program in.
- Ada langauge
- Go language
- Nim language
These languages are some of the most fun and easy languages I have ever used, but I never see people talk about it.
Nim and Go are relatively new and really dont have much to offer, Go is like C but safe and slow, Nim is like a mix of Python and Ada which runs about as
fast as C, as the language itself is a transpiler to C.
Ada stands out among these as Ada was designed to be safe and fast. The syntax is pretty easy as comfy. It can also be easily used alongside C or any thing that
follows the C ABI. I want to see more people try this language instead of dickriding the Rust hype, even 30 years ago people knew C was not a safe and reliable language, which is why the USA's DOD came up with this language.