From: Martin Ling Date: Fri, 22 Nov 2013 18:43:41 +0000 (+0000) Subject: Fix memory leaks. X-Git-Tag: libserialport-0.1.0~73 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8d43110ad72dbc729651101b7cd218a5a7441437;p=libserialport.git Fix memory leaks. --- diff --git a/serialport.c b/serialport.c index 9697b7c..9cef266 100644 --- a/serialport.c +++ b/serialport.c @@ -610,11 +610,15 @@ static enum sp_return get_baudrate(int fd, int *baudrate) if (!(data = malloc(get_termios_size()))) return SP_ERR_MEM; - if (ioctl(fd, get_termios_get_ioctl(), data) < 0) + if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { + free(data); return SP_ERR_FAIL; + } *baudrate = get_termios_speed(data); + free(data); + return SP_OK; } @@ -625,13 +629,19 @@ static enum sp_return set_baudrate(int fd, int baudrate) if (!(data = malloc(get_termios_size()))) return SP_ERR_MEM; - if (ioctl(fd, get_termios_get_ioctl(), data) < 0) + if (ioctl(fd, get_termios_get_ioctl(), data) < 0) { + free(data); return SP_ERR_FAIL; + } set_termios_speed(data, baudrate); - if (ioctl(fd, get_termios_set_ioctl(), data) < 0) + if (ioctl(fd, get_termios_set_ioctl(), data) < 0) { + free(data); return SP_ERR_FAIL; + } + + free(data); return SP_OK; } @@ -644,11 +654,15 @@ static enum sp_return get_flow(int fd, int *flow) if (!(data = malloc(get_termiox_size()))) return SP_ERR_MEM; - if (ioctl(fd, TCGETX, data) < 0) + if (ioctl(fd, TCGETX, data) < 0) { + free(data); return SP_ERR_FAIL; + } *flow = get_termiox_flow(data); + free(data); + return SP_OK; } @@ -659,13 +673,19 @@ static enum sp_return set_flow(int fd, int flow) if (!(data = malloc(get_termiox_size()))) return SP_ERR_MEM; - if (ioctl(fd, TCGETX, data) < 0) + if (ioctl(fd, TCGETX, data) < 0) { + free(data); return SP_ERR_FAIL; + } set_termiox_flow(data, flow); - if (ioctl(fd, TCSETX, data) < 0) + if (ioctl(fd, TCSETX, data) < 0) { + free(data); return SP_ERR_FAIL; + } + + free(data); return SP_OK; }