various files - fix some alerts raised by lgtm code analysis

This patch fixes several issues reported by the lgtm code analysis tool:

https://lgtm.com/projects/g/asterisk/asterisk

Not all reported issues were addressed in this patch. This patch mostly fixes
confirmed reported errors, potential problematic code points, and a few other
"low hanging" warnings or recommendations found in core supported modules.
These include, but are not limited to the following:

* innapropriate stack allocation in loops
* buffer overflows
* variable declaration "hiding" another variable declaration
* comparisons results that are always the same
* ambiguously signed bit-field members
* missing header guards

Change-Id: Id4a881686605d26c94ab5409bc70fcc21efacc25
This commit is contained in:
Kevin Harwell
2019-10-23 12:36:17 -05:00
committed by George Joseph
parent 990a91b44a
commit bdd785d31c
49 changed files with 324 additions and 203 deletions

View File

@@ -517,7 +517,9 @@ static int filehelper(const char *filename, const void *arg2, const char *fmt, c
AST_RWLIST_RDLOCK(&formats);
/* Check for a specific format */
AST_RWLIST_TRAVERSE(&formats, f, list) {
char *stringp, *ext = NULL;
char *ext = NULL;
char storage[strlen(f->exts) + 1];
char *stringp;
if (fmt && !exts_compare(f->exts, fmt))
continue;
@@ -526,7 +528,8 @@ static int filehelper(const char *filename, const void *arg2, const char *fmt, c
* The file must exist, and for OPEN, must match
* one of the formats supported by the channel.
*/
stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
strcpy(storage, f->exts); /* safe - this is in the stack so does not need to be freed */
stringp = storage;
while ( (ext = strsep(&stringp, "|")) ) {
struct stat st;
char *fn = build_filename(filename, ext);
@@ -1407,13 +1410,13 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
*/
orig_fn = ast_strdupa(fn);
orig_fn = ast_strdup(fn);
for (c = fn; *c; c++)
if (*c == '/')
*c = '_';
size = strlen(fn) + strlen(record_cache_dir) + 2;
buf = ast_alloca(size);
buf = ast_malloc(size);
strcpy(buf, record_cache_dir);
strcat(buf, "/");
strcat(buf, fn);
@@ -1444,14 +1447,18 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
if (orig_fn) {
unlink(fn);
unlink(orig_fn);
ast_free(orig_fn);
}
if (fs) {
ast_closestream(fs);
fs = NULL;
}
if (!buf) {
ast_free(fn);
}
/*
* 'fn' was has either been allocated from build_filename, or that was freed
* and now 'fn' points to memory allocated for 'buf'. Either way the memory
* now needs to be released.
*/
ast_free(fn);
continue;
}
fs->trans = NULL;
@@ -1459,8 +1466,14 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
fs->flags = flags;
fs->mode = mode;
if (orig_fn) {
fs->realfilename = ast_strdup(orig_fn);
fs->filename = ast_strdup(fn);
fs->realfilename = orig_fn;
fs->filename = fn;
/*
* The above now manages the memory allocated for 'orig_fn' and 'fn', so
* set them to NULL, so they don't get released at the end of the loop.
*/
orig_fn = NULL;
fn = NULL;
} else {
fs->realfilename = NULL;
fs->filename = ast_strdup(filename);
@@ -1473,9 +1486,9 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
if (orig_fn)
unlink(orig_fn);
}
/* if buf != NULL then fn is already free and pointing to it */
if (!buf)
ast_free(fn);
/* Free 'fn', or if 'fn' points to 'buf' then free 'buf' */
ast_free(fn);
ast_free(orig_fn);
}
AST_RWLIST_UNLOCK(&formats);