From 9734f943cb5e787d88c42993d3970f760dfcab8c Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 18:22:14 -0600 Subject: [PATCH] Fixing bad C++ constructors. I am not sure if this is related to the random crashes on Linux, but the wrong C++ std::string constructors were being called because of bad use of c-style array dereferencing. The correct C++ std::string constructors are being called now. --- include/serial/serial.h | 2 +- src/impl/unix.cc | 8 ++++---- src/serial.cc | 33 ++++++++++++++++++++------------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index 1f73fdf..7d9fff9 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -78,7 +78,7 @@ typedef enum { */ typedef enum { flowcontrol_none = 0, - flowcontrol_software, + flowcontrol_software } flowcontrol_t; /*! diff --git a/src/impl/unix.cc b/src/impl/unix.cc index b8038a6..f9785e5 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -230,14 +230,14 @@ Serial::SerialImpl::reconfigurePort () // Linux Support #elif defined(__linux__) struct serial_struct ser; - ioctl(fd_, TIOCGSERIAL, &ser); + ioctl (fd_, TIOCGSERIAL, &ser); // set custom divisor ser.custom_divisor = ser.baud_base / baudrate_; // update flags ser.flags &= ~ASYNC_SPD_MASK; ser.flags |= ASYNC_SPD_CUST; - if (ioctl(fd_, TIOCSSERIAL, ser) < 0) + if (ioctl (fd_, TIOCSSERIAL, ser) < 0) { throw IOException (errno); } @@ -420,7 +420,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) // Calculate the time select took struct timeval diff; diff.tv_sec = end.tv_sec-start.tv_sec; - diff.tv_usec = (end.tv_nsec-start.tv_nsec)/1000; + diff.tv_usec = static_cast ((end.tv_nsec-start.tv_nsec)/1000); // Update the timeout if (timeout.tv_sec <= diff.tv_sec) { timeout.tv_sec = 0; @@ -703,7 +703,7 @@ Serial::SerialImpl::getDSR() { throw PortNotOpenedException ("Serial::getDSR"); } - int s = ioctl(fd_, TIOCMGET, 0); + int s = ioctl (fd_, TIOCMGET, 0); return (s & TIOCM_DSR) != 0; } diff --git a/src/serial.cc b/src/serial.cc index 462e000..b236baf 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -132,17 +132,19 @@ size_t Serial::readline (string &buffer, size_t size, string eol) { ScopedReadLock (this->pimpl_); - size_t eol_len = eol.length(); - unsigned char buffer_[size]; + size_t eol_len = eol.length (); + unsigned char *buffer_ = static_cast + (alloca (size * sizeof (unsigned char))); size_t read_so_far = 0; while (true) { - size_t bytes_read = this->read_ (buffer_+read_so_far, 1); + size_t bytes_read = this->read_ (buffer_ + read_so_far, 1); read_so_far += bytes_read; if (bytes_read == 0) { break; // Timeout occured on reading 1 byte } - if (string(buffer_[read_so_far-eol_len], eol_len) == eol) { + if (string (reinterpret_cast + (buffer_ + read_so_far - eol_len), eol_len) == eol) { break; // EOL found } if (read_so_far == size) { @@ -165,8 +167,9 @@ Serial::readlines (size_t size, string eol) { ScopedReadLock (this->pimpl_); std::vector lines; - size_t eol_len = eol.length(); - unsigned char buffer_[size]; + size_t eol_len = eol.length (); + unsigned char *buffer_ = static_cast + (alloca (size * sizeof (unsigned char))); size_t read_so_far = 0; size_t start_of_line = 0; while (read_so_far < size) { @@ -174,21 +177,25 @@ Serial::readlines (size_t size, string eol) read_so_far += bytes_read; if (bytes_read == 0) { if (start_of_line != read_so_far) { - lines.push_back( - std::string(buffer_[start_of_line], read_so_far-start_of_line)); + lines.push_back ( + string (reinterpret_cast (buffer_ + start_of_line), + read_so_far - start_of_line)); } break; // Timeout occured on reading 1 byte } - if (string(buffer_[read_so_far-eol_len], eol_len) == eol) { + if (string (reinterpret_cast + (buffer_ + read_so_far - eol_len), eol_len) == eol) { // EOL found lines.push_back( - std::string(buffer_[start_of_line], read_so_far-start_of_line)); + string(reinterpret_cast (buffer_ + start_of_line), + read_so_far - start_of_line)); start_of_line = read_so_far; } if (read_so_far == size) { if (start_of_line != read_so_far) { lines.push_back( - std::string(buffer_[start_of_line], read_so_far-start_of_line)); + string(reinterpret_cast (buffer_ + start_of_line), + read_so_far - start_of_line)); } break; // Reached the maximum read length } @@ -208,10 +215,10 @@ Serial::setPort (const string &port) { ScopedReadLock(this->pimpl_); ScopedWriteLock(this->pimpl_); - bool was_open = pimpl_->isOpen(); + bool was_open = pimpl_->isOpen (); if (was_open) close(); pimpl_->setPort (port); - if (was_open) open(); + if (was_open) open (); } string