From: Mike Brady Date: Sun, 3 Sep 2017 14:08:13 +0000 (+0100) Subject: Fix some silent issues that were highlighted by "infer" X-Git-Tag: 3.2d5~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31a09bb368b27bcf2614328f12a0e04257f96d2c;p=thirdparty%2Fshairport-sync.git Fix some silent issues that were highlighted by "infer" --- diff --git a/alac.c b/alac.c index c814ee3f..d8b6ac9c 100644 --- a/alac.c +++ b/alac.c @@ -998,12 +998,13 @@ void alac_decode_frame(alac_file *alac, unsigned char *inbuffer, void *outbuffer alac_file *alac_create(int samplesize, int numchannels) { alac_file *newfile = malloc(sizeof(alac_file)); - - memset(newfile, 0, sizeof(alac_file)); - - newfile->samplesize = samplesize; - newfile->numchannels = numchannels; - newfile->bytespersample = (samplesize / 8) * numchannels; - + if (newfile) { + memset(newfile, 0, sizeof(alac_file)); + newfile->samplesize = samplesize; + newfile->numchannels = numchannels; + newfile->bytespersample = (samplesize / 8) * numchannels; + } else { + fprintf(stderr, "FIXME: can not allocate memory for a new file in alac_cxreate."); + } return newfile; } diff --git a/common.c b/common.c index 08243fb3..8e747b43 100644 --- a/common.c +++ b/common.c @@ -767,22 +767,26 @@ char *str_replace(const char *string, const char *substr, const char *replacemen return strdup(string); newstr = strdup(string); head = newstr; - while ((tok = strstr(head, substr))) { - oldstr = newstr; - newstr = malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1); - /*failed to alloc mem, free old string and return NULL */ - if (newstr == NULL) { + if (head) { + while ((tok = strstr(head, substr))) { + oldstr = newstr; + newstr = malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1); + /*failed to alloc mem, free old string and return NULL */ + if (newstr == NULL) { + free(oldstr); + return NULL; + } + memcpy(newstr, oldstr, tok - oldstr); + memcpy(newstr + (tok - oldstr), replacement, strlen(replacement)); + memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), + strlen(oldstr) - strlen(substr) - (tok - oldstr)); + memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement), 0, 1); + /* move back head right after the last replacement */ + head = newstr + (tok - oldstr) + strlen(replacement); free(oldstr); - return NULL; } - memcpy(newstr, oldstr, tok - oldstr); - memcpy(newstr + (tok - oldstr), replacement, strlen(replacement)); - memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), - strlen(oldstr) - strlen(substr) - (tok - oldstr)); - memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement), 0, 1); - /* move back head right after the last replacement */ - head = newstr + (tok - oldstr) + strlen(replacement); - free(oldstr); + } else { + die("failed to allocate memory in str_replace."); } return newstr; } @@ -832,10 +836,14 @@ int ranarraynext; void ranarrayinit() { ranarray = (uint64_t *)malloc(ranarraylength * sizeof(uint64_t)); - int i; - for (i = 0; i < ranarraylength; i++) - ranarray[i] = r64u(); - ranarraynext = 0; + if (ranarray) { + int i; + for (i = 0; i < ranarraylength; i++) + ranarray[i] = r64u(); + ranarraynext = 0; + } else { + die("failed to allocate space for the ranarray."); + } } uint64_t ranarrayval() { diff --git a/rtsp.c b/rtsp.c index c0e34811..3a661223 100644 --- a/rtsp.c +++ b/rtsp.c @@ -252,8 +252,12 @@ void rtsp_request_shutdown_stream(void) { static int nconns = 0; static void track_thread(rtsp_conn_info *conn) { conns = realloc(conns, sizeof(rtsp_conn_info *) * (nconns + 1)); - conns[nconns] = conn; - nconns++; + if (conns) { + conns[nconns] = conn; + nconns++; + } else { + die("could not reallocate memnory for \"conns\" in rtsp.c."); + } } static void cleanup_threads(void) { @@ -329,8 +333,12 @@ static void msg_retain(rtsp_message *msg) { static rtsp_message *msg_init(void) { rtsp_message *msg = malloc(sizeof(rtsp_message)); - memset(msg, 0, sizeof(rtsp_message)); - msg->referenceCount = 1; // from now on, any access to this must be protected with the lock + if (msg) { + memset(msg, 0, sizeof(rtsp_message)); + msg->referenceCount = 1; // from now on, any access to this must be protected with the lock + } else { + die("can not allocate memory for an rtsp_message."); + } return msg; } diff --git a/shairport.c b/shairport.c index 83dd3fc1..613b15ec 100644 --- a/shairport.c +++ b/shairport.c @@ -971,6 +971,8 @@ int main(int argc, char **argv) { char *basec = strdup(argv[0]); char *bname = basename(basec); appName = strdup(bname); + if (appName==NULL) + die("can not allocate memory for the app name!"); free(basec); // set defaults