Get the latest tech news

Malloc broke Serenity's JPGLoader, or: how to win the lottery (2021)


I got the chance to investigate an interesting bug in SerenityOS this week. It was related to the decoding of JPG images in the operating system. For some reason, when a JPG image is viewed, it comes out like this: Lenna, showing up with incorrect colors. Weird, huh? Also seems like a simple confusion of RGB vs. BGR. And sure enough, making the following change on JPGLoader.cpp: - const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] }; + const Color color { (u8)block.cr[pixel_index], (u8)block.cb[pixel_index], (u8)block.y[pixel_index] }; context.bitmap->set_pixel(x, y, color); makes the image show up correctly. Case closed! …not. Why did this even break in the first place?

SerenityOS, being an operating system project that focuses on doing its own thing, also has its own standard library called AK (which stands for Agnostic Kit). This library is analogous to C++’s STL, but is more readable due to not having to support a myriad of different operating systems and not having to contort oneself to conform to hideous coding standards. As a result, during the 1000 commits I ended up bisecting for, I had to build SerenityOS from scratch about 4-5 times on a 2011 laptop with Sandy Bridge Mobile.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Serenity

Serenity

Photo of Malloc

Malloc

Photo of lottery

lottery

Related news:

News photo

Serenity takes a full-home radar approach to fall detection