November 11, 2018

Scanning unsigned types in C

printf and scanf are the C standard library functions for printing out and reading in data. They have string counterparts, prefixed with s, which work on strings (sprintf and sscanf). All these functions use a format string to match parts of the string with C variables. As an example, %lu in the format string corresponds to an unsigned long, a positive integer type in C. Scanning unsigned types What happens if you try to sscanf a negative number using %lu? Read more

October 20, 2018

Unknown type name 'u_int'

I ran across the error message (Unknown type name 'u_int', 'u_long', 'u_char' etc.) when trying to incorporate some old C code into a more modern project. It shows up in system headers, like sys/attr.h, and I was confused. After quite some time I figured out what the problem was, so I thought I’d share. Simplified example The source code, when simplified, looked something like this: $ cat test.c #define _XOPEN_SOURCE 500 #include <sys/attr. Read more

October 6, 2018

AddressSanitizer (ASAN) examples

AddressSanitizer is a great tool for finding bugs in C or C++ projects. It consists of some instrumentation added to code at compile time, as well as a dynamically linked runtime. Good test coverage is required, as it detects problems at runtime, while running your test-suite. To use it, supply -fsanitize=address to both the compiler and linker when building for tests (works in newer versions of clang and gcc). There are great resources for understanding how it works, but when I started, I was missing a few simple examples. Read more

© Ole Herman Schumacher Elgesem 2021

Powered by Hugo & Kiss.