Skip to content

Commit

Permalink
Get rid of errors reported by static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Aug 14, 2021
1 parent 7d2a440 commit aac8742
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/asound/bluealsa-ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ static void bluealsa_elem_set_name(struct ctl_elem *elem, const char *name, int

const int name_len = strlen(name);
int len = sizeof(elem->name) - 16 - 1;
char no[8] = "";
char no[16] = "";

if (id != -1) {
sprintf(no, " #%u", id);
Expand Down Expand Up @@ -740,9 +740,9 @@ static DBusHandlerResult bluealsa_dbus_msg_filter(DBusConnection *conn,
}

if (strcmp(signal, "PCMRemoved") == 0) {
const char *path;
dbus_message_iter_get_basic(&iter, &path);
bluealsa_pcm_remove(ctl, path);
const char *pcm_path;
dbus_message_iter_get_basic(&iter, &pcm_path);
bluealsa_pcm_remove(ctl, pcm_path);
goto remove_add;
}

Expand Down
23 changes: 12 additions & 11 deletions src/at.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - at.c
* Copyright (c) 2016-2019 Arkadiusz Bokowy
* Copyright (c) 2016-2021 Arkadiusz Bokowy
* 2017 Juha Kuikka
*
* This file is a part of bluez-alsa.
Expand All @@ -24,37 +24,38 @@
/**
* Build AT message.
*
* @param buffer Address of the buffer, which shall be big enough to contain
* @param buffer Address of the buffer, which should be big enough to contain
* AT message: len(command) + len(value) + 6 bytes.
* @param buffer The size of the buffer.
* @param type AT message type.
* @param command AT command. If one wants to build unsolicited response code,
* this parameter should be set to NULL, otherwise AT command response will
* be build.
* @param value AT command value or unsolicited response code.
* @return Pointer to the destination buffer. */
char *at_build(char *buffer, enum bt_at_type type, const char *command,
const char *value) {
char *at_build(char *buffer, size_t size, enum bt_at_type type,
const char *command, const char *value) {
switch (type) {
case AT_TYPE_RAW:
strcpy(buffer, command);
snprintf(buffer, size, "%s", command);
break;
case AT_TYPE_CMD:
sprintf(buffer, "AT%s\r", command);
snprintf(buffer, size, "AT%s\r", command);
break;
case AT_TYPE_CMD_GET:
sprintf(buffer, "AT%s?\r", command);
snprintf(buffer, size, "AT%s?\r", command);
break;
case AT_TYPE_CMD_SET:
sprintf(buffer, "AT%s=%s\r", command, value);
snprintf(buffer, size, "AT%s=%s\r", command, value);
break;
case AT_TYPE_CMD_TEST:
sprintf(buffer, "AT%s=?\r", command);
snprintf(buffer, size, "AT%s=?\r", command);
break;
case AT_TYPE_RESP:
if (command != NULL)
sprintf(buffer, "\r\n%s:%s\r\n", command, value);
snprintf(buffer, size, "\r\n%s:%s\r\n", command, value);
else
sprintf(buffer, "\r\n%s\r\n", value);
snprintf(buffer, size, "\r\n%s\r\n", value);
break;
case __AT_TYPE_MAX:
break;
Expand Down
8 changes: 5 additions & 3 deletions src/at.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - at.c
* Copyright (c) 2016-2019 Arkadiusz Bokowy
* Copyright (c) 2016-2021 Arkadiusz Bokowy
* 2017 Juha Kuikka
*
* This file is a part of bluez-alsa.
Expand All @@ -12,7 +12,9 @@
#ifndef BLUEALSA_AT_H_
#define BLUEALSA_AT_H_

#pragma once
#include <stdbool.h>
#include <stddef.h>

#include "hfp.h"

Expand All @@ -32,8 +34,8 @@ struct bt_at {
char *value;
};

char *at_build(char *buffer, enum bt_at_type type, const char *command,
const char *value);
char *at_build(char *buffer, size_t size, enum bt_at_type type,
const char *command, const char *value);
char *at_parse(const char *str, struct bt_at *at);
int at_parse_bia(const char *str, bool state[__HFP_IND_MAX]);
int at_parse_cind(const char *str, enum hfp_ind map[20]);
Expand Down
11 changes: 6 additions & 5 deletions src/ba-rfcomm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - ba-rfcomm.c
* Copyright (c) 2016-2020 Arkadiusz Bokowy
* Copyright (c) 2016-2021 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -107,7 +107,7 @@ static int rfcomm_write_at(int fd, enum bt_at_type type, const char *command,
debug("Sending AT message: %s: command:%s, value:%s",
at_type2str(type), command, value);

at_build(msg, type, command, value);
at_build(msg, sizeof(msg), type, command, value);
len = strlen(msg);

retry:
Expand Down Expand Up @@ -807,7 +807,7 @@ static int rfcomm_notify_volume_change_mic(struct ba_rfcomm *r, bool force) {
struct ba_transport * const t_sco = r->sco;
struct ba_transport_pcm *pcm = &t_sco->sco.mic_pcm;
const int fd = r->fd;
char tmp[16];
char tmp[24];

int gain = ba_transport_pcm_volume_level_to_bt(pcm, pcm->volume[0].level);
if (!force && r->gain_mic == gain)
Expand Down Expand Up @@ -837,7 +837,7 @@ static int rfcomm_notify_volume_change_spk(struct ba_rfcomm *r, bool force) {
struct ba_transport * const t_sco = r->sco;
struct ba_transport_pcm *pcm = &t_sco->sco.spk_pcm;
const int fd = r->fd;
char tmp[16];
char tmp[24];

int gain = ba_transport_pcm_volume_level_to_bt(pcm, pcm->volume[0].level);
if (!force && r->gain_spk == gain)
Expand Down Expand Up @@ -1173,7 +1173,8 @@ static void *rfcomm_thread(struct ba_rfcomm *r) {
callback = rfcomm_get_callback(&reader.at);

if (pfds[2].fd != -1 && !predefined_callback) {
at_build(tmp, reader.at.type, reader.at.command, reader.at.value);
at_build(tmp, sizeof(tmp), reader.at.type,
reader.at.command, reader.at.value);
if (write(pfds[2].fd, tmp, strlen(tmp)) == -1)
warn("Couldn't forward AT: %s", strerror(errno));
}
Expand Down
14 changes: 7 additions & 7 deletions test/test-at.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ START_TEST(test_at_build) {
char buffer[256];

/* build commands */
ck_assert_str_eq(at_build(buffer, AT_TYPE_RAW, "\r\nRING", NULL), "\r\nRING");
ck_assert_str_eq(at_build(buffer, AT_TYPE_CMD, "+CLCC", NULL), "AT+CLCC\r");
ck_assert_str_eq(at_build(buffer, AT_TYPE_CMD_GET, "+COPS", NULL), "AT+COPS?\r");
ck_assert_str_eq(at_build(buffer, AT_TYPE_CMD_SET, "+BCS", "1"), "AT+BCS=1\r");
ck_assert_str_eq(at_build(buffer, AT_TYPE_CMD_TEST, "+CIND", NULL), "AT+CIND=?\r");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_RAW, "\r\nRING", NULL), "\r\nRING");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_CMD, "+CLCC", NULL), "AT+CLCC\r");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_CMD_GET, "+COPS", NULL), "AT+COPS?\r");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_CMD_SET, "+BCS", "1"), "AT+BCS=1\r");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_CMD_TEST, "+CIND", NULL), "AT+CIND=?\r");

/* build response result code */
ck_assert_str_eq(at_build(buffer, AT_TYPE_RESP, "+CIND", ""), "\r\n+CIND:\r\n");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_RESP, "+CIND", ""), "\r\n+CIND:\r\n");

/* build unsolicited result code */
ck_assert_str_eq(at_build(buffer, AT_TYPE_RESP, NULL, "OK"), "\r\nOK\r\n");
ck_assert_str_eq(at_build(buffer, sizeof(buffer), AT_TYPE_RESP, NULL, "OK"), "\r\nOK\r\n");

} END_TEST

Expand Down

0 comments on commit aac8742

Please sign in to comment.