Fixed current and potential bugs caught by Coverity

Change-Id: I9f9d09dc797ffea062eeb49c881be1d5acb63d7b
This commit is contained in:
Tyler Veness
2015-11-15 14:49:50 -08:00
committed by Peter Johnson
parent b0fec4089b
commit 055ee09825
34 changed files with 105 additions and 65 deletions

View File

@@ -88,7 +88,10 @@ AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel)
* significant
* drift in the gyro and it needs to be recalibrated after it has been running.
*/
void AnalogGyro::Reset() { m_analog->ResetAccumulator(); }
void AnalogGyro::Reset() {
if (StatusIsFatal()) return;
m_analog->ResetAccumulator();
}
/**
* {@inheritDoc}
@@ -127,6 +130,8 @@ void AnalogGyro::Calibrate() {
* of the returned rate from the gyro.
*/
float AnalogGyro::GetAngle() const {
if (StatusIsFatal()) return 0.f;
int64_t rawValue;
uint32_t count;
m_analog->GetAccumulatorOutput(rawValue, count);
@@ -148,6 +153,8 @@ float AnalogGyro::GetAngle() const {
* @return the current rate in degrees per second
*/
double AnalogGyro::GetRate() const {
if (StatusIsFatal()) return 0.0;
return (m_analog->GetAverageValue() - ((double)m_center + m_offset)) * 1e-9 *
m_analog->GetLSBWeight() /
((1 << m_analog->GetOversampleBits()) * m_voltsPerDegreePerSecond);
@@ -176,6 +183,8 @@ void AnalogGyro::SetSensitivity(float voltsPerDegreePerSecond) {
* @param volts The size of the deadband in volts
*/
void AnalogGyro::SetDeadband(float volts) {
if (StatusIsFatal()) return;
int32_t deadband = volts * 1e9 / m_analog->GetLSBWeight() *
(1 << m_analog->GetOversampleBits());
m_analog->SetAccumulatorDeadband(deadband);

View File

@@ -10,6 +10,7 @@
#include "Timer.h"
#include "WPIErrors.h"
#include "LiveWindow/LiveWindow.h"
#include "HAL/Port.h"
#include <sstream>
@@ -43,10 +44,11 @@ AnalogInput::AnalogInput(uint32_t channel) {
m_channel = channel;
void *port = getPort(channel);
void* port = getPort(channel);
int32_t status = 0;
m_port = initializeAnalogInputPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
LiveWindow::GetInstance()->AddSensor("AnalogInput", channel, this);
HALReport(HALUsageReporting::kResourceType_AnalogChannel, channel);

View File

@@ -9,7 +9,9 @@
#include "Resource.h"
#include "WPIErrors.h"
#include "LiveWindow/LiveWindow.h"
#include "HAL/Port.h"
#include <limits>
#include <sstream>
static std::unique_ptr<Resource> outputs;
@@ -27,21 +29,26 @@ AnalogOutput::AnalogOutput(uint32_t channel) {
if (!checkAnalogOutputChannel(channel)) {
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
m_channel = std::numeric_limits<uint32_t>::max();
m_port = nullptr;
return;
}
if (outputs->Allocate(channel, buf.str()) ==
std::numeric_limits<uint32_t>::max()) {
CloneError(*outputs);
m_channel = std::numeric_limits<uint32_t>::max();
m_port = nullptr;
return;
}
m_channel = channel;
void *port = getPort(m_channel);
void* port = getPort(m_channel);
int32_t status = 0;
m_port = initializeAnalogOutputPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
LiveWindow::GetInstance()->AddActuator("AnalogOutput", m_channel, this);
HALReport(HALUsageReporting::kResourceType_AnalogOutput, m_channel);

View File

@@ -11,6 +11,8 @@
//#include "NetworkCommunication/UsageReporting.h"
#include "Resource.h"
#include "WPIErrors.h"
#include "HAL/Port.h"
#include <memory>
/**
@@ -20,11 +22,12 @@
* on-board 4-7 are on the MXP port.
*/
AnalogTrigger::AnalogTrigger(int32_t channel) {
void *port = getPort(channel);
void* port = getPort(channel);
int32_t status = 0;
uint32_t index = 0;
m_trigger = initializeAnalogTrigger(port, &index, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
m_index = index;
HALReport(HALUsageReporting::kResourceType_AnalogTrigger, channel);

View File

@@ -149,7 +149,10 @@ unsigned int CameraServer::GetQuality() {
void CameraServer::Serve() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) wpi_setErrnoError();
if (sock == -1) {
wpi_setErrnoError();
return;
}
int reuseAddr = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseAddr,

View File

@@ -11,6 +11,7 @@
#include "WPIErrors.h"
#include "LiveWindow/LiveWindow.h"
#include <limits>
#include <sstream>
/**
@@ -25,6 +26,7 @@ DigitalInput::DigitalInput(uint32_t channel) {
if (!CheckDigitalChannel(channel)) {
buf << "Digital Channel " << channel;
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
m_channel = std::numeric_limits<uint32_t>::max();
return;
}
m_channel = channel;

View File

@@ -9,6 +9,7 @@
#include "Resource.h"
#include "WPIErrors.h"
#include <limits>
#include <sstream>
/**
@@ -21,13 +22,14 @@
DigitalOutput::DigitalOutput(uint32_t channel) {
std::stringstream buf;
m_pwmGenerator = (void *)std::numeric_limits<uint32_t>::max();
if (!CheckDigitalChannel(channel)) {
buf << "Digital Channel " << channel;
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
m_channel = std::numeric_limits<uint32_t>::max();
return;
}
m_channel = channel;
m_pwmGenerator = (void *)std::numeric_limits<uint32_t>::max();
int32_t status = 0;
allocateDIO(m_digital_ports[channel], false, &status);

View File

@@ -107,7 +107,6 @@ void PIDController::Calculate() {
pidInput = m_pidInput;
pidOutput = m_pidOutput;
enabled = m_enabled;
pidInput = m_pidInput;
}
if (pidInput == nullptr) return;

View File

@@ -56,8 +56,10 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
FILE *file = nullptr;
file = fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w");
fputs("2016 C++ Beta2.0", file);
if (file != nullptr) fclose(file);
if (file != nullptr) {
fputs("2016 C++ Beta2.0", file);
fclose(file);
}
}
/**

View File

@@ -10,6 +10,7 @@
#include "NetworkCommunication/LoadOut.h"
#include "WPIErrors.h"
#include "HAL/HAL.hpp"
#include "HAL/Port.h"
const uint32_t SensorBase::kDigitalChannels;
const uint32_t SensorBase::kAnalogInputs;
@@ -36,6 +37,7 @@ SensorBase::SensorBase() {
int32_t status = 0;
m_digital_ports[i] = initializeDigitalPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
}
for (uint32_t i = 0; i < kRelayChannels; i++) {
@@ -43,6 +45,7 @@ SensorBase::SensorBase() {
int32_t status = 0;
m_relay_ports[i] = initializeDigitalPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
}
for (uint32_t i = 0; i < kPwmChannels; i++) {
@@ -50,6 +53,7 @@ SensorBase::SensorBase() {
int32_t status = 0;
m_pwm_ports[i] = initializeDigitalPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
}
}
}

View File

@@ -21,6 +21,7 @@ SolenoidBase::SolenoidBase(uint8_t moduleNumber)
SolenoidBase::m_ports[moduleNumber][i] =
initializeSolenoidPort(port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
freePort(port);
}
}

View File

@@ -126,6 +126,8 @@ Ultrasonic::Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel,
m_counter(m_echoChannel) {
if (pingChannel == nullptr || echoChannel == nullptr) {
wpi_setWPIError(NullParameter);
m_nextSensor = nullptr;
m_units = units;
return;
}
m_units = units;

View File

@@ -117,8 +117,10 @@ HSLImage *AxisCamera::GetImage() {
int AxisCamera::CopyJPEG(char **destImage, unsigned int &destImageSize,
unsigned int &destImageBufferSize) {
std::lock_guard<priority_mutex> lock(m_imageDataMutex);
if (destImage == nullptr)
if (destImage == nullptr) {
wpi_setWPIErrorWithContext(NullParameter, "destImage must not be nullptr");
return 0;
}
if (m_imageData.size() == 0) return 0; // if no source image
@@ -440,6 +442,7 @@ void AxisCamera::ReadImagesFromCamera() {
wpi_setWPIErrorWithContext(IncompatibleMode,
"No content-length token found in packet");
close(m_cameraSocket);
if (imgBuffer) delete[] imgBuffer;
return;
}
contentLength = contentLength + 16; // skip past "content length"
@@ -564,8 +567,10 @@ int AxisCamera::CreateCameraSocket(std::string const &requestString,
}
if (getaddrinfo(m_cameraHost.c_str(), "80", nullptr, &address) == -1) {
if (setError)
if (setError) {
wpi_setErrnoErrorWithContext("Failed to create the camera socket");
close(camSocket);
}
return -1;
}
@@ -573,10 +578,13 @@ int AxisCamera::CreateCameraSocket(std::string const &requestString,
if (connect(camSocket, address->ai_addr, address->ai_addrlen) == -1) {
if (setError)
wpi_setErrnoErrorWithContext("Failed to connect to the camera");
freeaddrinfo(address);
close(camSocket);
return -1;
}
freeaddrinfo(address);
int sent = send(camSocket, requestString.c_str(), requestString.size(), 0);
if (sent == -1) {
if (setError)

View File

@@ -69,6 +69,7 @@ void dprintf(const char *tempString, ...) /* Variable argument list */
for (index = 0; index < tempStringLen; index++) {
if (tempString[index] == ' ') {
printf("ERROR in dprintf: malformed calling sequence (%s)\n", tempString);
va_end(args);
return;
}
if (tempString[index] == '\\' || tempString[index] == '/')
@@ -292,6 +293,7 @@ int processFile(char *inputFile, char *outputString, int lineNumber) {
FILE *infile;
const int stringSize = 80; // max size of one line in file
char inputStr[stringSize];
inputStr[0] = '\0';
int lineCount = 0;
if (lineNumber < 0) return (-1);
@@ -323,14 +325,10 @@ int processFile(char *inputFile, char *outputString, int lineNumber) {
if (lineNumber == 0) return (lineCount);
// check for input out of range
if (lineNumber > lineCount) return (-1);
// return the line selected
if (lineCount) {
stripString(inputStr);
strcpy(outputString, inputStr);
return (lineCount);
} else {
return (-1);
}
// return the line selected; lineCount guaranteed to be greater than zero
stripString(inputStr);
strcpy(outputString, inputStr);
return (lineCount);
}
/** Ignore empty string

View File

@@ -70,6 +70,7 @@ int frcDispose(const char* functionName, ...) /* Variable argument list */
}
disposalPtr = va_arg(disposalPtrList, void*);
}
va_end(disposalPtrList);
return returnValue;
}