utils.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "crypto/tls"
  11. "database/sql/driver"
  12. "encoding/binary"
  13. "fmt"
  14. "io"
  15. "strings"
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. )
  20. var (
  21. tlsConfigLock sync.RWMutex
  22. tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
  23. )
  24. // RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
  25. // Use the key as a value in the DSN where tls=value.
  26. //
  27. // Note: The tls.Config provided to needs to be exclusively owned by the driver after registering.
  28. //
  29. // rootCertPool := x509.NewCertPool()
  30. // pem, err := ioutil.ReadFile("/path/ca-cert.pem")
  31. // if err != nil {
  32. // log.Fatal(err)
  33. // }
  34. // if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
  35. // log.Fatal("Failed to append PEM.")
  36. // }
  37. // clientCert := make([]tls.Certificate, 0, 1)
  38. // certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
  39. // if err != nil {
  40. // log.Fatal(err)
  41. // }
  42. // clientCert = append(clientCert, certs)
  43. // mysql.RegisterTLSConfig("custom", &tls.Config{
  44. // RootCAs: rootCertPool,
  45. // Certificates: clientCert,
  46. // })
  47. // db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
  48. //
  49. func RegisterTLSConfig(key string, config *tls.Config) error {
  50. if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
  51. return fmt.Errorf("key '%s' is reserved", key)
  52. }
  53. tlsConfigLock.Lock()
  54. if tlsConfigRegister == nil {
  55. tlsConfigRegister = make(map[string]*tls.Config)
  56. }
  57. tlsConfigRegister[key] = config
  58. tlsConfigLock.Unlock()
  59. return nil
  60. }
  61. // DeregisterTLSConfig removes the tls.Config associated with key.
  62. func DeregisterTLSConfig(key string) {
  63. tlsConfigLock.Lock()
  64. if tlsConfigRegister != nil {
  65. delete(tlsConfigRegister, key)
  66. }
  67. tlsConfigLock.Unlock()
  68. }
  69. func getTLSConfigClone(key string) (config *tls.Config) {
  70. tlsConfigLock.RLock()
  71. if v, ok := tlsConfigRegister[key]; ok {
  72. config = cloneTLSConfig(v)
  73. }
  74. tlsConfigLock.RUnlock()
  75. return
  76. }
  77. // Returns the bool value of the input.
  78. // The 2nd return value indicates if the input was a valid bool value
  79. func readBool(input string) (value bool, valid bool) {
  80. switch input {
  81. case "1", "true", "TRUE", "True":
  82. return true, true
  83. case "0", "false", "FALSE", "False":
  84. return false, true
  85. }
  86. // Not a valid bool value
  87. return
  88. }
  89. /******************************************************************************
  90. * Time related utils *
  91. ******************************************************************************/
  92. // NullTime represents a time.Time that may be NULL.
  93. // NullTime implements the Scanner interface so
  94. // it can be used as a scan destination:
  95. //
  96. // var nt NullTime
  97. // err := db.QueryRow("SELECT time FROM foo WHERE id=?", id).Scan(&nt)
  98. // ...
  99. // if nt.Valid {
  100. // // use nt.Time
  101. // } else {
  102. // // NULL value
  103. // }
  104. //
  105. // This NullTime implementation is not driver-specific
  106. type NullTime struct {
  107. Time time.Time
  108. Valid bool // Valid is true if Time is not NULL
  109. }
  110. // Scan implements the Scanner interface.
  111. // The value type must be time.Time or string / []byte (formatted time-string),
  112. // otherwise Scan fails.
  113. func (nt *NullTime) Scan(value interface{}) (err error) {
  114. if value == nil {
  115. nt.Time, nt.Valid = time.Time{}, false
  116. return
  117. }
  118. switch v := value.(type) {
  119. case time.Time:
  120. nt.Time, nt.Valid = v, true
  121. return
  122. case []byte:
  123. nt.Time, err = parseDateTime(string(v), time.UTC)
  124. nt.Valid = (err == nil)
  125. return
  126. case string:
  127. nt.Time, err = parseDateTime(v, time.UTC)
  128. nt.Valid = (err == nil)
  129. return
  130. }
  131. nt.Valid = false
  132. return fmt.Errorf("Can't convert %T to time.Time", value)
  133. }
  134. // Value implements the driver Valuer interface.
  135. func (nt NullTime) Value() (driver.Value, error) {
  136. if !nt.Valid {
  137. return nil, nil
  138. }
  139. return nt.Time, nil
  140. }
  141. func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
  142. base := "0000-00-00 00:00:00.0000000"
  143. switch len(str) {
  144. case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
  145. if str == base[:len(str)] {
  146. return
  147. }
  148. t, err = time.Parse(timeFormat[:len(str)], str)
  149. default:
  150. err = fmt.Errorf("invalid time string: %s", str)
  151. return
  152. }
  153. // Adjust location
  154. if err == nil && loc != time.UTC {
  155. y, mo, d := t.Date()
  156. h, mi, s := t.Clock()
  157. t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  158. }
  159. return
  160. }
  161. func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Value, error) {
  162. switch num {
  163. case 0:
  164. return time.Time{}, nil
  165. case 4:
  166. return time.Date(
  167. int(binary.LittleEndian.Uint16(data[:2])), // year
  168. time.Month(data[2]), // month
  169. int(data[3]), // day
  170. 0, 0, 0, 0,
  171. loc,
  172. ), nil
  173. case 7:
  174. return time.Date(
  175. int(binary.LittleEndian.Uint16(data[:2])), // year
  176. time.Month(data[2]), // month
  177. int(data[3]), // day
  178. int(data[4]), // hour
  179. int(data[5]), // minutes
  180. int(data[6]), // seconds
  181. 0,
  182. loc,
  183. ), nil
  184. case 11:
  185. return time.Date(
  186. int(binary.LittleEndian.Uint16(data[:2])), // year
  187. time.Month(data[2]), // month
  188. int(data[3]), // day
  189. int(data[4]), // hour
  190. int(data[5]), // minutes
  191. int(data[6]), // seconds
  192. int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
  193. loc,
  194. ), nil
  195. }
  196. return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
  197. }
  198. // zeroDateTime is used in formatBinaryDateTime to avoid an allocation
  199. // if the DATE or DATETIME has the zero value.
  200. // It must never be changed.
  201. // The current behavior depends on database/sql copying the result.
  202. var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
  203. const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  204. const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
  205. func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value, error) {
  206. // length expects the deterministic length of the zero value,
  207. // negative time and 100+ hours are automatically added if needed
  208. if len(src) == 0 {
  209. if justTime {
  210. return zeroDateTime[11 : 11+length], nil
  211. }
  212. return zeroDateTime[:length], nil
  213. }
  214. var dst []byte // return value
  215. var pt, p1, p2, p3 byte // current digit pair
  216. var zOffs byte // offset of value in zeroDateTime
  217. if justTime {
  218. switch length {
  219. case
  220. 8, // time (can be up to 10 when negative and 100+ hours)
  221. 10, 11, 12, 13, 14, 15: // time with fractional seconds
  222. default:
  223. return nil, fmt.Errorf("illegal TIME length %d", length)
  224. }
  225. switch len(src) {
  226. case 8, 12:
  227. default:
  228. return nil, fmt.Errorf("invalid TIME packet length %d", len(src))
  229. }
  230. // +2 to enable negative time and 100+ hours
  231. dst = make([]byte, 0, length+2)
  232. if src[0] == 1 {
  233. dst = append(dst, '-')
  234. }
  235. if src[1] != 0 {
  236. hour := uint16(src[1])*24 + uint16(src[5])
  237. pt = byte(hour / 100)
  238. p1 = byte(hour - 100*uint16(pt))
  239. dst = append(dst, digits01[pt])
  240. } else {
  241. p1 = src[5]
  242. }
  243. zOffs = 11
  244. src = src[6:]
  245. } else {
  246. switch length {
  247. case 10, 19, 21, 22, 23, 24, 25, 26:
  248. default:
  249. t := "DATE"
  250. if length > 10 {
  251. t += "TIME"
  252. }
  253. return nil, fmt.Errorf("illegal %s length %d", t, length)
  254. }
  255. switch len(src) {
  256. case 4, 7, 11:
  257. default:
  258. t := "DATE"
  259. if length > 10 {
  260. t += "TIME"
  261. }
  262. return nil, fmt.Errorf("illegal %s packet length %d", t, len(src))
  263. }
  264. dst = make([]byte, 0, length)
  265. // start with the date
  266. year := binary.LittleEndian.Uint16(src[:2])
  267. pt = byte(year / 100)
  268. p1 = byte(year - 100*uint16(pt))
  269. p2, p3 = src[2], src[3]
  270. dst = append(dst,
  271. digits10[pt], digits01[pt],
  272. digits10[p1], digits01[p1], '-',
  273. digits10[p2], digits01[p2], '-',
  274. digits10[p3], digits01[p3],
  275. )
  276. if length == 10 {
  277. return dst, nil
  278. }
  279. if len(src) == 4 {
  280. return append(dst, zeroDateTime[10:length]...), nil
  281. }
  282. dst = append(dst, ' ')
  283. p1 = src[4] // hour
  284. src = src[5:]
  285. }
  286. // p1 is 2-digit hour, src is after hour
  287. p2, p3 = src[0], src[1]
  288. dst = append(dst,
  289. digits10[p1], digits01[p1], ':',
  290. digits10[p2], digits01[p2], ':',
  291. digits10[p3], digits01[p3],
  292. )
  293. if length <= byte(len(dst)) {
  294. return dst, nil
  295. }
  296. src = src[2:]
  297. if len(src) == 0 {
  298. return append(dst, zeroDateTime[19:zOffs+length]...), nil
  299. }
  300. microsecs := binary.LittleEndian.Uint32(src[:4])
  301. p1 = byte(microsecs / 10000)
  302. microsecs -= 10000 * uint32(p1)
  303. p2 = byte(microsecs / 100)
  304. microsecs -= 100 * uint32(p2)
  305. p3 = byte(microsecs)
  306. switch decimals := zOffs + length - 20; decimals {
  307. default:
  308. return append(dst, '.',
  309. digits10[p1], digits01[p1],
  310. digits10[p2], digits01[p2],
  311. digits10[p3], digits01[p3],
  312. ), nil
  313. case 1:
  314. return append(dst, '.',
  315. digits10[p1],
  316. ), nil
  317. case 2:
  318. return append(dst, '.',
  319. digits10[p1], digits01[p1],
  320. ), nil
  321. case 3:
  322. return append(dst, '.',
  323. digits10[p1], digits01[p1],
  324. digits10[p2],
  325. ), nil
  326. case 4:
  327. return append(dst, '.',
  328. digits10[p1], digits01[p1],
  329. digits10[p2], digits01[p2],
  330. ), nil
  331. case 5:
  332. return append(dst, '.',
  333. digits10[p1], digits01[p1],
  334. digits10[p2], digits01[p2],
  335. digits10[p3],
  336. ), nil
  337. }
  338. }
  339. /******************************************************************************
  340. * Convert from and to bytes *
  341. ******************************************************************************/
  342. func uint64ToBytes(n uint64) []byte {
  343. return []byte{
  344. byte(n),
  345. byte(n >> 8),
  346. byte(n >> 16),
  347. byte(n >> 24),
  348. byte(n >> 32),
  349. byte(n >> 40),
  350. byte(n >> 48),
  351. byte(n >> 56),
  352. }
  353. }
  354. func uint64ToString(n uint64) []byte {
  355. var a [20]byte
  356. i := 20
  357. // U+0030 = 0
  358. // ...
  359. // U+0039 = 9
  360. var q uint64
  361. for n >= 10 {
  362. i--
  363. q = n / 10
  364. a[i] = uint8(n-q*10) + 0x30
  365. n = q
  366. }
  367. i--
  368. a[i] = uint8(n) + 0x30
  369. return a[i:]
  370. }
  371. // treats string value as unsigned integer representation
  372. func stringToInt(b []byte) int {
  373. val := 0
  374. for i := range b {
  375. val *= 10
  376. val += int(b[i] - 0x30)
  377. }
  378. return val
  379. }
  380. // returns the string read as a bytes slice, wheter the value is NULL,
  381. // the number of bytes read and an error, in case the string is longer than
  382. // the input slice
  383. func readLengthEncodedString(b []byte) ([]byte, bool, int, error) {
  384. // Get length
  385. num, isNull, n := readLengthEncodedInteger(b)
  386. if num < 1 {
  387. return b[n:n], isNull, n, nil
  388. }
  389. n += int(num)
  390. // Check data length
  391. if len(b) >= n {
  392. return b[n-int(num) : n : n], false, n, nil
  393. }
  394. return nil, false, n, io.EOF
  395. }
  396. // returns the number of bytes skipped and an error, in case the string is
  397. // longer than the input slice
  398. func skipLengthEncodedString(b []byte) (int, error) {
  399. // Get length
  400. num, _, n := readLengthEncodedInteger(b)
  401. if num < 1 {
  402. return n, nil
  403. }
  404. n += int(num)
  405. // Check data length
  406. if len(b) >= n {
  407. return n, nil
  408. }
  409. return n, io.EOF
  410. }
  411. // returns the number read, whether the value is NULL and the number of bytes read
  412. func readLengthEncodedInteger(b []byte) (uint64, bool, int) {
  413. // See issue #349
  414. if len(b) == 0 {
  415. return 0, true, 1
  416. }
  417. switch b[0] {
  418. // 251: NULL
  419. case 0xfb:
  420. return 0, true, 1
  421. // 252: value of following 2
  422. case 0xfc:
  423. return uint64(b[1]) | uint64(b[2])<<8, false, 3
  424. // 253: value of following 3
  425. case 0xfd:
  426. return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16, false, 4
  427. // 254: value of following 8
  428. case 0xfe:
  429. return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
  430. uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
  431. uint64(b[7])<<48 | uint64(b[8])<<56,
  432. false, 9
  433. }
  434. // 0-250: value of first byte
  435. return uint64(b[0]), false, 1
  436. }
  437. // encodes a uint64 value and appends it to the given bytes slice
  438. func appendLengthEncodedInteger(b []byte, n uint64) []byte {
  439. switch {
  440. case n <= 250:
  441. return append(b, byte(n))
  442. case n <= 0xffff:
  443. return append(b, 0xfc, byte(n), byte(n>>8))
  444. case n <= 0xffffff:
  445. return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
  446. }
  447. return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
  448. byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
  449. }
  450. // reserveBuffer checks cap(buf) and expand buffer to len(buf) + appendSize.
  451. // If cap(buf) is not enough, reallocate new buffer.
  452. func reserveBuffer(buf []byte, appendSize int) []byte {
  453. newSize := len(buf) + appendSize
  454. if cap(buf) < newSize {
  455. // Grow buffer exponentially
  456. newBuf := make([]byte, len(buf)*2+appendSize)
  457. copy(newBuf, buf)
  458. buf = newBuf
  459. }
  460. return buf[:newSize]
  461. }
  462. // escapeBytesBackslash escapes []byte with backslashes (\)
  463. // This escapes the contents of a string (provided as []byte) by adding backslashes before special
  464. // characters, and turning others into specific escape sequences, such as
  465. // turning newlines into \n and null bytes into \0.
  466. // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L823-L932
  467. func escapeBytesBackslash(buf, v []byte) []byte {
  468. pos := len(buf)
  469. buf = reserveBuffer(buf, len(v)*2)
  470. for _, c := range v {
  471. switch c {
  472. case '\x00':
  473. buf[pos] = '\\'
  474. buf[pos+1] = '0'
  475. pos += 2
  476. case '\n':
  477. buf[pos] = '\\'
  478. buf[pos+1] = 'n'
  479. pos += 2
  480. case '\r':
  481. buf[pos] = '\\'
  482. buf[pos+1] = 'r'
  483. pos += 2
  484. case '\x1a':
  485. buf[pos] = '\\'
  486. buf[pos+1] = 'Z'
  487. pos += 2
  488. case '\'':
  489. buf[pos] = '\\'
  490. buf[pos+1] = '\''
  491. pos += 2
  492. case '"':
  493. buf[pos] = '\\'
  494. buf[pos+1] = '"'
  495. pos += 2
  496. case '\\':
  497. buf[pos] = '\\'
  498. buf[pos+1] = '\\'
  499. pos += 2
  500. default:
  501. buf[pos] = c
  502. pos++
  503. }
  504. }
  505. return buf[:pos]
  506. }
  507. // escapeStringBackslash is similar to escapeBytesBackslash but for string.
  508. func escapeStringBackslash(buf []byte, v string) []byte {
  509. pos := len(buf)
  510. buf = reserveBuffer(buf, len(v)*2)
  511. for i := 0; i < len(v); i++ {
  512. c := v[i]
  513. switch c {
  514. case '\x00':
  515. buf[pos] = '\\'
  516. buf[pos+1] = '0'
  517. pos += 2
  518. case '\n':
  519. buf[pos] = '\\'
  520. buf[pos+1] = 'n'
  521. pos += 2
  522. case '\r':
  523. buf[pos] = '\\'
  524. buf[pos+1] = 'r'
  525. pos += 2
  526. case '\x1a':
  527. buf[pos] = '\\'
  528. buf[pos+1] = 'Z'
  529. pos += 2
  530. case '\'':
  531. buf[pos] = '\\'
  532. buf[pos+1] = '\''
  533. pos += 2
  534. case '"':
  535. buf[pos] = '\\'
  536. buf[pos+1] = '"'
  537. pos += 2
  538. case '\\':
  539. buf[pos] = '\\'
  540. buf[pos+1] = '\\'
  541. pos += 2
  542. default:
  543. buf[pos] = c
  544. pos++
  545. }
  546. }
  547. return buf[:pos]
  548. }
  549. // escapeBytesQuotes escapes apostrophes in []byte by doubling them up.
  550. // This escapes the contents of a string by doubling up any apostrophes that
  551. // it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
  552. // effect on the server.
  553. // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
  554. func escapeBytesQuotes(buf, v []byte) []byte {
  555. pos := len(buf)
  556. buf = reserveBuffer(buf, len(v)*2)
  557. for _, c := range v {
  558. if c == '\'' {
  559. buf[pos] = '\''
  560. buf[pos+1] = '\''
  561. pos += 2
  562. } else {
  563. buf[pos] = c
  564. pos++
  565. }
  566. }
  567. return buf[:pos]
  568. }
  569. // escapeStringQuotes is similar to escapeBytesQuotes but for string.
  570. func escapeStringQuotes(buf []byte, v string) []byte {
  571. pos := len(buf)
  572. buf = reserveBuffer(buf, len(v)*2)
  573. for i := 0; i < len(v); i++ {
  574. c := v[i]
  575. if c == '\'' {
  576. buf[pos] = '\''
  577. buf[pos+1] = '\''
  578. pos += 2
  579. } else {
  580. buf[pos] = c
  581. pos++
  582. }
  583. }
  584. return buf[:pos]
  585. }
  586. /******************************************************************************
  587. * Sync utils *
  588. ******************************************************************************/
  589. // noCopy may be embedded into structs which must not be copied
  590. // after the first use.
  591. //
  592. // See https://github.com/golang/go/issues/8005#issuecomment-190753527
  593. // for details.
  594. type noCopy struct{}
  595. // Lock is a no-op used by -copylocks checker from `go vet`.
  596. func (*noCopy) Lock() {}
  597. // atomicBool is a wrapper around uint32 for usage as a boolean value with
  598. // atomic access.
  599. type atomicBool struct {
  600. _noCopy noCopy
  601. value uint32
  602. }
  603. // IsSet returns wether the current boolean value is true
  604. func (ab *atomicBool) IsSet() bool {
  605. return atomic.LoadUint32(&ab.value) > 0
  606. }
  607. // Set sets the value of the bool regardless of the previous value
  608. func (ab *atomicBool) Set(value bool) {
  609. if value {
  610. atomic.StoreUint32(&ab.value, 1)
  611. } else {
  612. atomic.StoreUint32(&ab.value, 0)
  613. }
  614. }
  615. // TrySet sets the value of the bool and returns wether the value changed
  616. func (ab *atomicBool) TrySet(value bool) bool {
  617. if value {
  618. return atomic.SwapUint32(&ab.value, 1) == 0
  619. }
  620. return atomic.SwapUint32(&ab.value, 0) > 0
  621. }
  622. // atomicError is a wrapper for atomically accessed error values
  623. type atomicError struct {
  624. _noCopy noCopy
  625. value atomic.Value
  626. }
  627. // Set sets the error value regardless of the previous value.
  628. // The value must not be nil
  629. func (ae *atomicError) Set(value error) {
  630. ae.value.Store(value)
  631. }
  632. // Value returns the current error value
  633. func (ae *atomicError) Value() error {
  634. if v := ae.value.Load(); v != nil {
  635. // this will panic if the value doesn't implement the error interface
  636. return v.(error)
  637. }
  638. return nil
  639. }