Article 6DFTT CodeSOD: All Thumbs

CodeSOD: All Thumbs

by
Remy Porter
from The Daily WTF on (#6DFTT)

Nick L supports a hardware system that should autodetect a USB drive when inserted, and then load a config file off of it. This fairly simple task is made more complicated by the freelancer they hired to solve the problem, who has some... interesting approaches to writing Python.

thumb_name = []while thumb_name == []: sleep(1) thumb_name = os.listdir("/media/update") ser.write(bytes("usb drv not detected\r",'UTF-8')) print ("test") print (thumb_name) print ("test")thumb_name_pure = thumb_name[0]

This code's attempt to confirm whether or not the USB drive has been inserted is to attempt to list the directories at the mount point. The result of this is stored in a variable named thumb_name, which importantly, isn't the name of the drive, but is the list of files in the folder. This whole thing is gated in a while loop that requires thumb_name to actually hold entries, so I hope that the drive eventually mounts, and that it holds at least one file in that directory, otherwise this is going to sit and wait for a loooooong time.

Then we get thumb_name_pure set, which is just the first entry in the directory. Let's see how that's used:

buf10 = "False"while buf10 == 'False': buf10 = (str(path.exists("/media/update/" + thumb_name_pure + "/data/cfg.txt"))) sleep(1) #ser.write(bytes("cfg.txt not detected\r",'UTF-8'))

So, thumb_name_pure is inserted into a path, where it will search for /data/cfg.txt. Which this all is a long, roundabout way of saying "the first directory in the drive had better contain a /data/cfg.txt file, because otherwise you're going to loop forever.

But that's not even the WTF here, as WTF as that is. buf10 (I'm not even sure what's happening with that variable name, but I assume there is a buf1 and a buf9) stores the string "False". And then we cast path.exists to a string, because why on Earth would we use boolean literals when we can make everything stringly typed instead.

Nick adds:

Also, elsewhere in the code, False is misspelled as "fasle". Yes, it causes bugs.

buildmaster-icon.png [Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!
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