diff options
| author | Keith Packard <keithp@keithp.com> | 2010-07-29 10:45:02 -0700 | 
|---|---|---|
| committer | Keith Packard <keithp@keithp.com> | 2010-07-29 10:45:02 -0700 | 
| commit | 53c279b9e96da8b69837ae84038a78ca5707f2a5 (patch) | |
| tree | 5d843f5c8f17e99840ead4ebc9dcf046c9f4cd8b /ao-tools/libaltos/libaltos.c | |
| parent | b8bc9994d8bfde6116c8a509e70ddf45fc4decce (diff) | |
altosui: Close serial, join reader thread, free altos_file
Separating out the close and free actions ensures that the reader thread will not
access freed memory or dereference a null pointer while shutting down the
connection to the serial device. Otherwise, a race condition exists between the
serial close and the thread join.
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'ao-tools/libaltos/libaltos.c')
| -rw-r--r-- | ao-tools/libaltos/libaltos.c | 12 | 
1 files changed, 12 insertions, 0 deletions
| diff --git a/ao-tools/libaltos/libaltos.c b/ao-tools/libaltos/libaltos.c index df0d5b2e..00fb2125 100644 --- a/ao-tools/libaltos/libaltos.c +++ b/ao-tools/libaltos/libaltos.c @@ -567,6 +567,14 @@ void  altos_close(struct altos_file *file)  {  	close(file->fd); +	file->fd = -1; +} + +void +altos_free(struct altos_file *file) +{ +	if (file->fd != -1) +		close(file->fd);  	free(file);  } @@ -592,6 +600,8 @@ altos_flush(struct altos_file *file)  	while (file->out_used) {  		int	ret; +		if (file->fd < 0) +			return -EBADF;  		ret = write (file->fd, file->out_data, file->out_used);  		if (ret < 0)  			return -errno; @@ -610,6 +620,8 @@ altos_getchar(struct altos_file *file, int timeout)  		int	ret;  		altos_flush(file); +		if (file->fd < 0) +			return -EBADF;  		ret = read(file->fd, file->in_data, USB_BUF_SIZE);  		if (ret < 0)  			return -errno; | 
