Ask coding questions

← Back to all posts
Coalescing if statements leads to change in control flow?
xxpertHacker (864)

On line 228 of base64.hpp, I had this:

if ( '=' == ascii[i] ) {
	if ( '=' != ascii[1 + i] ) {
		return false;
	}
}

I looked at this, and thought that I could just use an && operator, like so:

if ( '=' == ascii[i] && '=' != ascii[1 + i] ) {
	return false;
}

Then my tests crashed and the program aborted...

How are these not the same?

if x {
    if y {
        return false;
    }
}
// vs
if x && y {
    return false;
}
Comments
hotnewtop
xxpertHacker (864)

@DynamicSquid Umm... so, I never figured this out, and I think it happened without optimization applied too, do you think you could take a look?

It's mostly pointer arithmetic, comparisons, and memory writing/reading; very simple C-like code.

DynamicSquid (4619)

@xxpertHacker I don't have much time right now, but I'll take a look at it again in a couple hours.

Coder100 (16757)

are you SURE that's what is causing the tests to fail?

They are the same thing

xxpertHacker (864)

@Coder100 I swear, I JUST tried it to make sure; try it yourself: https://repl.it/@xxpertHacker/base64#base64.hpp lines 228-231.

You'll get std::bad_optional_access thrown at that point, that just means that the function believes that the base64 was invalid, since you'd be breaking the base64 integrity checking function.

xxpertHacker (864)

@Coder100 Did you ever try it yourself?

Baconman321 (1048)

This happens to me in JavaScript too.

I think it would mainly be something logical..

Hmmm... (thinking). I'll edit it when I find an answer (or don't find an answer).

Baconman321 (1048)

@xxpertHacker Yeah I don't think I found an answer :(