bpf: fix file parsing memory handling

Fix improper fread string handling. Improve error handling.

Skip trailing spaces for slightly more pretty printing.

Coverity CID 400763.

Thanks to Steve Grubb for helping address this issue.
pull/2242/head
Victor Julien 9 years ago
parent 519b2970ec
commit 71c8d1f46c

@ -484,11 +484,15 @@ static void SetBpfStringFromFile(char *filename)
} }
memset(bpf_filter, 0x00, bpf_len); memset(bpf_filter, 0x00, bpf_len);
nm = fread(bpf_filter, bpf_len - 1, 1, fp); nm = fread(bpf_filter, 1, bpf_len - 1, fp);
if((ferror(fp) != 0)||( nm != 1)) { if ((ferror(fp) != 0) || (nm != (bpf_len - 1))) {
*bpf_filter='\0'; SCLogError(SC_ERR_BPF, "Failed to read complete BPF file %s", filename);
SCFree(bpf_filter);
fclose(fp);
exit(EXIT_FAILURE);
} }
fclose(fp); fclose(fp);
bpf_filter[nm] = '\0';
if(strlen(bpf_filter) > 0) { if(strlen(bpf_filter) > 0) {
/*replace comments with space*/ /*replace comments with space*/
@ -508,10 +512,18 @@ static void SetBpfStringFromFile(char *filename)
while((bpf_comment_tmp = strchr(bpf_filter, '\n')) != NULL) { while((bpf_comment_tmp = strchr(bpf_filter, '\n')) != NULL) {
*bpf_comment_tmp = ' '; *bpf_comment_tmp = ' ';
} }
if(ConfSetFinal("bpf-filter", bpf_filter) != 1) { /* cut trailing spaces */
SCLogError(SC_ERR_FOPEN, "ERROR: Failed to set bpf filter!"); while (strlen(bpf_filter) > 0 &&
SCFree(bpf_filter); bpf_filter[strlen(bpf_filter)-1] == ' ')
exit(EXIT_FAILURE); {
bpf_filter[strlen(bpf_filter)-1] = '\0';
}
if (strlen(bpf_filter) > 0) {
if(ConfSetFinal("bpf-filter", bpf_filter) != 1) {
SCLogError(SC_ERR_FOPEN, "ERROR: Failed to set bpf filter!");
SCFree(bpf_filter);
exit(EXIT_FAILURE);
}
} }
} }
SCFree(bpf_filter); SCFree(bpf_filter);

Loading…
Cancel
Save