File tree 2 files changed +38
-4
lines changed
2 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -533,11 +533,21 @@ const ImplementsGetwd = true
533
533
534
534
func Getwd () (wd string , err error ) {
535
535
b := make ([]uint16 , 300 )
536
- n , e := GetCurrentDirectory (uint32 (len (b )), & b [0 ])
537
- if e != nil {
538
- return "" , e
536
+ // The path of the current directory may not fit in the initial 300-word
537
+ // buffer when long path support is enabled. The current directory may also
538
+ // change between subsequent calls of GetCurrentDirectory. As a result, we
539
+ // need to retry the call in a loop until the current directory fits, each
540
+ // time with a bigger buffer.
541
+ for {
542
+ n , e := GetCurrentDirectory (uint32 (len (b )), & b [0 ])
543
+ if e != nil {
544
+ return "" , e
545
+ }
546
+ if int (n ) <= len (b ) {
547
+ return UTF16ToString (b [:n ]), nil
548
+ }
549
+ b = make ([]uint16 , n )
539
550
}
540
- return UTF16ToString (b [0 :n ]), nil
541
551
}
542
552
543
553
func Chdir (path string ) (err error ) {
Original file line number Diff line number Diff line change @@ -180,6 +180,30 @@ int main(int argc, char *argv[])
180
180
}
181
181
}
182
182
183
+ func TestGetwd_DoesNotPanicWhenPathIsLong (t * testing.T ) {
184
+ // Regression test for https://github.com/golang/go/issues/60051.
185
+
186
+ // The length of a filename is also limited, so we can't reproduce the
187
+ // crash by creating a single directory with a very long name; we need two
188
+ // layers.
189
+ a200 := strings .Repeat ("a" , 200 )
190
+ dirname := filepath .Join (t .TempDir (), a200 , a200 )
191
+
192
+ err := os .MkdirAll (dirname , 0o700 )
193
+ if err != nil {
194
+ t .Skipf ("MkdirAll failed: %v" , err )
195
+ }
196
+ err = os .Chdir (dirname )
197
+ if err != nil {
198
+ t .Skipf ("Chdir failed: %v" , err )
199
+ }
200
+ // Change out of the temporary directory so that we don't inhibit its
201
+ // removal during test cleanup.
202
+ defer os .Chdir (`\` )
203
+
204
+ syscall .Getwd ()
205
+ }
206
+
183
207
func FuzzUTF16FromString (f * testing.F ) {
184
208
f .Add ("hi" ) // ASCII
185
209
f .Add ("â" ) // latin1
You can’t perform that action at this time.
0 commit comments