Segmentation fault during PC/SC reader selection
### Steps to reproduce
- have a PC/SC reader at slot `0`
- set `LPAC_APDU_PCSC_DRV_IFID` to 1 or higher
- do not set `LPAC_APDU_PCSC_DRV_NAME`
- run `lpac chip info`
### Cause
In [`driver/apdu/pcsc.c -> pcsc_open_hCard_iter()`](https://github.com/estkme-group/lpac/blob/ce4abfaf75d24a4578c6423cf7a833b1ecc55300/driver/apdu/pcsc.c#L120) when the current reader index does not match `LPAC_APDU_PCSC_DRV_IFID`, `LPAC_APDU_PCSC_DRV_NAME` is read but the code does not check if `getenv()` returns null.
### Fix
A quick fix would be to check the nulllity:
```C
const int id = getenv_or_default(ENV_DRV_IFID, (int)-1);
if (id != -1 && id != index) {
const char *part_name = getenv(ENV_DRV_NAME);
if (part_name == NULL || strstr(reader, part_name) == NULL) {
return 0;
}
}
```
However the current reader selection logic is a bit strange. `LPAC_APDU_PCSC_DRV_NAME` is only read if `LPAC_APDU_PCSC_DRV_IFID` is set (but at that point why?).
I find either of these more intuitive:
- `LPAC_APDU_PCSC_DRV_IFID` and `LPAC_APDU_PCSC_DRV_NAME` act independently (whichever matches first, selects the reader)
```C
const int id = getenv_or_default(ENV_DRV_IFID, (int)-1);
const char *part_name = getenv(ENV_DRV_NAME);
if ((id != -1 || part_name != NULL) && // some of the positive filter env vars are set
(id == -1 || id != index) && // DRV_IFID does not match
(part_name == NULL || strstr(reader, part_name) == NULL) // DRV_NAME does not match
) {
return 0; // reject the reader
}
```
- `LPAC_APDU_PCSC_DRV_IFID` overrides `LPAC_APDU_PCSC_DRV_NAME` (i.e. the latter is ignored if the former is set)
```C
const int id = getenv_or_default(ENV_DRV_IFID, (int)-1);
const char *part_name = getenv(ENV_DRV_NAME);
if ((id != -1 && id != index) || // DRV_IFID does not match
(id == -1 && part_name != NULL && strstr(reader, part_name) == NULL) // DRV_IFID is not set and DRV_NAME does not match
) {
return 0; // reject the reader
}
```
0 条评论