Skip to content

Commit

Permalink
fix some coverity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkscheper committed Feb 4, 2018
1 parent cfabb51 commit 1122395
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 65 deletions.
26 changes: 19 additions & 7 deletions sw/airborne/arch/linux/udp_socket.c
Expand Up @@ -78,15 +78,24 @@ int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port
sock->sockfd = socket(PF_INET, SOCK_DGRAM, 0);
int one = 1;
// Enable reusing of address
setsockopt(sock->sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if(setsockopt(sock->sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
{
return -1;
}
#ifdef SO_REUSEPORT
// needed for OSX
setsockopt(sock->sockfd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
if(setsockopt(sock->sockfd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)))
{
return -1;
}
#endif

// Enable broadcasting
if (broadcast) {
setsockopt(sock->sockfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
if(setsockopt(sock->sockfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)))
{
return -1;
}
}

// if an input port was specified, bind to it
Expand All @@ -96,7 +105,10 @@ int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port
sock->addr_in.sin_port = htons(port_in);
sock->addr_in.sin_addr.s_addr = htonl(INADDR_ANY);

bind(sock->sockfd, (struct sockaddr *)&sock->addr_in, sizeof(sock->addr_in));
if(bind(sock->sockfd, (struct sockaddr *)&sock->addr_in, sizeof(sock->addr_in)))
{
return -1;
}
}

// set the output/destination address for use in sendto later
Expand Down Expand Up @@ -222,10 +234,10 @@ int udp_socket_set_sendbuf(struct UdpSocket *sock, int buf_size)
// Set and check
unsigned int optval_size = 4;
int buf_ret;
setsockopt(sock->sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, optval_size);
getsockopt(sock->sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&buf_ret, &optval_size);

if (buf_size != buf_ret) {
if (setsockopt(sock->sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, optval_size) ||
getsockopt(sock->sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&buf_ret, &optval_size) ||
buf_size != buf_ret) {
return -1;
}

Expand Down
1 change: 1 addition & 0 deletions sw/airborne/arch/sim/mcu_periph/rng_arch.c
Expand Up @@ -50,6 +50,7 @@ bool rng_get(uint32_t *rand_nr)
if (res != len) {
printf("Error on reading, expected %u bytes, got %u bytes\n",
len, res);
close(fd);
return false;
}
close(fd);
Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/modules/computer_vision/lib/encoding/rtp.c
Expand Up @@ -253,5 +253,5 @@ static void rtp_packet_send(
// append the JPEG scan data to the RTP buffer
memcpy(&RtpBuf[20], Jpeg, JpegLen);

udp_socket_send_dontwait(udp, RtpBuf, RtpPacketSize);
(void)udp_socket_send_dontwait(udp, RtpBuf, RtpPacketSize);
};
1 change: 1 addition & 0 deletions sw/airborne/modules/computer_vision/lib/v4l/v4l2.c
Expand Up @@ -231,6 +231,7 @@ struct v4l2_device *v4l2_init(char *device_name, struct img_size_t size, struct
// Accept if no format can be get
if(fmtdesc.index != 0 && fmtdesc.pixelformat != _pixelformat) {
printf("[v4l2] Pixelformat not available on device %s (wanted: %4X)\r\n", device_name, _pixelformat);
close(fd);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions sw/airborne/modules/computer_vision/lib/vision/image.c
Expand Up @@ -578,8 +578,8 @@ void image_show_points_color(struct image_t *img, struct point_t *points, uint16
uint8_t *img_buf = (uint8_t *)img->buf;
uint8_t pixel_width = (img->type == IMAGE_YUV422) ? 2 : 1;

int cross_hair = 1;
int size_crosshair = 5;
static const int cross_hair = 1;
static const int size_crosshair = 5;

// Go trough all points and color them
for (int i = 0; i < points_cnt; i++) {
Expand Down
6 changes: 3 additions & 3 deletions sw/airborne/modules/computer_vision/lib/vision/lucas_kanade.c
Expand Up @@ -224,12 +224,12 @@ struct flow_t *opticFlowLK(struct image_t *new_img, struct image_t *old_img, str
image_free(&window_DY);
image_free(&window_diff);

for (int8_t i = pyramid_level; i != -1; i--) {
for (int8_t i = 0; i < pyramid_level; i++) {
image_free(&pyramid_old[i]);
image_free(&pyramid_new[i]);
}
pyramid_old = NULL;
pyramid_new = NULL;
free(pyramid_old);
free(pyramid_new);

// Return the vectors
return vectors;
Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/modules/loggers/sdlog_chibios/sdLog.c
Expand Up @@ -355,7 +355,7 @@ SdioError sdLogCloseAllLogs(bool flush)
// queue flush + close order, then stop worker thread
for (FileDes fd = 0; fd < SDLOG_NUM_FILES; fd++) {
if (fileDes[fd].inUse) {
flushWriteByteBuffer(fd);
(void)flushWriteByteBuffer(fd);
sdLogCloseLog(fd);
}
}
Expand Down
16 changes: 10 additions & 6 deletions sw/airborne/modules/loggers/sdlog_chibios/usb_msd.c
Expand Up @@ -128,7 +128,8 @@ PACK_STRUCT_BEGIN typedef struct {
/**
* @brief Read-write buffers (TODO: need a way of specifying the size of this)
*/
static uint8_t IN_DMA_SECTION_CLEAR(rw_buf[2][512]);
#define RW_BUF_SIZE 512
static uint8_t IN_DMA_SECTION_CLEAR(rw_buf[2][RW_BUF_SIZE]);

typedef uint32_t DWORD __attribute__((__may_alias__));;
typedef uint16_t WORD __attribute__((__may_alias__));
Expand Down Expand Up @@ -231,11 +232,11 @@ bool msdRequestsHook(USBDriver *usbp)
CBW from the host.
To generate the BOT Mass Storage Reset, the host must send a device request on the
default pipe of:
bmRequestType: Class, interface, host to device
bRequest field set to 255 (FFh)
wValue field set to ‘0’
wIndex field set to the interface number
wLength field set to ‘0’
bmRequestType: Class, interface, host to device
bRequest field set to 255 (FFh)
wValue field set to �0�
wIndex field set to the interface number
wLength field set to �0�
*/
UMSD.bot_reset = true;
chSysLockFromISR();
Expand Down Expand Up @@ -466,6 +467,9 @@ bool msd_scsi_process_start_read_write_10(USBMassStorageDriver *msdp)

uint32_t rw_block_address = swap_uint32(*(DWORD *)&cbw->scsi_cmd_data[2]);
uint16_t total = swap_uint16(*(WORD *)&cbw->scsi_cmd_data[7]);
if (total > RW_BUF_SIZE){
total = RW_BUF_SIZE;
}
uint16_t i = 0;

if (rw_block_address >= msdp->block_dev_info.blk_num) {
Expand Down
78 changes: 39 additions & 39 deletions sw/airborne/modules/nav/nav_survey_polygon.c
Expand Up @@ -136,10 +136,10 @@ void nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float
{
int i;
struct FloatVect2 small, sweep;
float divider, angle_rad = angle / 180.0 * M_PI;
float divider, angle_rad = angle / 180.f * M_PI;

if (angle < 0.0) { angle += 360.0; }
if (angle >= 360.0) { angle -= 360.0; }
if (angle < 0.f) { angle += 360.f; }
if (angle >= 360.f) { angle -= 360.f; }

survey.poly_first = first_wp;
survey.poly_count = size;
Expand All @@ -153,29 +153,29 @@ void nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float
survey.return_angle = angle + 180;
if (survey.return_angle > 359) { survey.return_angle -= 360; }

if (angle <= 45.0 || angle >= 315.0) {
if (angle <= 45.f || angle >= 315.f) {
//north
survey.dir_vec.y = 1.0;
survey.dir_vec.x = 1.0 * tanf(angle_rad);
sweep.x = 1.0;
survey.dir_vec.y = 1.f;
survey.dir_vec.x = 1.f * tanf(angle_rad);
sweep.x = 1.f;
sweep.y = - survey.dir_vec.x / survey.dir_vec.y;
} else if (angle <= 135.0) {
} else if (angle <= 135.f) {
//east
survey.dir_vec.x = 1.0;
survey.dir_vec.y = 1.0 / tanf(angle_rad);
sweep.y = - 1.0;
survey.dir_vec.x = 1.f;
survey.dir_vec.y = 1.f / tanf(angle_rad);
sweep.y = - 1.f;
sweep.x = survey.dir_vec.y / survey.dir_vec.x;
} else if (angle <= 225.0) {
} else if (angle <= 225.f) {
//south
survey.dir_vec.y = -1.0;
survey.dir_vec.x = -1.0 * tanf(angle_rad);
sweep.x = -1.0;
survey.dir_vec.y = -1.f;
survey.dir_vec.x = -1.f * tanf(angle_rad);
sweep.x = -1.f;
sweep.y = survey.dir_vec.x / survey.dir_vec.y;
} else {
//west
survey.dir_vec.x = -1.0;
survey.dir_vec.y = -1.0 / tanf(angle_rad);
sweep.y = 1.0;
survey.dir_vec.x = -1.f;
survey.dir_vec.y = -1.f / tanf(angle_rad);
sweep.y = 1.f;
sweep.x = - survey.dir_vec.y / survey.dir_vec.x;
}

Expand All @@ -191,22 +191,22 @@ void nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float
divider = (survey.sweep_vec.y * survey.dir_vec.x) - (survey.sweep_vec.x * survey.dir_vec.y);

//calculate the leftmost point if one sees the dir vec as going "up" and the sweep vec as going right
if (divider < 0.0) {
if (divider < 0.f) {
for (i = 1; i < survey.poly_count; i++)
if ((survey.dir_vec.x * (waypoints[survey.poly_first + i].y - small.y)) + (survey.dir_vec.y *
(small.x - waypoints[survey.poly_first + i].x)) > 0.0) {
(small.x - waypoints[survey.poly_first + i].x)) > 0.f) {
VECT2_COPY(small, waypoints[survey.poly_first + i]);
}
} else
for (i = 1; i < survey.poly_count; i++)
if ((survey.dir_vec.x * (waypoints[survey.poly_first + i].y - small.y)) + (survey.dir_vec.y *
(small.x - waypoints[survey.poly_first + i].x)) > 0.0) {
(small.x - waypoints[survey.poly_first + i].x)) > 0.f) {
VECT2_COPY(small, waypoints[survey.poly_first + i]);
}

//calculate the line the defines the first flyover
survey.seg_start.x = small.x + 0.5 * survey.sweep_vec.x;
survey.seg_start.y = small.y + 0.5 * survey.sweep_vec.y;
survey.seg_start.x = small.x + 0.5f * survey.sweep_vec.x;
survey.seg_start.y = small.y + 0.5f * survey.sweep_vec.y;
VECT2_SUM(survey.seg_end, survey.seg_start, survey.dir_vec);

if (!get_two_intersects(&survey.seg_start, &survey.seg_end, survey.seg_start, survey.seg_end)) {
Expand All @@ -219,7 +219,7 @@ void nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float

//fast climbing to desired altitude
NavVerticalAutoThrottleMode(0.0);
NavVerticalAltitudeMode(survey.psa_altitude, 0.0);
NavVerticalAltitudeMode(survey.psa_altitude, 0.f);

survey.stage = ENTRY;
}
Expand All @@ -231,8 +231,8 @@ void nav_survey_polygon_setup(uint8_t first_wp, uint8_t size, float angle, float
*/
bool nav_survey_polygon_run(void)
{
NavVerticalAutoThrottleMode(0.0);
NavVerticalAltitudeMode(survey.psa_altitude, 0.0);
NavVerticalAutoThrottleMode(0.f);
NavVerticalAltitudeMode(survey.psa_altitude, 0.f);

//entry circle around entry-center until the desired altitude is reached
if (survey.stage == ENTRY) {
Expand All @@ -243,22 +243,22 @@ bool nav_survey_polygon_run(void)
survey.stage = SEG;
nav_init_stage();
#ifdef DIGITAL_CAM
dc_survey(survey.psa_shot_dist, survey.seg_start.x - survey.dir_vec.x * survey.psa_shot_dist * 0.5,
survey.seg_start.y - survey.dir_vec.y * survey.psa_shot_dist * 0.5);
dc_survey(survey.psa_shot_dist, survey.seg_start.x - survey.dir_vec.x * survey.psa_shot_dist * 0.5f,
survey.seg_start.y - survey.dir_vec.y * survey.psa_shot_dist * 0.5f);
#endif
}
}
//fly the segment until seg_end is reached
if (survey.stage == SEG) {
nav_points(survey.seg_start, survey.seg_end);
//calculate all needed points for the next flyover
if (nav_approaching_xy(survey.seg_end.x, survey.seg_end.y, survey.seg_start.x, survey.seg_start.y, 0)) {
if (nav_approaching_xy(survey.seg_end.x, survey.seg_end.y, survey.seg_start.x, survey.seg_start.y, 0.f)) {
#ifdef DIGITAL_CAM
dc_stop();
#endif
VECT2_DIFF(survey.seg_center1, survey.seg_end, survey.rad_vec);
survey.ret_start.x = survey.seg_end.x - 2 * survey.rad_vec.x;
survey.ret_start.y = survey.seg_end.y - 2 * survey.rad_vec.y;
survey.ret_start.x = survey.seg_end.x - 2.f * survey.rad_vec.x;
survey.ret_start.y = survey.seg_end.y - 2.f * survey.rad_vec.y;

//if we get no intersection the survey is finished
static struct FloatVect2 sum_start_sweep;
Expand All @@ -269,11 +269,11 @@ bool nav_survey_polygon_run(void)
return false;
}

survey.ret_end.x = survey.seg_start.x - survey.sweep_vec.x - 2 * survey.rad_vec.x;
survey.ret_end.y = survey.seg_start.y - survey.sweep_vec.y - 2 * survey.rad_vec.y;
survey.ret_end.x = survey.seg_start.x - survey.sweep_vec.x - 2.f * survey.rad_vec.x;
survey.ret_end.y = survey.seg_start.y - survey.sweep_vec.y - 2.f * survey.rad_vec.y;

survey.seg_center2.x = survey.seg_start.x - 0.5 * (2.0 * survey.rad_vec.x + survey.sweep_vec.x);
survey.seg_center2.y = survey.seg_start.y - 0.5 * (2.0 * survey.rad_vec.y + survey.sweep_vec.y);
survey.seg_center2.x = survey.seg_start.x - 0.f * (2.f * survey.rad_vec.x + survey.sweep_vec.x);
survey.seg_center2.y = survey.seg_start.y - 0.f * (2.f * survey.rad_vec.y + survey.sweep_vec.y);

survey.stage = TURN1;
nav_init_stage();
Expand All @@ -289,19 +289,19 @@ bool nav_survey_polygon_run(void)
//return
} else if (survey.stage == RET) {
nav_points(survey.ret_start, survey.ret_end);
if (nav_approaching_xy(survey.ret_end.x, survey.ret_end.y, survey.ret_start.x, survey.ret_start.y, 0)) {
if (nav_approaching_xy(survey.ret_end.x, survey.ret_end.y, survey.ret_start.x, survey.ret_start.y, 0.f)) {
survey.stage = TURN2;
nav_init_stage();
}
//turn from return to stage
} else if (survey.stage == TURN2) {
nav_circle_XY(survey.seg_center2.x, survey.seg_center2.y, -(2 * survey.psa_min_rad + survey.psa_sweep_width) * 0.5);
nav_circle_XY(survey.seg_center2.x, survey.seg_center2.y, -(2.f * survey.psa_min_rad + survey.psa_sweep_width) * 0.5f);
if (NavCourseCloseTo(survey.segment_angle)) {
survey.stage = SEG;
nav_init_stage();
#ifdef DIGITAL_CAM
dc_survey(survey.psa_shot_dist, survey.seg_start.x - survey.dir_vec.x * survey.psa_shot_dist * 0.5,
survey.seg_start.y - survey.dir_vec.y * survey.psa_shot_dist * 0.5);
dc_survey(survey.psa_shot_dist, survey.seg_start.x - survey.dir_vec.x * survey.psa_shot_dist * 0.5f,
survey.seg_start.y - survey.dir_vec.y * survey.psa_shot_dist * 0.5f);
#endif
}
}
Expand Down
8 changes: 5 additions & 3 deletions sw/airborne/modules/pose_history/pose_history.c
Expand Up @@ -69,9 +69,11 @@ struct pose_t get_rotation_at_timestamp(uint32_t timestamp)
}

// Save the pose closest to the given timestamp and return this
struct pose_t closest_pose;
closest_pose.eulers = location_history.ring_data[closestIndex].eulers;
closest_pose.rates = location_history.ring_data[closestIndex].rates;
struct pose_t closest_pose = {
.timestamp = location_history.ring_data[closestIndex].timestamp,
.eulers = location_history.ring_data[closestIndex].eulers,
.rates = location_history.ring_data[closestIndex].rates
};

#ifdef __linux__
pthread_mutex_unlock(&pose_mutex);
Expand Down
1 change: 1 addition & 0 deletions sw/airborne/subsystems/actuators/actuators_asctec_v2_new.c
Expand Up @@ -151,6 +151,7 @@ void actuators_asctec_v2_set(void)

i2c_transmit(&ACTUATORS_ASCTEC_V2_I2C_DEV, &actuators_asctec_v2.i2c_trans, ACTUATORS_ASCTEC_V2_SLAVE_ADDR, 4);
set_addr++;
/* fall through */
default:
set_addr = 0;
actuators_asctec_v2.cmd = NONE;
Expand Down
3 changes: 3 additions & 0 deletions sw/simulator/nps/nps_main_hitl.c
Expand Up @@ -245,6 +245,9 @@ void *nps_ap_data_loop(void *data __attribute__((unused)))
case DL_COMMANDS:
// parse commands message
cmd_len = DL_COMMANDS_values_length(buf);
if (cmd_len > NPS_COMMANDS_NB) {
cmd_len = NPS_COMMANDS_NB;
}
memcpy(&cmd_buf, DL_COMMANDS_values(buf), cmd_len * sizeof(int16_t));
pthread_mutex_lock(&fdm_mutex);
// update commands
Expand Down
9 changes: 6 additions & 3 deletions sw/simulator/nps/nps_sensors_utils.c
Expand Up @@ -21,7 +21,9 @@ void UpdateSensorLatency(double time, gpointer cur_reading, GSList **history, do
}
/* update sensor */
//g_memmove((gpointer)sensor_reading, (gpointer)((struct BoozDatedSensor*)last->data)->value, sizeof(struct DoubleVect3));
VECT3_COPY(*((struct DoubleVect3 *)sensor_reading), *((struct BoozDatedSensor *)last->data)->value);
if (last) {
VECT3_COPY(*((struct DoubleVect3 *)sensor_reading), *((struct BoozDatedSensor *)last->data)->value);
}
}

void UpdateSensorLatency_Single(double time, gpointer cur_reading, GSList **history, double latency,
Expand All @@ -44,6 +46,7 @@ void UpdateSensorLatency_Single(double time, gpointer cur_reading, GSList **hist

/* update sensor */
//g_memmove((gpointer)sensor_reading, (gpointer)((struct BoozDatedSensor*)last->data)->value, sizeof(struct DoubleVect3));
*((double *)sensor_reading) = *(((struct BoozDatedSensor_Single *)last->data)->value);

if(last) {
*((double *)sensor_reading) = *(((struct BoozDatedSensor_Single *)last->data)->value);
}
}

0 comments on commit 1122395

Please sign in to comment.