Skip to content

Commit

Permalink
Remove dead code to satisfy full cppcheck
Browse files Browse the repository at this point in the history
While enabling all checks with cppcheck, it would report unused
functions. Therefore, this patch attempted to remove the dead and revise
the corresponding call flow.
  • Loading branch information
jserv committed Jan 23, 2020
1 parent 04cf2c6 commit 8271bf9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 155 deletions.
39 changes: 3 additions & 36 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ void add_quit_helper(cmd_function qf)
report_event(MSG_FATAL, "Exceeded limit on quit helpers");
}

/* Set prompt string */
void set_prompt(char *p)
{
prompt = p;
}

/* Turn echoing on/off */
void set_echo(bool on)
{
Expand Down Expand Up @@ -475,7 +469,7 @@ static char *readline()
char c;
char *lptr = linebuf;

if (buf_stack == NULL)
if (!buf_stack)
return NULL;

for (cnt = 0; cnt < RIO_BUFSIZE - 2; cnt++) {
Expand Down Expand Up @@ -519,23 +513,6 @@ static char *readline()
return linebuf;
}


void block_console()
{
block_flag = true;
}

void unblock_console()
{
block_flag = false;
if (block_timing) {
double delta = delta_time(&last_time);
report(1, "Delta time = %.3f", delta);
}
block_timing = false;
}


/* Determine if there is a complete command line in input buffer */
static bool read_ready()
{
Expand Down Expand Up @@ -576,7 +553,7 @@ int cmd_select(int nfds,
return 0;
if (!block_flag) {
/* Process any commands in input buffer */
if (readfds == NULL)
if (!readfds)
readfds = &local_readset;
/* Add input fd to readset for select */
infd = buf_stack->fd;
Expand Down Expand Up @@ -607,19 +584,9 @@ int cmd_select(int nfds,
return result;
}


bool start_cmd(char *infile_name)
{
bool ok = push_file(infile_name);
if (!ok)
report(1, "Could not open source file '%s'",
infile_name ? infile_name : "standard input");
return ok;
}

bool cmd_done()
{
return buf_stack == NULL || quit_flag;
return !buf_stack || quit_flag;
}


Expand Down
21 changes: 0 additions & 21 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,9 @@ bool get_int(char *vname, int *loc);
/* Add function to be executed as part of program exit */
void add_quit_helper(cmd_function qf);

/* Set prompt */
void set_prompt(char *prompt);

/* Turn echoing on/off */
void set_echo(bool on);

/*
Some console commands require network activity to complete.
Program maintains a flag indicating whether console is ready to execute a new
command (unblocked) or it must wait for pending network activity (blocked).
The following calls set/clear that flag
*/

void block_console();
void unblock_console();

/* Start command intrepretation.
If infile_name is NULL, then read commands from stdin
Otherwise, use infile as command source
*/
bool start_cmd(char *infile_name);


/* Is it time to quit the command loop? */
bool cmd_done();

Expand Down
15 changes: 10 additions & 5 deletions harness.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static bool fail_allocation()
*/
static block_ele_t *find_header(void *p)
{
if (p == NULL) {
if (!p) {
report_event(MSG_ERROR, "Attempting to free null block");
error_occurred = true;
}
Expand Down Expand Up @@ -109,6 +109,7 @@ static block_ele_t *find_header(void *p)
/* Given pointer to block, find its footer */
static size_t *find_footer(block_ele_t *b)
{
// cppcheck-suppress nullPointerRedundantCheck
size_t *p = (size_t *) ((size_t) b + b->payload_size + sizeof(block_ele_t));
return p;
}
Expand All @@ -129,16 +130,20 @@ void *test_malloc(size_t size)
}
block_ele_t *new_block =
malloc(size + sizeof(block_ele_t) + sizeof(size_t));
if (new_block == NULL) {
if (!new_block) {
report_event(MSG_FATAL, "Couldn't allocate any more memory");
error_occurred = true;
}
// cppcheck-suppress nullPointerRedundantCheck
new_block->magic_header = MAGICHEADER;
// cppcheck-suppress nullPointerRedundantCheck
new_block->payload_size = size;
*find_footer(new_block) = MAGICFOOTER;
void *p = (void *) &new_block->payload;
memset(p, FILLCHAR, size);
// cppcheck-suppress nullPointerRedundantCheck
new_block->next = allocated;
// cppcheck-suppress nullPointerRedundantCheck
new_block->prev = NULL;
if (allocated)
allocated->prev = new_block;
Expand All @@ -153,9 +158,8 @@ void test_free(void *p)
report_event(MSG_FATAL, "Calls to free disallowed");
return;
}
if (p == NULL) {
if (!p)
return;
}
block_ele_t *b = find_header(p);
size_t footer = *find_footer(b);
if (footer != MAGICFOOTER) {
Expand Down Expand Up @@ -183,12 +187,13 @@ void test_free(void *p)
allocated_count--;
}

// cppcheck-suppress unusedFunction
char *test_strdup(const char *s)
{
size_t len = strlen(s) + 1;
void *new = test_malloc(len);

if (new == NULL)
if (!new)
return NULL;
return (char *) memcpy(new, s, len);
}
Expand Down
37 changes: 17 additions & 20 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bool do_new(int argc, char *argv[])
return false;
}
bool ok = true;
if (q != NULL) {
if (q) {
report(3, "Freeing old queue");
ok = do_free(argc, argv);
}
Expand All @@ -119,7 +119,7 @@ bool do_free(int argc, char *argv[])
return false;
}
bool ok = true;
if (q == NULL)
if (!q)
report(3, "Warning: Calling free on null queue");
error_check();
if (qcnt > big_queue_size)
Expand All @@ -145,7 +145,6 @@ bool do_insert_head(int argc, char *argv[])
char *inserts;
char *lasts = NULL;
int reps = 1;
int r;
bool ok = true;
if (argc != 2 && argc != 3) {
report(1, "%s needs 1-2 arguments", argv[0]);
Expand All @@ -158,11 +157,11 @@ bool do_insert_head(int argc, char *argv[])
return false;
}
}
if (q == NULL)
if (!q)
report(3, "Warning: Calling insert head on null queue");
error_check();
if (exception_setup(true)) {
for (r = 0; ok && r < reps; r++) {
for (int r = 0; ok && r < reps; r++) {
bool rval = q_insert_head(q, inserts);
if (rval) {
qcnt++;
Expand Down Expand Up @@ -206,7 +205,6 @@ bool do_insert_tail(int argc, char *argv[])
{
char *inserts;
int reps = 1;
int r;
bool ok = true;
if (argc != 2 && argc != 3) {
report(1, "%s needs 1-2 arguments", argv[0]);
Expand All @@ -219,11 +217,11 @@ bool do_insert_tail(int argc, char *argv[])
return false;
}
}
if (q == NULL)
if (!q)
report(3, "Warning: Calling insert tail on null queue");
error_check();
if (exception_setup(true)) {
for (r = 0; ok && r < reps; r++) {
for (int r = 0; ok && r < reps; r++) {
bool rval = q_insert_tail(q, inserts);
if (rval) {
qcnt++;
Expand Down Expand Up @@ -257,13 +255,13 @@ bool do_remove_head(int argc, char *argv[])
return false;
}
char *removes = malloc(string_length + STRINGPAD + 1);
if (removes == NULL) {
if (!removes) {
report(1,
"INTERNAL ERROR. Could not allocate space for removed strings");
return false;
}
char *checks = malloc(string_length + 1);
if (checks == NULL) {
if (!checks) {
report(1,
"INTERNAL ERROR. Could not allocate space for removed strings");
free(removes);
Expand All @@ -280,9 +278,9 @@ bool do_remove_head(int argc, char *argv[])
memset(removes + 1, 'X', string_length + STRINGPAD - 1);
removes[string_length + STRINGPAD] = '\0';

if (q == NULL)
if (!q)
report(3, "Warning: Calling remove head on null queue");
else if (q->head == NULL)
else if (!q->head)
report(3, "Warning: Calling remove head on empty queue");
error_check();
bool rval = false;
Expand Down Expand Up @@ -341,9 +339,9 @@ bool do_remove_head_quiet(int argc, char *argv[])
return false;
}
bool ok = true;
if (q == NULL)
if (!q)
report(3, "Warning: Calling remove head on null queue");
else if (q->head == NULL)
else if (!q->head)
report(3, "Warning: Calling remove head on empty queue");
error_check();
bool rval = false;
Expand Down Expand Up @@ -372,7 +370,7 @@ bool do_reverse(int argc, char *argv[])
report(1, "%s takes no arguments", argv[0]);
return false;
}
if (q == NULL)
if (!q)
report(3, "Warning: Calling reverse on null queue");
error_check();
set_noallocate_mode(true);
Expand All @@ -391,7 +389,6 @@ bool do_size(int argc, char *argv[])
return false;
}
int reps = 1;
int r;
bool ok = true;
if (argc != 1 && argc != 2) {
report(1, "%s needs 0-1 arguments", argv[0]);
Expand All @@ -403,11 +400,11 @@ bool do_size(int argc, char *argv[])
}
}
int cnt = 0;
if (q == NULL)
if (!q)
report(3, "Warning: Calling size on null queue");
error_check();
if (exception_setup(true)) {
for (r = 0; ok && r < reps; r++) {
for (int r = 0; ok && r < reps; r++) {
cnt = q_size(q);
ok = ok && !error_check();
}
Expand All @@ -434,7 +431,7 @@ static bool show_queue(int vlevel)
if (verblevel < vlevel)
return true;
int cnt = 0;
if (q == NULL) {
if (!q) {
report(vlevel, "q = NULL");
return true;
}
Expand All @@ -454,7 +451,7 @@ static bool show_queue(int vlevel)
report(vlevel, " ... ]");
return false;
}
if (e == NULL) {
if (!e) {
if (cnt <= big_queue_size)
report(vlevel, "]");
else
Expand Down
Loading

0 comments on commit 8271bf9

Please sign in to comment.