@@ -250,7 +250,9 @@ func downloadZip(mod module.Version, zipfile string) (err error) {
250
250
if err != nil {
251
251
return err
252
252
}
253
- checkModSum (mod , hash )
253
+ if err := checkModSum (mod , hash ); err != nil {
254
+ return err
255
+ }
254
256
255
257
if err := renameio .WriteFile (zipfile + "hash" , []byte (hash ), 0666 ); err != nil {
256
258
return err
@@ -282,21 +284,22 @@ var goSum struct {
282
284
}
283
285
284
286
// initGoSum initializes the go.sum data.
285
- // It reports whether use of go.sum is now enabled.
287
+ // The boolean it returns reports whether the
288
+ // use of go.sum is now enabled.
286
289
// The goSum lock must be held.
287
- func initGoSum () bool {
290
+ func initGoSum () ( bool , error ) {
288
291
if GoSumFile == "" {
289
- return false
292
+ return false , nil
290
293
}
291
294
if goSum .m != nil {
292
- return true
295
+ return true , nil
293
296
}
294
297
295
298
goSum .m = make (map [module.Version ][]string )
296
299
goSum .checked = make (map [modSum ]bool )
297
300
data , err := renameio .ReadFile (GoSumFile )
298
301
if err != nil && ! os .IsNotExist (err ) {
299
- base . Fatalf ( "go: %v" , err )
302
+ return false , err
300
303
}
301
304
goSum .enabled = true
302
305
readGoSum (goSum .m , GoSumFile , data )
@@ -314,7 +317,7 @@ func initGoSum() bool {
314
317
}
315
318
goSum .modverify = alt
316
319
}
317
- return true
320
+ return true , nil
318
321
}
319
322
320
323
// emptyGoModHash is the hash of a 1-file tree containing a 0-length go.mod.
@@ -324,7 +327,7 @@ const emptyGoModHash = "h1:G7mAYYxgmS0lVkHyy2hEOLQCFB0DlQFTMLWggykrydY="
324
327
325
328
// readGoSum parses data, which is the content of file,
326
329
// and adds it to goSum.m. The goSum lock must be held.
327
- func readGoSum (dst map [module.Version ][]string , file string , data []byte ) {
330
+ func readGoSum (dst map [module.Version ][]string , file string , data []byte ) error {
328
331
lineno := 0
329
332
for len (data ) > 0 {
330
333
var line []byte
@@ -341,7 +344,7 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) {
341
344
continue
342
345
}
343
346
if len (f ) != 3 {
344
- base . Fatalf ( "go: malformed go.sum:\n %s:%d: wrong number of fields %v" , file , lineno , len (f ))
347
+ return fmt . Errorf ( " malformed go.sum:\n %s:%d: wrong number of fields %v" , file , lineno , len (f ))
345
348
}
346
349
if f [2 ] == emptyGoModHash {
347
350
// Old bug; drop it.
@@ -350,6 +353,7 @@ func readGoSum(dst map[module.Version][]string, file string, data []byte) {
350
353
mod := module.Version {Path : f [0 ], Version : f [1 ]}
351
354
dst [mod ] = append (dst [mod ], f [2 ])
352
355
}
356
+ return nil
353
357
}
354
358
355
359
// checkMod checks the given module's checksum.
@@ -377,7 +381,9 @@ func checkMod(mod module.Version) {
377
381
base .Fatalf ("verifying %v" , module .VersionError (mod , fmt .Errorf ("unexpected ziphash: %q" , h )))
378
382
}
379
383
380
- checkModSum (mod , h )
384
+ if err := checkModSum (mod , h ); err != nil {
385
+ base .Fatalf ("%s" , err )
386
+ }
381
387
}
382
388
383
389
// goModSum returns the checksum for the go.mod contents.
@@ -389,37 +395,42 @@ func goModSum(data []byte) (string, error) {
389
395
390
396
// checkGoMod checks the given module's go.mod checksum;
391
397
// data is the go.mod content.
392
- func checkGoMod (path , version string , data []byte ) {
398
+ func checkGoMod (path , version string , data []byte ) error {
393
399
h , err := goModSum (data )
394
400
if err != nil {
395
- base . Fatalf ("verifying %s %s go.mod: %v" , path , version , err )
401
+ return & module. ModuleError { Path : path , Version : version , Err : fmt . Errorf ("verifying go.mod: %v" , err )}
396
402
}
397
403
398
- checkModSum (module.Version {Path : path , Version : version + "/go.mod" }, h )
404
+ return checkModSum (module.Version {Path : path , Version : version + "/go.mod" }, h )
399
405
}
400
406
401
407
// checkModSum checks that the recorded checksum for mod is h.
402
- func checkModSum (mod module.Version , h string ) {
408
+ func checkModSum (mod module.Version , h string ) error {
403
409
// We lock goSum when manipulating it,
404
410
// but we arrange to release the lock when calling checkSumDB,
405
411
// so that parallel calls to checkModHash can execute parallel calls
406
412
// to checkSumDB.
407
413
408
414
// Check whether mod+h is listed in go.sum already. If so, we're done.
409
415
goSum .mu .Lock ()
410
- inited := initGoSum ()
416
+ inited , err := initGoSum ()
417
+ if err != nil {
418
+ return err
419
+ }
411
420
done := inited && haveModSumLocked (mod , h )
412
421
goSum .mu .Unlock ()
413
422
414
423
if done {
415
- return
424
+ return nil
416
425
}
417
426
418
427
// Not listed, so we want to add them.
419
428
// Consult checksum database if appropriate.
420
429
if useSumDB (mod ) {
421
430
// Calls base.Fatalf if mismatch detected.
422
- checkSumDB (mod , h )
431
+ if err := checkSumDB (mod , h ); err != nil {
432
+ return err
433
+ }
423
434
}
424
435
425
436
// Add mod+h to go.sum, if it hasn't appeared already.
@@ -428,6 +439,7 @@ func checkModSum(mod module.Version, h string) {
428
439
addModSumLocked (mod , h )
429
440
goSum .mu .Unlock ()
430
441
}
442
+ return nil
431
443
}
432
444
433
445
// haveModSumLocked reports whether the pair mod,h is already listed in go.sum.
@@ -461,22 +473,23 @@ func addModSumLocked(mod module.Version, h string) {
461
473
462
474
// checkSumDB checks the mod, h pair against the Go checksum database.
463
475
// It calls base.Fatalf if the hash is to be rejected.
464
- func checkSumDB (mod module.Version , h string ) {
476
+ func checkSumDB (mod module.Version , h string ) error {
465
477
db , lines , err := lookupSumDB (mod )
466
478
if err != nil {
467
- base . Fatalf ( "verifying %s@%s : %v" , mod . Path , mod . Version , err )
479
+ return module . VersionError ( mod , fmt . Errorf ( "verifying module : %v" , err ) )
468
480
}
469
481
470
482
have := mod .Path + " " + mod .Version + " " + h
471
483
prefix := mod .Path + " " + mod .Version + " h1:"
472
484
for _ , line := range lines {
473
485
if line == have {
474
- return
486
+ return nil
475
487
}
476
488
if strings .HasPrefix (line , prefix ) {
477
- base . Fatalf ( "verifying %s@%s : checksum mismatch\n \t downloaded: %v\n \t %s: %v" + sumdbMismatch , mod . Path , mod . Version , h , db , line [len (prefix )- len ("h1:" ):])
489
+ return module . VersionError ( mod , fmt . Errorf ( "verifying module : checksum mismatch\n \t downloaded: %v\n \t %s: %v" + sumdbMismatch , h , db , line [len (prefix )- len ("h1:" ):]) )
478
490
}
479
491
}
492
+ return nil
480
493
}
481
494
482
495
// Sum returns the checksum for the downloaded copy of the given module,
@@ -586,7 +599,11 @@ func WriteGoSum() {
586
599
func TrimGoSum (keep map [module.Version ]bool ) {
587
600
goSum .mu .Lock ()
588
601
defer goSum .mu .Unlock ()
589
- if ! initGoSum () {
602
+ inited , err := initGoSum ()
603
+ if err != nil {
604
+ base .Fatalf ("%s" , err )
605
+ }
606
+ if ! inited {
590
607
return
591
608
}
592
609
0 commit comments