]> go.fuhry.dev Git - fsnotify.git/commitdiff
Fix potential crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH (#361)
authorHangkun Ung <hangkun.ung@gmail.com>
Wed, 26 Jan 2022 13:21:14 +0000 (08:21 -0500)
committerGitHub <noreply@github.com>
Wed, 26 Jan 2022 13:21:14 +0000 (22:21 +0900)
* Fix crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH

* Add comment

* Update windows.go

windows.go

index c02b75f7c374733c4c657215d59b1afd00dada9a..b0105ae4cb0148ecba7f5383c6757f6b877f3b67 100644 (file)
@@ -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