Skip to content

Commit

Permalink
data race on concurrent file reads; #15
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlehane committed Sep 13, 2024
1 parent 1b0a087 commit 2e9a4d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
32 changes: 17 additions & 15 deletions mscfb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
// early MS software such as MS Office.
//
// Example:
// file, _ := os.Open("test/test.doc")
// defer file.Close()
// doc, err := mscfb.New(file)
// if err != nil {
// log.Fatal(err)
// }
// for entry, err := doc.Next(); err == nil; entry, err = doc.Next() {
// buf := make([]byte, 512)
// i, _ := entry.Read(buf)
// if i > 0 {
// fmt.Println(buf[:i])
// }
// fmt.Println(entry.Name)
// }
//
// file, _ := os.Open("test/test.doc")
// defer file.Close()
// doc, err := mscfb.New(file)
// if err != nil {
// log.Fatal(err)
// }
// for entry, err := doc.Next(); err == nil; entry, err = doc.Next() {
// buf := make([]byte, 512)
// i, _ := entry.Read(buf)
// if i > 0 {
// fmt.Println(buf[:i])
// }
// fmt.Println(entry.Name)
// }
package mscfb

import (
Expand Down Expand Up @@ -262,7 +263,8 @@ func (r *Reader) findNext(sn uint32, mini bool) (uint32, error) {
}
fatIndex := sn % entries // find position within FAT or MiniFAT sector
offset := fileOffset(r.sectorSize, sect) + int64(fatIndex*4)
buf, err := r.readAt(offset, 4)
buf := make([]byte, 4)
_, err := r.ra.ReadAt(buf, offset)
if err != nil {
return 0, Error{ErrRead, "bad read finding next sector (" + err.Error() + ")", offset}
}
Expand Down
23 changes: 23 additions & 0 deletions mscfb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"sync"
"testing"
)

Expand Down Expand Up @@ -143,6 +145,27 @@ func TestXls(t *testing.T) {
testFile(t, testXls)
}

func TestConcurrentAccess(t *testing.T) {
file, _ := os.Open(testXls)
defer file.Close()
doc, err := New(file)
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(len(doc.File))
for _, f := range doc.File {
go func() {
defer wg.Done()
_, err := io.Copy(io.Discard, f)
if err != nil {
log.Println(err)
}
}()
}
wg.Wait()
}

func TestSeek(t *testing.T) {
file, _ := os.Open(testXls)
defer file.Close()
Expand Down

0 comments on commit 2e9a4d7

Please sign in to comment.