Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025

      I may have found the ultimate monitor for conferencing and productivity, but it has a few weaknesses

      June 2, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      May report 2025

      June 2, 2025
      Recent

      May report 2025

      June 2, 2025

      Write more reliable JavaScript with optional chaining

      June 2, 2025

      Deploying a Scalable Next.js App on Vercel – A Step-by-Step Guide

      June 2, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025
      Recent

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: On Deep Background

    CodeSOD: On Deep Background

    February 10, 2025

    Andrew worked with Stuart. Stuart was one of those developers who didn’t talk to anyone except to complain about how stupid management was, or how stupid the other developers were. Stuart was also the kind of person who would suddenly go on a tear, write three thousand lines of code in an evening, and then submit an pull request. He wouldn’t respond to PR comments, however, and just wait until management needed the feature merged badly enough that someone said, “just approve it so we can move on.”

    int iDisplayFlags = objectProps.DisplayInfo.BackgroundPrintFlags;
    
    bool bForceBackgroundOn = false;
    bool bForceBackgroundOff = false;
    
    // Can't use _displayTypeID because it will always be 21 since text displays as image
    if (_fileTypeID == 11) // TEXT
    {
        if ((iDisplayFlags & 0x1008) != 0) // Text Background is required
        {
            bForceBackgroundOn = true;
        }
        else if ((iDisplayFlags & 0x1001) != 0) // Text Background is not available
        {
            bForceBackgroundOff = true;
        }
    }
    else if (_displayTypeID == 21) // IMAGE
    {
        if ((iDisplayFlags & 0x1200) != 0) // Image Background is required
        {
            bForceBackgroundOn = true;
        }
        else if ((iDisplayFlags & 0x1040) != 0) // Image Background is not available
        {
            bForceBackgroundOff = true;
        }
    }
    
    bool useBackground = bForceBackgroundOn;
    
    // If an object does not have an Background and we try to use it, bad things happen.
    // So we check to see if we really have an Background, if not we don't want to try and use it
    if (!useBackground && objectProps.DisplayInfo.Background)
    {
        useBackground = Convert.ToBoolean(BackgroundShown);
    }
    
    if (bForceBackgroundOff)
    {
        useBackground = false;
    }
    

    This code is inside of a document viewer application. As you might gather from skimming it, the viewer will display text (as an image) or images (as an image) and may or may not display a background as part of it.

    This code, of course, uses a bunch of magic numbers and bitwise operators, which is always fun. We don’t need any constants. It’s important to note that all the other developers on the project did use enumerations and constants. The values were defined and well organized in the code- Stuart simply chose not to use them.

    You’ll note that there’s some comments and confusion about how we can’t use _displayTypeID because text always displays as an image. I’m going to let Andrew explain this:

    The client this code exists in renders text documents to images (for reasons that aren’t relevant) when presenting them to the user. We have a multitude of filetypes that we do similar actions with, and fileTypes are user configurable. Because of this, we also keep track of the display type. This allows the user to configure a multitude of filetypes, and depending on the display type configured for the file type, we know if we can show it in our viewer. In the case of display type ‘text’ our viewer ultimately renders the text as an image. At some point in time Stuart decided that since the final product of a text document is an image, we should convert display type text over to image when referencing it in code (hence the comment ‘Can’t use display type ID’). If none of this paragraph makes any sense to you, then you’re not alone, because the second someone competent got wind of this, they thankfully nixed the idea and display type text, went back to meaning display type text (aka this goes through OUR TEXT RENDERER).

    What I get from that paragraph is that none of this makes sense, but it’s all Stuart’s fault.

    What makes this special is that the developer is writing code to control a binary status: “do we show a background or not?”, but needs two booleans to handle this case. We have a bForceBackgroundOn and a bForceBackgroundOff.

    So, tracing through, if we’re text and any of the bits 0x1008 are set in iDisplayFlags, we want the background on. Otherwise, if any of the bits 0x1001 are set, we want to force the background off. If it’s an image, we do the same thing, though for 0x1200 and 0x1040 respectively.

    Then, we stuff bForceBackgroundOn into a different variable, useBackground. If that is false and a different property flag is set, we’ll check the value of BackgroundShown– which we choose to convert to boolean which implies that it isn’t a boolean, which raises its own questions, except it actually is a boolean value, and Stuart just didn’t understand how to deal with a nullable boolean. Finally, after all this work, we check the bForceBackgroundOff value, and if that’s true, we set useBackground to false.

    I’ll be frank, none of this quite makes sense to me, and I can certainly imagine a world where the convoluted process of having a “on” and “forceOff” variable actually makes sense, so I’d almost think this code isn’t that bad- except for this little detail, from Andrew:

    The final coup de grace is that all of the twisted logic for determining if the background is needed is completely unnecessary. When the call to retrieve the file to display is made, another method checks to see if the background was requested (useBackground), and performs the same logic check (albeit in a sane manner) as above.

    The code is confusing and unnecessary.

    [Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous Article5 Open-source Local AI Tools for Image Generation I Found Interesting
    Next Article AlsaPlayer – heavily multi-threaded PCM player

    Related Posts

    News & Updates

    The Alters: Release date, mechanics, and everything else you need to know

    June 2, 2025
    News & Updates

    I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-46833 – Apache SimplePythonEncryption RSA Brute Force Decryption Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Tx-DevSecOps – Bridging the Gap Between Security and Speed in DevOps

    Development

    Voicebots in Banking: The New Standard for 24/7 Support

    Web Development

    5 TED Talks by Ethical Hackers that Will Change How You See Cybersecurity

    Development

    Highlights

    CVE-2025-4328 – Spring Cloud Base HTTP Header Handler Open Redirect Vulnerability

    May 6, 2025

    CVE ID : CVE-2025-4328

    Published : May 6, 2025, 7:15 a.m. | 32 minutes ago

    Description : A vulnerability was found in fp2952 spring-cloud-base up to 7f050dc6db9afab82c5ce1d41cd74ed255ec9bfa. It has been declared as problematic. Affected by this vulnerability is the function sendBack of the file /spring-cloud-base-master/auth-center/auth-center-provider/src/main/java/com/peng/auth/provider/config/web/MvcController.java of the component HTTP Header Handler. The manipulation of the argument Referer leads to open redirect. The attack can be launched remotely. The exploit has been disclosed to the public and may be used. This product takes the approach of rolling releases to provide continious delivery. Therefore, version details for affected and updated releases are not available.

    Severity: 3.5 | LOW

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-43554 – Substance3D Modeler – Out-of-Bounds Write Arbitrary Code Execution Vulnerability

    May 13, 2025

    This social media shift could be the opportunity you’ve been waiting for

    February 19, 2025

    LG’s new projector is also a Bluetooth speaker and a mood lamp

    January 5, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.