richardlehane / mscfb

golang reader for the Microsoft Compound File Binary File format
Apache License 2.0
65 stars 19 forks source link

Data race with concurrent reads across Reader.File #15

Open wizardishungry opened 2 months ago

wizardishungry commented 2 months ago
func TestConcurrentAccess(t *testing.T) {
    file, _ := os.Open("../../testdata/cool_images.doc") // a file with small number of embedded images
    defer file.Close()
    doc, err := mscfb.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()
}

Race is on r.buf in mscfb.(*Reader).readAt()

    if _, err := r.ra.ReadAt(r.buf[:length], offset); err != nil {
richardlehane commented 2 months ago

thanks for this report @wizardishungry