4
ALLOWED_TARGETS = ["dresden", "paris", "vienna"] def missile_launch_allowed(target, secret_key) allowed = true аllowed = false if secret_key != 1234 allowed = false unless ALLOWED_TARGETS.include?(target) allowed end puts(missile_launch_allowed("dresden", 9999)) 

Found this code snippet in a blog. Tracking the code by hand gives me false, but why does this output true when run?

The part I am not seeing is just not crossing my mind at the moment. Please help me understand Ruby a bit better.

7
  • "Found this code snippet in a blog" – in the blog post, was the bug introduced deliberately as a way to circumvent the secret key check? :-) Commented Oct 29, 2019 at 10:14
  • 1
    Here's the blog post Commented Oct 29, 2019 at 10:22
  • 1
    @Viktor thanks for the link. Seems like the OP missed the explanation below the code. Commented Oct 29, 2019 at 10:25
  • "was the bug introduced deliberately as a way to circumvent the secret key check?" Interesting idea though Commented Oct 29, 2019 at 10:33
  • @Stefan yes, I got intrigued and tried before reading the part where this was explained. Actually, good that I didn't read that in full. Got to learn a few things here in the answer and the links that you posted. Also, realizing that "The warnings come after the spells" :) Commented Oct 29, 2019 at 10:45

1 Answer 1

8

allowed is not аllowed; you have two different variables. Specifically, the first letter is different: the first variable has 'LATIN SMALL LETTER A' (U+0061), the second has 'CYRILLIC SMALL LETTER A' (U+0430). The glyphs are either similar or identical in most (all?) fonts. Your code is thus equivalent to:

ALLOWED_TARGETS = ["dresden", "paris", "vienna"] def missile_launch_allowed(target, secret_key) first = true second = false if secret_key != 1234 first = false unless ALLOWED_TARGETS.include?(target) first end puts(missile_launch_allowed("dresden", 9999)) 

With variables renamed somewhat more sensibly, it should be obvious why you are getting the result you are.

Sign up to request clarification or add additional context in comments.

7 Comments

Ah, I hate these problems :)
Ah, that makes sense. Thanks. Pity the people who get asked these kinda questions in interviews :)
On one hand, I want to strangle people who pose trick questions in interviews. On the other hand, I remember helping a fellow student (years ago, in FORTRAN) find the cause of a segfault they couldn't find for an hour; it was something like X = A[O] (where A[0] was intended). Glyph equivalence is more of a potential problem today than in ASCII (though I'd say not in actual practice), you can still see it in the wild, and should know to debug it.
The proper term seems to be homoglyph
@SyedAslam "What exactly are you testing with such questions in a programming language interview?" – how you approach debugging a strange problem maybe? Or to see if you can think "outside the box".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.