clausecker / nfc

Go bindings for the libnfc
https://pkg.go.dev/github.com/clausecker/nfc/v2
GNU Lesser General Public License v3.0
101 stars 20 forks source link

Device.InitiatorSelectPassiveTarget panic when InfiniteSelect == false and no tag was found #9

Closed wincss closed 8 years ago

wincss commented 8 years ago

Problem

When InfiniteSelect == false, nfc_initiator_select_passive_target return 0 when no tag was found, but Device.InitiatorSelectPassiveTarget always calls unmarshallTarget.

Example in https://github.com/nfc-tools/libnfc/blob/master/utils/nfc-mfclassic.c

  if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
    printf("Error: no tag was found\n");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

This code use <= 0 for error checking.

How to reproduce

func main() {
    dev, _ := nfc.Open("")
    defer dev.Close()
    dev.InitiatorInit()
    dev.SetPropertyBool(nfc.InfiniteSelect, false)
    dev.SetPropertyBool(nfc.AutoISO14443_4, false)
    fmt.Printf("NFC reader: %s opened\n", dev)
    dev.InitiatorSelectPassiveTarget(nmMifare, nil) // panic when no tag was present
}

Affected version

all version include latest

clausecker commented 8 years ago

If you make a patch, I'll include it. Otherwise, you have to wait until I get around doing this.

wincss commented 8 years ago

@fuzxxl OK, I'll submit a pull request later.

Should I fix it just in lastest version(2.0), or in all versions?

clausecker commented 8 years ago

Just fix 2.0 and dev. The other paths aren't maintained. Don't forget to update CHANGES as well and enter yourself as an author into the file you update together with an updated copyright year.