Go ReadString(..) bug on release but works on debug

Image for post
Image for post
Photo by: Bart Christiaanse @bartchristiaanse (unsplash.com)

Did you ever encounter an annoying bug that was exist only on release but when you debugged it, it worked fine? It happened to me today and I thought you can learn something new from this small issue.

While working on a CLI tool in Go, I needed to prompt an interactive question to the user, receive the input and check if it is “y” (yes) or “n” (no), sounds simple right? Because it supposed to be!

But every time I typed y” and pressed Enter, the condition that checked if I received “y”, was . Here is the code, see if you can find it by yourself (you already have a clue) before I will write what is the problem:

In debug it works!

I thought I can find the problem simply by debugging it. I started the debugger, going step by step. The value of is “y” and I don’t see any problem with this. No white spaces, only “y”. Stepping to the condition, and... it enters the condition with the “y”!

Image for post
Image for post

Wait wait wait, I want to understand. If I run the program without debug mode, it doesn’t match the “y” I typed. But is it IDE issue or debug vs release issue? It seems that this is related to the IDE I am using which is GoLand.

When I run the program with GoLand in release mode, it also works fine. So running with the GoLand IDE is OK, but still, when I run the program from the command line it behaves differently, it doesn’t enter the condition of “y”.

Debugging with old-style

This is the time of using the old-style debugging, printing on release mode like good old days :)

My guess is that that variable contains some other characters. This is the only thing that can cause it and make sense. The simple thing I did is to print the with this row:

After I printed it, something weird happened:

Image for post
Image for post
“text” is missing (without IDE)

It didn’t print “text: “ nor the variable . When I ran it with GoLand, it does:

Image for post
Image for post

OK, maybe there are some hidden characters. Time to pull out the big guns, I added the killer rows:

Let’s see what is going on:

Image for post
Image for post

Well well well, as I remember from my ASCII training, 121 is “y” and “13” is … (drums effect): CR carriage return.

When running the same compiled program with the IDE, it seems that GoLand removed the carriage return automatically:

Image for post
Image for post
Run with GoLand

The fix is simple, changing the following row by adding “\r”:

Summary

I think most of the people usually don’t think about the IDE as one that can cause issues. It runs the program like we will run it without it. But this small bug shows the there are differences and this is why you need to take it into consideration. Next time, it might be more tricky bug and you want to make sure that the IDE is not “sabotaging“ your code.

Security researcher interested in reversing, solving CTFs, malware analysis, penetration testing and DevOps security (docker and Kubernetes)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store