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

      Upwork Freelancers vs Dedicated React.js Teams: What’s Better for Your Project in 2025?

      August 1, 2025

      Is Agile dead in the age of AI?

      August 1, 2025

      Top 15 Enterprise Use Cases That Justify Hiring Node.js Developers in 2025

      July 31, 2025

      The Core Model: Start FROM The Answer, Not WITH The Solution

      July 31, 2025

      Error’d: Monkey Business

      August 1, 2025

      Not just YouTube: Google is using AI to guess your age based on your activity – everywhere

      July 31, 2025

      Malicious extensions can use ChatGPT to steal your personal data – here’s how

      July 31, 2025

      What Zuckerberg’s ‘personal superintelligence’ sales pitch leaves out

      July 31, 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

      The details of TC39’s last meeting

      August 1, 2025
      Recent

      The details of TC39’s last meeting

      August 1, 2025

      Jumbo-sized JavaScript for issue 747

      August 1, 2025

      How to install IoT platform — Total.js

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

      Rilasciata 4MLinux 49: Distribuzione GNU/Linux Leggera e Versatile

      August 1, 2025
      Recent

      Rilasciata 4MLinux 49: Distribuzione GNU/Linux Leggera e Versatile

      August 1, 2025

      US Tariff Change Could Send SBC & Mini PC Prices Soaring

      August 1, 2025

      Hyprland lancia Hyprperks: Abbonamento a pagamento per vantaggi esclusivi

      August 1, 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:

    void parseExpiryDate (const char* expiryDate)
    {
        // expiryDate is in "YYMM" format
        int year, month;
        sscanf(expiryDate, "%2d%2d", &year, &month);
    	
        //...
    }
    

    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!)

    void parseExpiryDate(const char* expiryDate)
    {
        // expiryDate is in "YYMM" format but, in some part of the code, it is formatted to "MMYY"
        int year, month;	 
        char correctFormat[5];
    
        correctFormat[0] = expiryDate[2];
        correctFormat[1] = expiryDate[3];
        correctFormat[2] = expiryDate[0];
        correctFormat[3] = expiryDate[1];
        correctFormat[4] = '';
        sscanf(correctFormat, "%2d%2d", &year, &month);
    
        //...
    }
    

    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:

    void parseExpiryDate (const char* expiryDate)
    {
        // expiryDate is in "MMYY" format
        int month, year;
        sscanf(expiryDate, "%2d%2d", &month, &year);
    	
        //...
    }
    

    [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

    Error’d: Monkey Business

    August 1, 2025
    News & Updates

    Not just YouTube: Google is using AI to guess your age based on your activity – everywhere

    July 31, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Google’s new Search mode puts classic results back on top – how to access it

    News & Updates

    Japachar – learn Japanese characters

    Linux

    The Wi-Fi Forest – Horror Audiobook by Srinidhi Ranganathan

    Artificial Intelligence

    Minecraft is reviving a dead Mob Vote loser and actually making copper useful in 2025’s third update

    News & Updates

    Highlights

    News & Updates

    Gamers continue to make the switch to Windows 11 — and not just from Windows 10, either

    July 3, 2025

    Steam’s latest monthly hardware survey is here and it shows that gamers continue to embrace…

    CVE-2025-4813 – PHPGurukul Human Metapneumovirus Testing Management System SQL Injection Vulnerability

    May 16, 2025

    The End of Nvidia’s Dominance? Huawei’s New AI Chip Could Be a Game-Changer

    April 29, 2025

    Marvel Rivals’ swimsuit lineup kicks off this week — with hot new outfits for these characters

    July 15, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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