Adjustments and annotations as per splint and flawfinder reports.

This commit is contained in:
Andrew Wood
2023-08-14 22:33:06 +01:00
parent 32f984de56
commit 7f25aff91d
6 changed files with 92 additions and 31 deletions
+29 -7
View File
@@ -35,8 +35,8 @@ void pv_remote_fini(void);
int main(int argc, char **argv)
{
struct termios t, t_save;
opts_t opts;
pvstate_t state;
/*@only@ */ opts_t opts = NULL;
/*@only@ */ pvstate_t state = NULL;
bool t_saved, t_needs_reset;
int retcode = 0;
@@ -46,7 +46,7 @@ int main(int argc, char **argv)
(void) textdomain(PACKAGE);
#endif
opts = opts_parse(argc, argv);
opts = opts_parse(argc >= 0 ? (unsigned int) argc : 0, argv);
if (NULL == opts) {
debug("%s: %d", "exiting with status", 64);
return 64;
@@ -72,10 +72,17 @@ int main(int argc, char **argv)
*/
state = pv_state_alloc(opts->program_name);
if (NULL == state) {
/*@-mustfreefresh@ */
/*
* splint note: the gettext calls made by _() cause memory
* leak warnings, but in this case it's unavoidable, and
* mitigated by the fact we only translate each string once.
*/
fprintf(stderr, "%s: %s: %s\n", opts->program_name, _("state allocation failed"), strerror(errno));
opts_free(opts);
debug("%s: %d", "exiting with status", 64);
return 64;
/*@+mustfreefresh@ */
}
/*
@@ -85,10 +92,12 @@ int main(int argc, char **argv)
FILE *pidfptr;
pidfptr = fopen(opts->pidfile, "w");
if (NULL == pidfptr) {
/*@-mustfreefresh@ *//* see above */
fprintf(stderr, "%s: %s: %s\n", opts->program_name, opts->pidfile, strerror(errno));
pv_state_free(state);
opts_free(opts);
return 1;
/*@+mustfreefresh@ */
}
fprintf(pidfptr, "%d\n", getpid());
if (0 != fclose(pidfptr)) {
@@ -101,13 +110,19 @@ int main(int argc, char **argv)
*/
if (0 == opts->argc) {
debug("%s", "no files given - adding fake argument `-'");
opts->argv[opts->argc++] = "-";
if (!opts_add_file(opts, "-")) {
pv_state_free(state);
opts_free(opts);
return 64;
}
}
/*
* Put our list of files into the PV internal state.
*/
pv_state_inputfiles(state, opts->argc, (const char **) (opts->argv));
if (NULL != opts->argv) {
pv_state_inputfiles(state, opts->argc, (const char **) (opts->argv));
}
if (0 == opts->watch_pid) {
/*
@@ -200,8 +215,13 @@ int main(int argc, char **argv)
pv_state_target_buffer_size_set(state, opts->buffer_size);
pv_state_no_splice_set(state, opts->no_splice);
pv_state_size_set(state, opts->size);
pv_state_name_set(state, opts->name);
pv_state_format_string_set(state, opts->format);
if (NULL != opts->name)
pv_state_name_set(state, opts->name);
if (NULL != opts->format)
pv_state_format_string_set(state, opts->format);
pv_state_watch_pid_set(state, opts->watch_pid);
pv_state_watch_fd_set(state, opts->watch_fd);
pv_state_average_rate_window_set(state, opts->average_rate_window);
@@ -239,8 +259,10 @@ int main(int argc, char **argv)
debug("%s", "saved terminal attributes");
t_saved = true;
} else {
/*@-mustfreefresh@ *//* see above */
fprintf(stderr, "%s: %s: %s\n", opts->program_name,
_("failed to read terminal attributes"), strerror(errno));
/*@+mustfreefresh@ */
}
}
t_save = t;
+48 -8
View File
@@ -39,7 +39,7 @@ char *xstrdup(const char *original)
return NULL;
}
length = strlen(original); /* flawfinder: ignore */
length = strlen(original); /* flawfinder: ignore */
/*
* flawfinder rationale: the original string is explicitly required
* to be \0 terminated.
@@ -84,18 +84,54 @@ void opts_free( /*@only@ */ opts_t opts)
free(opts);
}
/*
* Add a filename to the list of non-option arguments, returning false on
* error. The filename is not copied - the pointer is stored.
*/
bool opts_add_file(opts_t opts, char *filename)
{
/*@-branchstate@ */
if ((opts->argc >= opts->argv_length) || (NULL == opts->argv)) {
opts->argv_length = opts->argc + 10;
/*@-keeptrans@ */
opts->argv = realloc(opts->argv, opts->argv_length * sizeof(char *));
/*@+keeptrans@ */
if (NULL == opts->argv) {
fprintf(stderr, "%s: %s\n", opts->program_name, strerror(errno));
opts->argv_length = 0;
opts->argc = 0;
return false;
}
}
/*@+branchstate@ */
/*
* splint notes: we turned off "branchstate" above because depending
* on whether we have to extend the array, we change argv from
* "keep" to "only", which is also why we turned off "keeptrans";
* there doesn't seem to be a clean way to tell splint that everyone
* else should not touch argv but we're allowed to reallocate it and
* so is opts_parse.
*/
opts->argv[opts->argc++] = filename;
return true;
}
/*
* Parse the given command-line arguments into an opts_t object, handling
* "help" and "version" options internally.
*
* Returns an opts_t, or 0 on error.
* Returns an opts_t, or NULL on error.
*
* Note that the contents of *argv[] (i.e. the command line parameters)
* aren't copied anywhere, just the pointers are copied, so make sure the
* command line data isn't overwritten or argv[1] free()d or whatever.
*/
/*@null@ *//*@only@ */ opts_t opts_parse(int argc, char **argv)
/*@null@ *//*@only@ */
opts_t opts_parse(unsigned int argc, char **argv)
{
#ifdef HAVE_GETOPT_LONG
/*@-nullassign@ */
@@ -189,6 +225,7 @@ void opts_free( /*@only@ */ opts_t opts)
return NULL;
/*@+mustfreefresh@ */
}
opts->argv_length = 1 + argc;
numopts = 0;
@@ -200,9 +237,9 @@ void opts_free( /*@only@ */ opts_t opts)
do {
#ifdef HAVE_GETOPT_LONG
c = getopt_long(argc, argv, short_options, long_options, &option_index); /* flawfinder: ignore */
c = getopt_long((int) argc, argv, short_options, long_options, &option_index); /* flawfinder: ignore */
#else
c = getopt(argc, argv, short_options); /* flawfinder: ignore */
c = getopt((int) argc, argv, short_options); /* flawfinder: ignore */
#endif
/*
* flawfinder rationale: we have to pass argv to getopt, and
@@ -519,7 +556,7 @@ void opts_free( /*@only@ */ opts_t opts)
/*@+mustfreefresh@ */
}
if (optind < argc) {
if (optind < (int) argc) {
/*@-mustfreefresh@ *//* see above */
fprintf(stderr, "%s: %s\n", opts->program_name,
_("cannot transfer files when watching file descriptors"));
@@ -559,8 +596,11 @@ void opts_free( /*@only@ */ opts_t opts)
/*
* Store remaining command-line arguments.
*/
while (optind < argc) {
opts->argv[opts->argc++] = argv[optind++];
while (optind < (int) argc) {
if (!opts_add_file(opts, argv[optind++])) {
opts_free(opts);
return NULL;
}
}
return opts;