]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: fix Gettimeofday on macOS Sierra
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 4 Aug 2016 19:21:05 +0000 (12:21 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 4 Aug 2016 21:42:44 +0000 (21:42 +0000)
Fixes #16606

Change-Id: I57465061b90e901293cd8b6ef756d6aa84ebd4a3
Reviewed-on: https://go-review.googlesource.com/25495
Reviewed-by: Quentin Smith <quentin@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/syscall/syscall_darwin_386.go
src/syscall/syscall_darwin_amd64.go
src/syscall/syscall_darwin_test.go [new file with mode: 0644]

index 7dbb1c3d64c3b4d4ddc1d8d9bfc3333be5092886..f75de000bdbb4db01b59770de8229180a23e2928 100644 (file)
@@ -26,14 +26,21 @@ func NsecToTimeval(nsec int64) (tv Timeval) {
 }
 
 //sysnb        gettimeofday(tp *Timeval) (sec int32, usec int32, err error)
-func Gettimeofday(tv *Timeval) (err error) {
-       // The tv passed to gettimeofday must be non-nil
-       // but is otherwise unused. The answers come back
-       // in the two registers.
+func Gettimeofday(tv *Timeval) error {
+       // The tv passed to gettimeofday must be non-nil.
+       // Before macOS Sierra (10.12), tv was otherwise unused and
+       // the answers came back in the two registers.
+       // As of Sierra, gettimeofday return zeros and populates
+       // tv itself.
        sec, usec, err := gettimeofday(tv)
-       tv.Sec = int32(sec)
-       tv.Usec = int32(usec)
-       return err
+       if err != nil {
+               return err
+       }
+       if sec != 0 || usec != 0 {
+               tv.Sec = int32(sec)
+               tv.Usec = int32(usec)
+       }
+       return nil
 }
 
 func SetKevent(k *Kevent_t, fd, mode, flags int) {
index 80e6024aeba4d00aec31d52f84b700934197f215..79083117b61e30e567c194412d7241914c3d2826 100644 (file)
@@ -26,14 +26,21 @@ func NsecToTimeval(nsec int64) (tv Timeval) {
 }
 
 //sysnb        gettimeofday(tp *Timeval) (sec int64, usec int32, err error)
-func Gettimeofday(tv *Timeval) (err error) {
-       // The tv passed to gettimeofday must be non-nil
-       // but is otherwise unused. The answers come back
-       // in the two registers.
+func Gettimeofday(tv *Timeval) error {
+       // The tv passed to gettimeofday must be non-nil.
+       // Before macOS Sierra (10.12), tv was otherwise unused and
+       // the answers came back in the two registers.
+       // As of Sierra, gettimeofday return zeros and populates
+       // tv itself.
        sec, usec, err := gettimeofday(tv)
-       tv.Sec = sec
-       tv.Usec = usec
-       return err
+       if err != nil {
+               return err
+       }
+       if sec != 0 || usec != 0 {
+               tv.Sec = sec
+               tv.Usec = usec
+       }
+       return nil
 }
 
 func SetKevent(k *Kevent_t, fd, mode, flags int) {
diff --git a/src/syscall/syscall_darwin_test.go b/src/syscall/syscall_darwin_test.go
new file mode 100644 (file)
index 0000000..dd0e32b
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin
+// +build amd64 386
+
+package syscall_test
+
+import (
+       "syscall"
+       "testing"
+)
+
+func TestDarwinGettimeofday(t *testing.T) {
+       tv := &syscall.Timeval{}
+       if err := syscall.Gettimeofday(tv); err != nil {
+               t.Fatal(err)
+       }
+       if tv.Sec == 0 && tv.Usec == 0 {
+               t.Fatal("Sec and Usec both zero")
+       }
+}