Skip to content

jody: fix checkptr segfault #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions jody/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ var mask64 = [...]uint64{
0xffffffffffffffff,
}

func accumulate(data, acc, cons uint64) uint64 {
acc = acc + data + cons
acc = (acc<<shift | acc>>(64-shift)) ^ data
acc = ((acc<<shift | acc>>(64-shift)) ^ cons) + data
return acc
}

// HashString64 returns the hash of s.
func HashString64(s string) uint64 {
return AddString64(Init64, s)
Expand Down Expand Up @@ -88,43 +95,44 @@ func AddString64(h uint64, s string) uint64 {
return hash;
*/

r := *(*reflect.StringHeader)(unsafe.Pointer(&s))
p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
dataLen := len(s)

for n := r.Len / 8; n != 0; n-- {
for dataLen > 8 {
v := *(*uint64)(p)
h = accumulate(v, h, constant)
p = unsafe.Pointer(uintptr(p) + 8)
dataLen -= 8
}

h = h + v + constant
h = (h<<shift | h>>(64-shift)) ^ v
h = ((h<<shift | h>>(64-shift)) ^ constant) + v
if dataLen == 8 {
v := *(*uint64)(p)
return accumulate(v, h, constant)
}

p = unsafe.Pointer(uintptr(p) + 8)
if dataLen == 0 {
return h
}

if n := (r.Len & 7); n != 0 {
c := constant & mask64[n]
v := uint64(0)
off := uint(0)
if 0 != (n & 4) {
v += uint64(*(*uint32)(p))
off += 32
p = unsafe.Pointer(uintptr(p) + 4)
}
if 0 != (n & 2) {
v += uint64(*(*uint16)(p)) << off
off += 16
p = unsafe.Pointer(uintptr(p) + 2)
}
if 0 != (n & 1) {
v += uint64(*(*uint8)(p)) << off
}
// now we have a remainder 1 <= r.Len <= 7

off := uintptr(0)
v := uint64(0)
if 0 != dataLen&4 {
v += uint64(*(*uint32)(unsafe.Pointer(uintptr(p) + off)))
off += 4
}

if 0 != dataLen&2 {
v += uint64(*(*uint16)(unsafe.Pointer(uintptr(p) + off)))
off += 2
}

h = h + v + c
h = (h<<shift | h>>(64-shift)) ^ v
h = ((h<<shift | h>>(64-shift)) ^ c) + v
if 0 != dataLen&1 {
v += uint64(*(*uint8)(unsafe.Pointer(uintptr(p) + off)))
}

return h
return accumulate(v, h, constant&mask64[dataLen])
}

// AddBytes64 adds the hash of b to the precomputed hash value h.
Expand All @@ -134,8 +142,5 @@ func AddBytes64(h uint64, b []byte) uint64 {

// AddUint64 adds the hash value of the 8 bytes of u to h.
func AddUint64(h uint64, u uint64) uint64 {
h = h + u + constant
h = (h<<shift | h>>(64-shift)) ^ u
h = ((h<<shift | h>>(64-shift)) ^ constant) + u
return h
return accumulate(u, h, constant)
}