Skip to content

Commit

Permalink
Fix potential crash on windows if raw.FileNameLength exceeds syscall.…
Browse files Browse the repository at this point in the history
…MAX_PATH (#361)

* Fix crash on windows if raw.FileNameLength exceeds syscall.MAX_PATH

* Add comment

* Update windows.go
  • Loading branch information
hu13 committed Jan 26, 2022
1 parent bfa0135 commit 712fe1d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions windows.go
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"sync"
"syscall"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 712fe1d

Please sign in to comment.