From: Hangkun Ung Date: Wed, 26 Jan 2022 13:21:14 +0000 (-0500) Subject: Fix potential crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH (#361) X-Git-Tag: v1.7.2~123 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=712fe1d802d8c20c95e843adac4d8b11c1d636d1;p=fsnotify.git Fix potential crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH (#361) * Fix crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH * Add comment * Update windows.go --- diff --git a/windows.go b/windows.go index c02b75f..b0105ae 100644 --- a/windows.go +++ b/windows.go @@ -12,6 +12,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "runtime" "sync" "syscall" @@ -452,8 +453,16 @@ func (w *Watcher) readEvents() { // Point "raw" to the event in the buffer raw := (*syscall.FileNotifyInformation)(unsafe.Pointer(&watch.buf[offset])) - buf := (*[syscall.MAX_PATH]uint16)(unsafe.Pointer(&raw.FileName)) - name := syscall.UTF16ToString(buf[:raw.FileNameLength/2]) + // TODO: Consider using unsafe.Slice that is available from go1.17 + // https://stackoverflow.com/questions/51187973/how-to-create-an-array-or-a-slice-from-an-array-unsafe-pointer-in-golang + // instead of using a fixed syscall.MAX_PATH buf, we create a buf that is the size of the path name + size := int(raw.FileNameLength / 2) + var buf []uint16 + sh := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) + sh.Data = uintptr(unsafe.Pointer(&raw.FileName)) + sh.Len = size + sh.Cap = size + name := syscall.UTF16ToString(buf) fullname := filepath.Join(watch.path, name) var mask uint64