CodeSOD: Duplicate Reports

Date:

Share:

Today's anonymous submitter sends us a short snippet. They found this because they were going through code committed by an expensive third-party contractor, trying to track down a bug: every report in the database kept getting duplicated for some reason.

This code has been in production for over a decade, bugs and all:

if (reportStatuses.indexOf(newStatus > -1))
{
    // add report to database
}

This is server-side JavaScript running in NodeJS. The mistake here is easy to make, it's a simple transposition error. But it's also easy to catch. Any sort of testing at all would find it.

The specific problem, if you haven't spotted it, is where the comparison operator happens: we're passing newStatus > -1 into indexOf as a parameter: this is a boolean value. Now, neither true nor false are in the reportStatuses array, so indexOf returns -1. But -1 is a truthy value, so the condition evaluates to true, adding the report to the database, even if it's already there.

Our submitter writes:

How has no one noticed this? How is the company still in business? How does the world not come down crashing around us more every day?

How is the world not crashing down? Have you looked outside, recently? Tis the season to quote Clark Griswold:

Worse? How could things get any worse? Take a look around here, Ellen. We're at the threshold of hell.


.comment { border: none; }

[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 link

Subscribe to our magazine

━ more like this

Intercom achieves AIUC-1 certification: A new standard for trust in AI Agents

Trust has always been a first principle at Intercom, from our early investments in security and privacy to the globally recognized certifications that shape...

Dior, Chanel and … Veja? The ethical Paris trainer worn by A-listers and royalty | Fashion

In the grand hierarchy of Paris fashion, it’s tricky for a brand to stand out. Especially one whose coup de maître is a goes-with-everything...

Ford Kills the All-Electric F-150 as It Rethinks Its EV Ambitions

Ford is once again shifting its electric vehicle manufacturing plans, a response to a year that’s been tough for the powertrain technology that’s still...

Paseo de las Estrellas de la Ciencia in Santa Cruz de la Palma, Spain

The Walk of the Stars of Science was inaugurated in December of 2020 on the Avenida Marítima in Santa Cruz de la Palma....

5D glass storage ‘memory crystals’ promise up to 13.8 billion years of data storage resilience, which is roughly the age of the universe —...

SPhotonix says it has moved its so-called 5D Memory Crystal technology out of the lab and closer to real-world deployment, outlining plans to pilot...