Skip to content

Commit

Permalink
libticonv, libtifiles, libticables, libticalcs: apply static analysis…
Browse files Browse the repository at this point in the history
… suggestions.

- Reduce scope of variables
- Use const when possible
- Use nullptr instead of NULL
  • Loading branch information
adriweb authored and debrouxl committed Nov 26, 2023
1 parent 1884397 commit 6f32882
Show file tree
Hide file tree
Showing 75 changed files with 2,132 additions and 3,014 deletions.
3 changes: 1 addition & 2 deletions libticables/trunk/src/data_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@

int log_start(CableHandle *h)
{
gchar *tmp;
int ret;

tmp = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, LOG_DIR, NULL);
gchar* tmp = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, LOG_DIR, NULL);
if (!g_mkdir_with_parents(tmp, 0750))
{
ret = log_hex_start();
Expand Down
7 changes: 3 additions & 4 deletions libticables/trunk/src/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
**/
int TICALL ticables_error_get(int number, char **message)
{
if (message == NULL)
if (message == nullptr)
{
ticables_critical("ticables_error_get(NULL)\n");
return number;
Expand Down Expand Up @@ -341,10 +341,9 @@ int TICALL ticables_error_get(int number, char **message)
#ifndef __WIN32__
if (errno != 0)
{
gchar *str;
char * tmp = *message;

str = g_strdup_printf(" (errno = %i)", errno);
gchar* str = g_strdup_printf(" (errno = %i)", errno);
*message = g_strconcat(tmp, "\n", "System: ", strerror(errno), str, "\n", NULL);
g_free(tmp);
g_free(str);
Expand Down Expand Up @@ -387,7 +386,7 @@ int TICALL ticables_error_get(int number, char **message)
**/
int TICALL ticables_error_free(char *message)
{
if (message == NULL)
if (message == nullptr)
{
ticables_critical("ticables_error_free(NULL)\n");
return ERR_ILLEGAL_ARG;
Expand Down
35 changes: 15 additions & 20 deletions libticables/trunk/src/hex2dbus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int fill_buf(FILE *f, char data, int flush)
{
static char buf[WIDTH];
static unsigned int cnt = 0;
unsigned int i, j;
unsigned int i;

if (!flush)
{
Expand All @@ -116,7 +116,7 @@ static int fill_buf(FILE *f, char data, int flush)

if (flush)
{
for (j = i; j < WIDTH; j++)
for (unsigned int j = i; j < WIDTH; j++)
{
fprintf(f, " ");
}
Expand All @@ -142,16 +142,11 @@ int dbus_decomp(const char *filename, int resync)
{
char src_name[1024];
char dst_name[1024];
FILE *fi = NULL, *fo = NULL;
long file_size;
unsigned char *buffer;
FILE *fi = nullptr, *fo = nullptr;
int i;
unsigned int j;
int num_bytes;
char str[256];
unsigned char mid, cid;
unsigned int length;
int idx;
int ret = 0;

// build filenames
Expand All @@ -163,7 +158,7 @@ int dbus_decomp(const char *filename, int resync)

// open files
fi = fopen(src_name, "rt");
if (fi == NULL)
if (fi == nullptr)
{
fprintf(stderr, "Unable to open input file: %s\n", src_name);
return -1;
Expand All @@ -174,7 +169,7 @@ int dbus_decomp(const char *filename, int resync)
fclose(fi);
return -1;
}
file_size = ftell(fi);
const long file_size = ftell(fi);
if (file_size < 0)
{
fclose(fi);
Expand All @@ -187,8 +182,8 @@ int dbus_decomp(const char *filename, int resync)
}

// allocate buffer
buffer = (unsigned char*)calloc(file_size < 131072 ? 65536 : file_size / 2, 1);
if (buffer == NULL)
unsigned char* buffer = (unsigned char*)calloc(file_size < 131072 ? 65536 : file_size / 2, 1);
if (buffer == nullptr)
{
fprintf(stderr, "calloc error.\n");
fclose(fi);
Expand All @@ -197,7 +192,7 @@ int dbus_decomp(const char *filename, int resync)
memset(buffer, 0xff, file_size/2);

fo = fopen(dst_name, "wt");
if (fo == NULL)
if (fo == nullptr)
{
fprintf(stderr, "Unable to open output file: %s\n", dst_name);
free(buffer);
Expand All @@ -208,9 +203,9 @@ int dbus_decomp(const char *filename, int resync)
fprintf(fo, "TI packet decompiler for D-BUS, version 1.2\n");

// skip comments
if ( fgets(str, sizeof(str), fi) == NULL
|| fgets(str, sizeof(str), fi) == NULL
|| fgets(str, sizeof(str), fi) == NULL)
if ( fgets(str, sizeof(str), fi) == nullptr
|| fgets(str, sizeof(str), fi) == nullptr
|| fgets(str, sizeof(str), fi) == nullptr)
{
goto exit;
}
Expand Down Expand Up @@ -247,9 +242,9 @@ int dbus_decomp(const char *filename, int resync)
for (i = 0; i < num_bytes;)
{
restart:
mid = buffer[i+0];
cid = buffer[i+1];
length = buffer[i+2];
const unsigned char mid = buffer[i + 0];
const unsigned char cid = buffer[i + 1];
unsigned int length = buffer[i + 2];
length |= ((int)(buffer[i+3])) << 8;

// check for valid packet
Expand All @@ -260,7 +255,7 @@ int dbus_decomp(const char *filename, int resync)
}

// check for valid packet
idx = is_a_command_id(cid);
const int idx = is_a_command_id(cid);
if (idx == -1)
{
ret = -2;
Expand Down
39 changes: 16 additions & 23 deletions libticables/trunk/src/hex2dusb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static const Packet packets[] =
{ 0x03, 1, 6, "Virtual Packet Data with Continuation" },
{ 0x04, 1, 6, "Virtual Packet Data Final" },
{ 0x05, 0, 2, "Virtual Packet Data Acknowledgement" },
{ 0x00, 0, 0, NULL }
{ 0x00, 0, 0, nullptr }
};

typedef struct
Expand Down Expand Up @@ -85,7 +85,7 @@ static const Opcode opcodes[] =
{ 0xBB00, "Acknowledgement of Parameter Request"},
{ 0xDD00, "End of Transmission"},
{ 0xEE00, "Error"},
{ 0, NULL }
{ 0, nullptr }
};

/* */
Expand All @@ -107,9 +107,7 @@ static const Opcode opcodes[] =

static const char* name_of_packet(uint8_t id)
{
int i;

for (i=0; packets[i].name; i++)
for (int i = 0; packets[i].name; i++)
{
if (id == packets[i].type)
{
Expand Down Expand Up @@ -155,9 +153,7 @@ static const char* name_of_packet(uint8_t id)

static const char* name_of_data(uint16_t id)
{
unsigned int i;

for (i=0; opcodes[i].name != NULL; i++)
for (unsigned int i = 0; opcodes[i].name != nullptr; i++)
{
if (id == opcodes[i].type)
{
Expand Down Expand Up @@ -255,21 +251,20 @@ static int add_data_code(uint16_t code)

/* */

static FILE *hex = NULL;
static FILE *logfile = NULL;
static FILE *hex = nullptr;
static FILE *logfile = nullptr;

static int hex_read(unsigned char *data)
{
static int idx = 0;
int ret;
int data2;

if (feof(hex))
{
return -1;
}

ret = fscanf(hex, "%02X", &data2);
const int ret = fscanf(hex, "%02X", &data2);
if (ret < 1)
{
return -1;
Expand All @@ -283,10 +278,8 @@ static int hex_read(unsigned char *data)

if (idx >= 16)
{
int i;

idx = 0;
for (i = 0; (i < 67-49) && !feof(hex); i++)
for (int i = 0; (i < 67-49) && !feof(hex); i++)
{
if (fgetc(hex) < 0)
{
Expand All @@ -310,7 +303,7 @@ static int dusb_write(int dir, uint8_t data)
static int cnt;
static int first = 1;

if (logfile == NULL)
if (logfile == nullptr)
{
return -1;
}
Expand Down Expand Up @@ -340,7 +333,7 @@ static int dusb_write(int dir, uint8_t data)
case 7:
if (raw_type == 5)
{
uint16_t tmp = (((uint32_t)(array[5])) << 8) | ((uint32_t)(array[6]));
const uint16_t tmp = (((uint32_t)(array[5])) << 8) | ((uint32_t)(array[6]));
fprintf(logfile, "\t[%04x]\n", tmp);
state = 0;
}
Expand All @@ -349,7 +342,7 @@ static int dusb_write(int dir, uint8_t data)
case 9:
if (raw_type == 1 || raw_type == 2)
{
uint32_t tmp = (((uint32_t)(array[5])) << 24) | (((uint32_t)(array[6])) << 16) | (((uint32_t)(array[7])) << 8) | ((uint32_t)(array[8]));
const uint32_t tmp = (((uint32_t)(array[5])) << 24) | (((uint32_t)(array[6])) << 16) | (((uint32_t)(array[7])) << 8) | ((uint32_t)(array[8]));
fprintf(logfile, "\t[%08x]\n", (unsigned int)tmp);
state = 0;
}
Expand Down Expand Up @@ -429,14 +422,14 @@ int dusb_decomp(const char *filename)
dst_name[sizeof(dst_name) - 1] = 0;

hex = fopen(src_name, "rt");
if (hex == NULL)
if (hex == nullptr)
{
fprintf(stderr, "Unable to open input file: %s\n", src_name);
return -1;
}

logfile = fopen(dst_name, "wt");
if (logfile == NULL)
if (logfile == nullptr)
{
fprintf(stderr, "Unable to open output file: %s\n", dst_name);
fclose(hex);
Expand All @@ -446,9 +439,9 @@ int dusb_decomp(const char *filename)
fprintf(logfile, "TI packet decompiler for D-USB, version 1.0\n");

// skip comments
if ( fgets(line, sizeof(line), hex) == NULL
|| fgets(line, sizeof(line), hex) == NULL
|| fgets(line, sizeof(line), hex) == NULL)
if ( fgets(line, sizeof(line), hex) == nullptr
|| fgets(line, sizeof(line), hex) == nullptr
|| fgets(line, sizeof(line), hex) == nullptr)
{
goto exit;
}
Expand Down
Loading

0 comments on commit 6f32882

Please sign in to comment.