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

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 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 Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

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

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Buff Reading

    CodeSOD: Buff Reading

    May 21, 2025

    Frank inherited some code that reads URLs from a file, and puts them into a collection. This is a delightfully simple task. What could go wrong?

    static String[]  readFile(String filename) {
        String record = null;
        Vector vURLs = new Vector();
        int recCnt = 0;
    
        try {
            FileReader fr = new FileReader(filename);
            BufferedReader br = new BufferedReader(fr);
    
            record = new String();
    
            while ((record = br.readLine()) != null) {
                vURLs.add(new String(record));
                //System.out.println(recCnt + ": " + vURLs.get(recCnt));
                recCnt++;
            }
        } catch (IOException e) {
            // catch possible io errors from readLine()
            System.out.println("IOException error reading " + filename + " in readURLs()!n");
            e.printStackTrace();
        }
    
        System.out.println("Reading URLs ...n");
    
        int arrCnt = 0;
        String[] sURLs = new String[vURLs.size()];
        Enumeration eURLs = vURLs.elements();
    
        for (Enumeration e = vURLs.elements() ; e.hasMoreElements() ;) {
            sURLs[arrCnt] = (String)e.nextElement();
            System.out.println(arrCnt + ": " + sURLs[arrCnt]);
            arrCnt++;
        }
    
        if (recCnt != arrCnt++) {
            System.out.println("WARNING: The number of URLs in the input file does not match the number of URLs in the array!nn");
        }
    
        return sURLs;
    } // end of readFile()
    

    So, we start by using a FileReader and a BufferedReader, which is the basic pattern any Java tutorial on file handling will tell you to do.

    What I see here is that the developer responsible didn’t fully understand how strings work in Java. They initialize record to a new String() only to immediately discard that reference in their while loop. They also copy the record by doing a new String which is utterly unnecessary.

    As they load the Vector of strings, they also increment a recCount variable, which is superfluous since the collection can tell you how many elements are in it.

    Once the Vector is populated, they need to copy all this data into a String[]. Instead of using the toArray function, which is built in and does that, they iterate across the Vector and put each element into the array.

    As they build the array, they increment an arrCnt variable. Then, they do a check: if (recCnt != arrCnt++). Look at that line. Look at the post-increment on arrCnt, despite never using arrCnt again. Why is that there? Just for fun, apparently. Why is this check even there?

    The only way it’s possible for the counts to not match is if somehow an exception was thrown after vURLs.add(new String(record)); but before recCount++, which doesn’t seem likely. Certainly, if it happens, there’s something worse going on.

    Now, I’m going to be generous and assume that this code predates Java 8- it just looks old. But it’s worth noting that in Java 8, the BufferedReader class got a lines() function which returns a Stream<String> that can be converted directly toArray, making all of this code superfluous, but also, so much of this code is just superfluous anyway.

    Anyway, for a fun game, start making the last use of every variable be a post-increment before it goes out of scope. See how many code reviews you can sneak it through!

    [Advertisement]
    Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to Find and Manage Your IP Address: A Step-by-Step Guide
    Next Article Red Hat presenta Red Hat Enterprise Linux 10 con intelligenza e sicurezza potenziate negli ambienti ibridi

    Related Posts

    News & Updates

    The best CRM software with email marketing in 2025: Expert tested and reviewed

    July 22, 2025
    News & Updates

    This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

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

    CVE-2025-5113 – Diviotec Professional Series Web Interface Command Injection

    Common Vulnerabilities and Exposures (CVEs)

    IPFire 2.29 Core Update 194: Tutte le novità del firewall GNU/Linux con kernel 6.12 LTS

    Linux

    Rilasciato Wine 10.9: Novità e Miglioramenti

    Linux

    CVE-2025-2817 – Mozilla Firefox System File Privilege Escalation

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-28971 – CWD Web Designer Easy Elements Hider Cross-site Scripting Vulnerability

    July 4, 2025

    CVE ID : CVE-2025-28971

    Published : July 4, 2025, 9:15 a.m. | 2 hours, 37 minutes ago

    Description : Improper Neutralization of Input During Web Page Generation (‘Cross-site Scripting’) vulnerability in CWD Web Designer Easy Elements Hider allows Stored XSS. This issue affects Easy Elements Hider: from n/a through 2.0.

    Severity: 5.9 | MEDIUM

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

    Canonical annuncia Ubuntu 24.04 per OrangePi RV2: la nuova frontiera delle SBC RISC-V economiche

    April 28, 2025

    CVE-2025-48415 – Cisco USB Backdoor Command Injection Vulnerability

    May 21, 2025

    Failing well and 3 other ways AI can help you solve your big business problems

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

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