gcpug / zagane

zagane is a static analysis tool which can find bugs in spanner's code
MIT License
89 stars 15 forks source link

unclosetx: false positive detection with errgroup #32

Open takatoshiono opened 4 years ago

takatoshiono commented 4 years ago

When I use errgroup, zagane reports transaction must be closed. I think that it's false positive detection.

sample code:

func f5(ctx context.Context, client *spanner.Client) error {
    tx := client.ReadOnlyTransaction() // OK
    defer tx.Close()

    var eg errgroup.Group

    eg.Go(func() error {
        _ = tx // use tx
        return nil
    })

    if err := eg.Wait(); err != nil {
        return err
    }
    return nil
}
tenntenn commented 4 years ago

more simple sample code:

func f5(ctx context.Context, client *spanner.Client) error {
    tx := client.ReadOnlyTransaction() // OK
    defer tx.Close()

    func() error {
        _ = tx // use tx
        return nil
    }()

    return nil
}
tenntenn commented 4 years ago

I think this issue has two bugs. First bug is false positive of tx := client.ReadOnlyTransaction(). Second bug is false positive of _ = tx // use tx. Actually the below code zagane reports false positive in only case (2).

func() error {
    func(tx *spanner.ReadOnlyTransaction) {}(tx) // (1) OK
    _ = tx // (2) NG
    return nil
}()