Commit 0df84466 authored by Stefan Reinauer's avatar Stefan Reinauer Committed by Stefan Tauner
Browse files

CID1129998/1129999: Unchecked return value from library


Check return values of various fcntl() invocations in
serialport_config(), serialport_read_nonblock() and
serialport_write_nonblock().

Also, remove some superfluous print conversion specifiers and refine
messages.

Corresponding to flashrom svn r1803.
Signed-off-by: default avatarStefan Reinauer <stefan.reinauer@coreboot.org>
Acked-by: default avatarStefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
parent 12f3d51a
...@@ -181,7 +181,10 @@ int serialport_config(fdtype fd, unsigned int baud) ...@@ -181,7 +181,10 @@ int serialport_config(fdtype fd, unsigned int baud)
msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate); msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
#else #else
struct termios wanted, observed; struct termios wanted, observed;
fcntl(fd, F_SETFL, 0); if (fcntl(fd, F_SETFL, 0) != 0) {
msg_perr_strerror("Could not clear serial port mode: ");
return 1;
}
if (tcgetattr(fd, &observed) != 0) { if (tcgetattr(fd, &observed) != 0) {
msg_perr_strerror("Could not fetch original serial port configuration: "); msg_perr_strerror("Could not fetch original serial port configuration: ");
return 1; return 1;
...@@ -419,14 +422,21 @@ int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in ...@@ -419,14 +422,21 @@ int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in
return -1; return -1;
} }
if(!SetCommTimeouts(sp_fd, &newTimeout)) { if(!SetCommTimeouts(sp_fd, &newTimeout)) {
msg_perr_strerror("Could not set serial port timeout settings: ");
return -1;
}
#else #else
ssize_t rv; ssize_t rv;
const int flags = fcntl(sp_fd, F_GETFL); const int flags = fcntl(sp_fd, F_GETFL);
if (flags == -1) {
msg_perr_strerror("Could not get serial port mode: ");
return -1;
}
if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) { if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
#endif msg_perr_strerror("Could not set serial port mode to non-blocking: ");
msg_perr_strerror("Could not set serial port timeout settings %s");
return -1; return -1;
} }
#endif
int i; int i;
int rd_bytes = 0; int rd_bytes = 0;
...@@ -458,12 +468,15 @@ int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in ...@@ -458,12 +468,15 @@ int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in
/* restore original blocking behavior */ /* restore original blocking behavior */
#ifdef _WIN32 #ifdef _WIN32
if (!SetCommTimeouts(sp_fd, &oldTimeout)) { if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
msg_perr_strerror("Could not restore serial port timeout settings: ");
ret = -1;
}
#else #else
if (fcntl(sp_fd, F_SETFL, flags) != 0) { if (fcntl(sp_fd, F_SETFL, flags) != 0) {
#endif msg_perr_strerror("Could not restore serial port mode to blocking: ");
msg_perr_strerror("Could not restore serial port timeout settings: %s\n");
ret = -1; ret = -1;
} }
#endif
return ret; return ret;
} }
...@@ -495,7 +508,14 @@ int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u ...@@ -495,7 +508,14 @@ int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u
#else #else
ssize_t rv; ssize_t rv;
const int flags = fcntl(sp_fd, F_GETFL); const int flags = fcntl(sp_fd, F_GETFL);
fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK); if (flags == -1) {
msg_perr_strerror("Could not get serial port mode: ");
return -1;
}
if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
msg_perr_strerror("Could not set serial port mode to non-blocking: ");
return -1;
}
#endif #endif
int i; int i;
...@@ -531,10 +551,13 @@ int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u ...@@ -531,10 +551,13 @@ int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u
#ifdef _WIN32 #ifdef _WIN32
if (!SetCommTimeouts(sp_fd, &oldTimeout)) { if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
msg_perr_strerror("Could not restore serial port timeout settings: "); msg_perr_strerror("Could not restore serial port timeout settings: ");
return -1;
}
#else #else
if (fcntl(sp_fd, F_SETFL, flags) != 0) { if (fcntl(sp_fd, F_SETFL, flags) != 0) {
#endif msg_perr_strerror("Could not restore serial port blocking behavior: ");
return -1; return -1;
} }
#endif
return ret; return ret;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment