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

      Representative Line: Brace Yourself

      September 18, 2025

      Beyond the Pilot: A Playbook for Enterprise-Scale Agentic AI

      September 18, 2025

      GitHub launches MCP Registry to provide central location for trusted servers

      September 18, 2025

      MongoDB brings Search and Vector Search to self-managed versions of database

      September 18, 2025

      Distribution Release: Security Onion 2.4.180

      September 18, 2025

      Distribution Release: Omarchy 3.0.1

      September 17, 2025

      Distribution Release: Mauna Linux 25

      September 16, 2025

      Distribution Release: SparkyLinux 2025.09

      September 16, 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

      AI Momentum and Perficient’s Inclusion in Analyst Reports – Highlights From 2025 So Far

      September 18, 2025
      Recent

      AI Momentum and Perficient’s Inclusion in Analyst Reports – Highlights From 2025 So Far

      September 18, 2025

      Shopping Portal using Python Django & MySQL

      September 17, 2025

      Perficient Earns Adobe’s Real-time CDP Specialization

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

      Valve Survey Reveals Slight Retreat in Steam-on-Linux Share

      September 18, 2025
      Recent

      Valve Survey Reveals Slight Retreat in Steam-on-Linux Share

      September 18, 2025

      Review: Elecrow’s All-in-one Starter Kit for Pico 2

      September 18, 2025

      FOSS Weekly #25.38: GNOME 49 Release, KDE Drama, sudo vs sudo-rs, Local AI on Android and More Linux Stuff

      September 18, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Going on a teDa

    CodeSOD: Going on a teDa

    July 30, 2025

    Carlos G found some C++ that caused him psychic harm, and wanted to know how it ended up that way. So he combed through the history. Let’s retrace the path with him.

    Here was the original code:

    <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">parseExpiryDate</span> <span class="hljs-params">(<span class="hljs-type">const</span> <span class="hljs-type">char</span>* expiryDate)</span>
    </span>{
        <span class="hljs-comment">// expiryDate is in "YYMM" format</span>
        <span class="hljs-type">int</span> year, month;
        <span class="hljs-built_in">sscanf</span>(expiryDate, <span class="hljs-string">"%2d%2d"</span>, &year, &month);
    	
        <span class="hljs-comment">//...</span>
    }
    

    This code takes a string containing an expiry date, and parses it out. The sscanf function is given a format string describing two, two digit integers, and it stores those values into the year and month variables.

    But oops! The expiry date is actually in a MMYY format. How on earth could we possibly fix this? It can’t be as simple as just swapping the year and month variables in the sscanf call, can it? (It is.) No, it couldn’t be that easy. (It is.) I can’t imagine how we would solve this problem. (Just swap them!)

    <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">parseExpiryDate</span><span class="hljs-params">(<span class="hljs-type">const</span> <span class="hljs-type">char</span>* expiryDate)</span>
    </span>{
        <span class="hljs-comment">// expiryDate is in "YYMM" format but, in some part of the code, it is formatted to "MMYY"</span>
        <span class="hljs-type">int</span> year, month;	 
        <span class="hljs-type">char</span> correctFormat[<span class="hljs-number">5</span>];
    
        correctFormat[<span class="hljs-number">0</span>] = expiryDate[<span class="hljs-number">2</span>];
        correctFormat[<span class="hljs-number">1</span>] = expiryDate[<span class="hljs-number">3</span>];
        correctFormat[<span class="hljs-number">2</span>] = expiryDate[<span class="hljs-number">0</span>];
        correctFormat[<span class="hljs-number">3</span>] = expiryDate[<span class="hljs-number">1</span>];
        correctFormat[<span class="hljs-number">4</span>] = <span class="hljs-string">''</span>;
        <span class="hljs-built_in">sscanf</span>(correctFormat, <span class="hljs-string">"%2d%2d"</span>, &year, &month);
    
        <span class="hljs-comment">//...</span>
    }
    

    There we go! That was easy! We just go, character by character, and shift the order around and copy it to a new string, so that we format it in YYMM.

    The comment here is a wonderful attempt at CYA. By the time this function is called, the input is in MMYY, so that’s the relevant piece of information to have in the comment. But the developer really truly believed that YYMM was the original input, and thus shifts blame for the original version of this function to “some part of the code” which is shifting the format around on them, thus justifying… this trainwreck.

    Carlos replaced it with:

    <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">parseExpiryDate</span> <span class="hljs-params">(<span class="hljs-type">const</span> <span class="hljs-type">char</span>* expiryDate)</span>
    </span>{
        <span class="hljs-comment">// expiryDate is in "MMYY" format</span>
        <span class="hljs-type">int</span> month, year;
        <span class="hljs-built_in">sscanf</span>(expiryDate, <span class="hljs-string">"%2d%2d"</span>, &month, &year);
    	
        <span class="hljs-comment">//...</span>
    }
    

    [Advertisement]
    Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticlePerformance Analysis with Laravel’s Measurement Tools
    Next Article RSLint – fast, customizable, and easy to use JavaScript and TypeScript linter

    Related Posts

    News & Updates

    Distribution Release: Security Onion 2.4.180

    September 18, 2025
    News & Updates

    Distribution Release: Omarchy 3.0.1

    September 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    He Hacked Servers, Not People — But Still Left a $4.5 Million Mess Behind

    Development

    Microsoft AI Released Phi-4-Reasoning: A 14B Parameter Open-Weight Reasoning Model that Achieves Strong Performance on Complex Reasoning Tasks

    Machine Learning

    UNC1151 Exploits Roundcube Flaw in Spear Phishing Attack

    Security

    xxHash – non-cryptographic hash algorithm

    Linux

    Highlights

    CVE-2025-4787 – SourceCodester Oretnom23 Stock Management System SQL Injection Vulnerability

    May 16, 2025

    CVE ID : CVE-2025-4787

    Published : May 16, 2025, 4:15 p.m. | 47 minutes ago

    Description : A vulnerability classified as critical has been found in SourceCodester/oretnom23 Stock Management System 1.0. Affected is an unknown function of the file /admin/?page=sales/view_sale. The manipulation of the argument ID leads to sql injection. It is possible to launch the attack remotely. The exploit has been disclosed to the public and may be used.

    Severity: 6.3 | MEDIUM

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

    Recipya – simple and powerful recipe manager

    August 2, 2025

    CVE-2025-5873 – eCharge Hardy Barth Salia Web UI Unrestricted File Upload Vulnerability

    June 9, 2025

    Microsoft makes limited edition Surface Slim Pen in vibrant colors, but you can’t buy one

    May 20, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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