6 ms·
Ignoring the tasteless act of trying to use the slack at the end of an allocation for your side projects, the bug really highlights the dangers of illiterate in
by jeffbee 5y ago
Ignoring the tasteless act of trying to use the slack at the end of an allocation for your side projects, the bug really highlights the dangers of illiterate interfaces, to wit Color::Color(u8 r, u8 g, u8 b) from Serenity LibGfx. Calling this incorrectly is incredibly easy to do and invisible to the reader at the call site. Authors need to work with the type system to avoid these bugs, in this case having distinct types for red, green, and blue so the compiler will stop you from assigning red to blue without an explicit conversion. "Simple confusion of RGB vs. BGR" is not something that should seem natural to programmers. It's an error caused by underuse of the type system, just treating everything as a bag of numbers.
- dvt 5y ago> "Simple confusion of RGB vs. BGR" is not something that should seem natural to programmers. These kinds of errors are extremely common in much more mature libraries, including OpenCV, as well as HLSL or GLSL. No disrespect, but it sounds like you've never really touched graphics programming before. Color spaces are literally just a bag of numbers. A good analogy is coordinate systems. There's no type difference between an x-coordinate or a y-coordinate; they're just numbers and are defined by their ordinal position in an (x, y) tuple.
- owl57 5y agoAlso, even if we model the color space in some overly clever way, we are talking about a JPEG loader here. Its input is inevitably a bag of numbers, and a horribly underspecified one.
- owl57 5y agoI felt like I forgot to add a link. Why don't we see images in wrong color space nearly as often as text with wrong character encoding, remains a mystery to me since the moment I was writing a toy JPEG decoder and found this article: https://entropymine.wordpress.com/2018/10/22/how-is-a-jpeg-images-color-type-determined/ https://entropymine.wordpress.com/2018/10/22/how-is-a-jpeg-i...
- owl57 5y agoProbably, a large part of the answer is that you can walk away from these tables in horror and simply assume YCbCr or grayscale, and still show most files in the wild correctly. Serenity seems to do just that. For text, UTF-8 is still not quite as dominant.
- jeffbee 5y agoSee, you've confused age with maturity. A library that is old and still broken and hard to use isn't mature, it's just old. A mature library would be one that has learned something from the history of software development. foo(5, 42, 99) is just a bad interface. foo(red(5), green(42), blue(99)) gives the compiler enough to whine about.
- angry_octet 5y agoMost programmers haven't experienced the power of strict type checking.
- jcelerier 5y ago> There's no type difference between an x-coordinate or a y-coordinate no human unit difference does not imply no type difference. Types are tools to write correct programs, we should use them at the maximum !
- MaulingMonkey 5y agoTo add to this, libraries aren't consistent about convention either. Take the common "RGBA". Is this describing the byte order? Or describing most-significant to least-significant 0xRRGGBBAA, which might be little-endian encoded into a "ABGR" byte order? If you think that's stupid idea and that I'm an idiot for suggesting it, what about 16-bit RGB formats where the G component straddles both bytes? Okay, so those aren't so common anymore - but what about DXT/BC compressed formats which encode 4x4 pixel chunks at a time, and don't cleanly decompose into "channels" in the first place? D3DFMT_A8R8G8B8 (Direct3D 9) is equivalent to DXGI_FORMAT_B8G8R8A8_UNORM (DXGI / Direct3D 10+). Note that one enumeration lists the components in reverse order of the other, and that this is correct! https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-legacy-formats https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3... . Documentation often completely omits information endian or encoding - and when it doesn't, it's often hidden away where you'll never find it, and usually assumes x86/x64/little-endian processors. The behavior on big-endian machines is best found out through testing - even if the documentation is clear, the documentation stands a good chances of lying, and CI probably doesn't test on big-endian meaning bugs have likely arisen, and there's a good chance your copy of the library is old and doesn't contain any bugfixes. In light of all of the above, RGB vs BGR confusion is one of the most natural points of confusion to run across when dealing with image formats. "Just use the type system!" ignores where these bugs crop up - (de)serialization, converting streams of bytes to types or vicea versa. Someone must write the code, declaration, whatever - and the type system has no means of ensuring that correctly matches whatever not-computer-readable spec that the (de)serialization is supposed to match - and so it will, frequently, be understandably incorrect.
- jeffbee 5y agoOk, but bring it back to this article. It is marshaling typed struct fields to typed parameters, but because the types are all primitive, the author entered them backwards and the language platform didn’t notice. The type system can prevent this flaw.
- MaulingMonkey 5y ago> the author entered them backwards You're mistaken. The author originally suspected - or hypothesized - that was possibly the bug, but the actual cause detailed in the post ended up being different (nondeterministic hashmap iteration over components) and the final patch does not include the initially proposed "fix" of swapping the red and blue components as was shown towards the very top of the article. The original order was correct. Swapping red/blue would've just been canceling out one bug by adding another. https://github.com/SerenityOS/serenity/commit/a10ad24c760bfe713f1493e49dff7da16d14bf39 https://github.com/SerenityOS/serenity/commit/a10ad24c760bfe... > The type system can prevent this flaw. The type system can add some roadbumps to encourage centralizing the conversion of "untyped" disk bytes to typed r/g/b components or a typed color component, but that conversion must still occur somewhere, and it can be written backwards at that somewhere, and one of those somewheres will be inside JPGLoader.cpp. That's the fundamental job of a JPEG loader - conversion of an untyped byte stream into useful types for the rest of the program to use. Additional use of the type system might limit where within JPGLoader.cpp such a component swapping flaw might be likely, but it's never going to prevent it for all of JPGLoader.cpp. The existence of the Color type already makes such a flaw fairly unlikely outside of JPGLoader.cpp and similar conversion points. That's sufficient use of the type system. More within JPGLoader.cpp, while possible, would be overkill based on my own experience with color conversion and swapped component bugs. Hell, to play devil's adovcate, further use of the type system could cause such flaws! The extra code is likely to cause additional reviewer fatigue in code reviews. Reviewer fatigue causes inattention. Inattention causes the reviewer to miss swapped or nondeterministics components. Perhaps such inattention caused this bug! I would be in favor of replacing: const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] }; with: const auto color = Color::from_rgb((u8)block.r[pixel_index], (u8)block.g[pixel_index], (u8)block.b[pixel_index]); To make it clearer that this code is correct as is. Doesn't add any new types though, just names a constructor in such a way as to imply an argument order, and uses the existing r/g/b union aliases of y/cb/cr in macroblocks (r/g/b is more correct at that point in the code, since ycbcr_to_rgb should've been called already - it's possible this code was written before those union aliases were added?) It's still possible to have a copy+paste bug with my replacement by using r/g/g as input instead of r/g/b or similar, but I've seen such bugs even with per-component types - just need the first copy to be mistaken and copy+pasted all over.
- signa11 5y agomay you please suggest an api or an interface that makes it hard or almost impossible to get ordering wrong ? and one which probably is succinct as well ?
- Gibbon1 5y agoYou can use named parameters. https://pdimov.github.io/blog/2020/09/07/named-parameters-in-c20/ https://pdimov.github.io/blog/2020/09/07/named-parameters-in...
- deleted 5y ago[deleted]
- signa11 5y agoyes ! named parameters are an option. it would be quite cool if c++ did have c99's equivalent of designated initializers, and without the restrictions of 1. following decl-order while doing the initializations and 2. omitting unwanted or unneeded parameter values probably, to some extent, 1 follows from 2 ?
- Gibbon1 5y agoLast time I looked a few years ago there was some issue with class instantiation and the order of initialization that was holding them back from implementing designated initializers. Can't remember the details.
- dodobirdlord 5y agoIn Rust this is a common pattern because single-element structs are unwrapped by the compiler, but only after typechecking. So they can be easily leveraged to prevent type confusion between differently typed values with compatible underlying byte representation. https://doc.rust-lang.org/rust-by-example/generics/new_types.html https://doc.rust-lang.org/rust-by-example/generics/new_types... So this would look something like: // single field tuple structs struct R(u8); struct G(u8); struct B(u8); // using a tuple struct struct Color(R, G, B); // or using a struct with named fields struct Color { r: R, g: G, b: B, } I assume something similar can be done in C with no loss of performance under most compilers, since unwrapping a single-field struct is an optimization that a compiler should pick up on every time.
- Const-me 5y agoParsers, decoders and decompressors consume streams of bytes. The bytes from these streams come with no type info. Type safety happens on higher levels, once these byte streams are already parsed into something else, at least partially so.
- jeffbee 5y agoThat has nothing to do with the article, where the bug is in assignment from struct fields to function parameters, all typed.
- masklinn 5y ago> Ignoring the tasteless act of trying to use the slack at the end of an allocation for your side projects It’s not for side-projects though? And it seems like a good idea to avoid reallocations if you can still grow in-place.