Skip to content

Commit 712fe1d

Browse files
authored
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
1 parent bfa0135 commit 712fe1d

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

windows.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"os"
1414
"path/filepath"
15+
"reflect"
1516
"runtime"
1617
"sync"
1718
"syscall"
@@ -452,8 +453,16 @@ func (w *Watcher) readEvents() {
452453

453454
// Point "raw" to the event in the buffer
454455
raw := (*syscall.FileNotifyInformation)(unsafe.Pointer(&watch.buf[offset]))
455-
buf := (*[syscall.MAX_PATH]uint16)(unsafe.Pointer(&raw.FileName))
456-
name := syscall.UTF16ToString(buf[:raw.FileNameLength/2])
456+
// TODO: Consider using unsafe.Slice that is available from go1.17
457+
// https://stackoverflow.com/questions/51187973/how-to-create-an-array-or-a-slice-from-an-array-unsafe-pointer-in-golang
458+
// instead of using a fixed syscall.MAX_PATH buf, we create a buf that is the size of the path name
459+
size := int(raw.FileNameLength / 2)
460+
var buf []uint16
461+
sh := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
462+
sh.Data = uintptr(unsafe.Pointer(&raw.FileName))
463+
sh.Len = size
464+
sh.Cap = size
465+
name := syscall.UTF16ToString(buf)
457466
fullname := filepath.Join(watch.path, name)
458467

459468
var mask uint64

0 commit comments

Comments
 (0)