Skip to content

Commit

Permalink
Fix some silent issues that were highlighted by "infer"
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebrady committed Sep 3, 2017
1 parent 47886a3 commit 31a09bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
15 changes: 8 additions & 7 deletions alac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
44 changes: 26 additions & 18 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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() {
Expand Down
16 changes: 12 additions & 4 deletions rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31a09bb

Please sign in to comment.