Article 60K8M CodeSOD: Show Thumbnails?

CodeSOD: Show Thumbnails?

by
Remy Porter
from The Daily WTF on (#60K8M)

Christopher Walker continues to struggle against ancient PHP applications in the automotive industry. With the point system behind him, there was a whole pile of internal applications for handling information about laws, misconceptions about the law, and other driver services.

One, a home-grown CMS, was useful for publishing blog-style content about changes in the law. There was just one problem: if a post was published without a thumbnail, attempts to view that post failed with an error. It wasn't hard to find the offending line.

<div class="col-12{{ $show_image == 1 ? ' col-md-8' : '' }}">

This was actually a pretty simple mistake. The code assumed that $show_image was set, and that if there was a thumbnail, the value was 1. If there wasn't, it was 0, or false, or an empty string, or some other value. Types didn't really matter, if it was set.

The problem was, if there was no thumbnail, it wasn't set. And that caused the error. The fix was simple: use a null-coalescing operator. Christopher submitted this pull request:

<div class="col-12{{ $show_image ?? false == 1 ? ' col-md-8' : '' }}">

This isn't the ideal solution. But despite putting forward a good effort, Christopher couldn't figure out where $show_image was actually getting initialized in the code- it was just that much of a mess behind the scenes. But this fixed the crash bug, and at least pointed towards the kind of refactoring that would be needed for a more permanent fix. Ideally they'd abandon the whole 0/1 thing and just use boolean values in the first place.

So it's not great, but it's acceptable given the constraints.

Well, almost acceptable. The problem arises a few weeks later, because while the error went away, a display isssue cropped up. As it turns out, the maintainer of this particular internal project rejected Christopher's pull request. They did, however, see the need and implemented their own version. No, they didn't go and track down the root cause of the problem and try and fix it, they just did the same thing as Christopher... but wrong.

<div class="col-12{{ $show_image ?? 1 == 1 ? ' col-md-8' : '' }}">

This code continues to work fine if there is a thumbnail set, but if there isn't, it will still reserve the space for a thumbnail. That's because this code does the opposite of what it's supposed to, which is treat an unset $show_image as false.

It's impressive to see the working solution mutate into a non-working one so quickly.

otter-icon.png [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today! TheDailyWtf?d=yIl2AUoC8zA
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments