Skip to content

Commit

Permalink
scpi_vxi: fix memory leak for SCPI response data in VXI support code
Browse files Browse the repository at this point in the history
Routine scpi_vxi_read_data() invokes device_read_1(), which provides a
static buffer where dynamically allocated memory for SCPI response data
is kept. Release this memory after getting a copy of the response data,
before the next device_read_1() call loses the reference.

Valgrind stats without the fix:

  ==238825== LEAK SUMMARY:
  ==238825== definitely lost: 45,547,737 bytes in 18,331 blocks
  ==238825== indirectly lost: 0 bytes in 0 blocks
  ==238825== possibly lost: 48,154 bytes in 14 blocks
  ==238825== still reachable: 42,859 bytes in 288 blocks
  ==238825== suppressed: 0 bytes in 0 blocks

Valgrind stats with the fix:

  ==239413== LEAK SUMMARY:
  ==239413== definitely lost: 40 bytes in 2 blocks
  ==239413== indirectly lost: 0 bytes in 0 blocks
  ==239413== possibly lost: 0 bytes in 0 blocks
  ==239413== still reachable: 38,613 bytes in 287 blocks
  ==239413== suppressed: 0 bytes in 0 blocks

Remaining leaks in scpi_vxi_open() are of lesser severity because they
don't accumulate during execution.

[ gsi: rebase to recent master, reword commit message ]
  • Loading branch information
danselmi authored and gsigh committed Jun 7, 2021
1 parent 7414fb5 commit ed78768
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/scpi/scpi_vxi.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,16 @@ static int scpi_vxi_read_data(void *priv, char *buf, int maxlen)
if (!read_resp || read_resp->error) {
sr_err("Device read failed for %s with error %ld",
vxi->address, read_resp ? read_resp->error : 0);
if (read_resp) {
g_free(read_resp->data.data_val);
read_resp->data.data_val = NULL;
}
return SR_ERR;
}

memcpy(buf, read_resp->data.data_val, read_resp->data.data_len);
g_free(read_resp->data.data_val);
read_resp->data.data_val = NULL;
vxi->read_complete = read_resp->reason & (RRR_TERM | RRR_END);
return read_resp->data.data_len; /* actual number of bytes received */
}
Expand Down

0 comments on commit ed78768

Please sign in to comment.