-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package secrethandshake | ||
|
||
import ( | ||
"errors" | ||
"unsafe" // ☢ ☣ | ||
) | ||
|
||
// the idea here is that the memory backed by bool | ||
// will be on byte and no other values than 0x00 and 0x01 | ||
func castBoolToInt(bptr *bool) int { | ||
uptr := unsafe.Pointer(bptr) | ||
bytePtr := (*byte)(uptr) | ||
|
||
return int(*bytePtr) | ||
} | ||
|
||
func testMemoryLayoutAssumption() error { | ||
var boolTrue, boolFalse bool | ||
boolTrue = true | ||
|
||
okTrue := castBoolToInt(&boolTrue) | ||
if okTrue != 1 { | ||
return errors.New("expected bool to int cast failed on true") | ||
} | ||
|
||
okFalse := castBoolToInt(&boolFalse) | ||
if okFalse != 0 { | ||
return errors.New("expected bool to int cast failed on false") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package secrethandshake | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestUnsafeBoolCase(t *testing.T) { | ||
if err := testMemoryLayoutAssumption(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |