Skip to content

Commit cb0a624

Browse files
fix bug in detecting and handling malformed json in magics.json
1 parent 013af03 commit cb0a624

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

bitboard_magic.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const (
1919
MAGICS_JSON = "magics.json"
2020
)
2121

22+
type MagicData struct {
23+
BishopMagics [64]BB `json:"bishopMagics"`
24+
RookMagics [64]BB `json:"rookMagics"`
25+
}
26+
2227
// In testing, homogenous array move DB actually outperformed a 'Fancy'
2328
// magic bitboard approach implemented using slices.
2429
var bishopMagicMoves, rookMagicMoves [64][MAGIC_DB_SIZE]BB
@@ -49,15 +54,17 @@ func setupMagicMoveGen() {
4954

5055
magicsNeeded := false
5156
if _, err := os.Stat(MAGICS_JSON); err == nil {
52-
if !loadMagics() {
53-
// if magics failed to load for any reason, we'll have to generate them.
57+
if !loadMagics() { // if magics failed to load for any reason, we'll have to generate them.
5458
magicsNeeded = true
5559
}
5660
} else {
5761
magicsNeeded = true
62+
}
63+
if magicsNeeded {
5864
fmt.Printf("Calculating magics")
5965
wg.Add(64 * 2)
6066
}
67+
6168
setupMagicsForPiece(magicsNeeded, &wg, &bishopMagicMasks, &bishopMasks, &bishopMagics,
6269
&bishopMagicMoves, generateBishopAttacks)
6370
setupMagicsForPiece(magicsNeeded, &wg, &rookMagicMasks, &rookMasks, &rookMagics,
@@ -70,11 +77,6 @@ func setupMagicMoveGen() {
7077
}
7178
}
7279

73-
type MagicData struct {
74-
BishopMagics [64]BB
75-
RookMagics [64]BB
76-
}
77-
7880
func checkError(e error) {
7981
if e != nil {
8082
panic(e)
@@ -101,6 +103,7 @@ func writeMagicsToDisk() {
101103
func loadMagics() (success bool) {
102104
defer func() {
103105
if r := recover(); r != nil {
106+
fmt.Printf("Failure reading magics from disk.")
104107
success = false // recover any panic
105108
}
106109
}()
@@ -110,7 +113,8 @@ func loadMagics() (success bool) {
110113

111114
magics := &MagicData{}
112115

113-
json.Unmarshal(data, magics) // will panic if malformed json present.
116+
err = json.Unmarshal(data, magics)
117+
checkError(err)
114118

115119
bishopMagics = magics.BishopMagics
116120
rookMagics = magics.RookMagics

magics.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"BishopMagics":[326515105195884672,9351232053886256656,1153554887999095568,1279391854758232840,2508575584797397643,10377490222769296512,9297699067995095360,2630157366951389193,8802687094852,7152280545523016708,3521817125150812672,1299295098307760257,9946270217809069328,4830112838100091072,8577110183787390088,8577110183787390088,6362469745186783772,27050485722976403,8856406803464638976,9225628295129731408,9940573590046773312,1170962292505319700,4686137352995758592,58547432158403584,9525887268097917955,12114762319265153580,2603241826299741453,871449450668925074,2543533783746352128,2852685241740,288531170235424840,1159993838274746502,2356392094547095634,2356392094547095634,288215995048530,2817032547733516,18054015824953376,5207112282836042064,363678052623975559,9241533839749709864,12360551463890538518,12360551463890538518,14233772891828548174,16287602324397948945,36031010705526808,2298408852045952,2333147469255086209,2423218246404587938,4611756845164986408,8381269559958603032,10993287420690171968,2956647083704587,2596888249488638081,9229054485693774856,36943661648381968,1461745738023469073,3674972516353442307,3468106406993106954,168304530694439478,11584454862638784516,7233134497237177864,11532035339632379936,9451930268312642048,13835208826628089136],"RookMagics":[5512410616834429472,90074192108601353,45036133784494177,1297107224636686342,180146493364371457,14992488932053188612,2458983092883587586,2053646377888260352,2456572912965009416,2774221793136487686,576502538107897890,3143516390372147720,14268810998723020802,9802717963571167442,14258115104468943232,1442735246310154368,2748709250460570304,1590052419337585738,2469764802480734920,4882464980394337396,2382421797218877451,4148237507996682560,2398324031804690580,1748419201242153377,509050321604214900,74313810101415936,9232981768625201188,2549055054292520968,147220466729746674,9228179239399851144,2738754549757204485,4707334948699259013,5476412332366103568,5199476138719611904,3175179067492139300,11004556824452005976,578757082606534720,9290891509531137,10443994872376762369,102493312912508930,9583730380397086720,4730194405340283981,38285819781529606,4683754616775573760,580968958149018000,1191237295877948417,434679140361831184,9799976904674000905,12718869045100298368,1442009091817669808,945767055108538390,1152939096977966112,9331528951476257793,10092590955283890210,9367529294270693504,9374859507322063488,2598031902657118722,17654251551660716265,2477780536947376385,11530624688696853513,3464569956992026242,10955643821066422275,9489166130917705990,3170552989404922882]}
1+
{"bishopMagics":[326515105195884672,9351232053886256656,1153554887999095568,1279391854758232840,2508575584797397643,10377490222769296512,9297699067995095360,2630157366951389193,8802687094852,7152280545523016708,3521817125150812672,1299295098307760257,9946270217809069328,4830112838100091072,8577110183787390088,8577110183787390088,6362469745186783772,27050485722976403,8856406803464638976,9225628295129731408,9940573590046773312,1170962292505319700,4686137352995758592,58547432158403584,9525887268097917955,12114762319265153580,2603241826299741453,871449450668925074,2543533783746352128,2852685241740,288531170235424840,1159993838274746502,2356392094547095634,2356392094547095634,288215995048530,2817032547733516,18054015824953376,5207112282836042064,363678052623975559,9241533839749709864,12360551463890538518,12360551463890538518,14233772891828548174,16287602324397948945,36031010705526808,2298408852045952,2333147469255086209,2423218246404587938,4611756845164986408,8381269559958603032,10993287420690171968,2956647083704587,2596888249488638081,9229054485693774856,36943661648381968,1461745738023469073,3674972516353442307,3468106406993106954,168304530694439478,11584454862638784516,7233134497237177864,11532035339632379936,9451930268312642048,13835208826628089136],"rookMagics":[5512410616834429472,90074192108601353,45036133784494177,1297107224636686342,180146493364371457,14992488932053188612,2458983092883587586,2053646377888260352,2456572912965009416,2774221793136487686,576502538107897890,3143516390372147720,14268810998723020802,9802717963571167442,14258115104468943232,1442735246310154368,2748709250460570304,1590052419337585738,2469764802480734920,4882464980394337396,2382421797218877451,4148237507996682560,2398324031804690580,1748419201242153377,509050321604214900,74313810101415936,9232981768625201188,2549055054292520968,147220466729746674,9228179239399851144,2738754549757204485,4707334948699259013,5476412332366103568,5199476138719611904,3175179067492139300,11004556824452005976,578757082606534720,9290891509531137,10443994872376762369,102493312912508930,9583730380397086720,4730194405340283981,38285819781529606,4683754616775573760,580968958149018000,1191237295877948417,434679140361831184,9799976904674000905,12718869045100298368,1442009091817669808,945767055108538390,1152939096977966112,9331528951476257793,10092590955283890210,9367529294270693504,9374859507322063488,2598031902657118722,17654251551660716265,2477780536947376385,11530624688696853513,3464569956992026242,10955643821066422275,9489166130917705990,3170552989404922882]}

0 commit comments

Comments
 (0)