Skip to content

Commit

Permalink
Fix crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
hu13 committed Mar 3, 2021
1 parent 7f4cf4d commit b53dd2e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions windows.go
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"sync"
"syscall"
Expand Down Expand Up @@ -451,8 +452,15 @@ 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])
// 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
Expand Down

0 comments on commit b53dd2e

Please sign in to comment.