Commit e2d027de authored by Daniel Salzman's avatar Daniel Salzman

zscanner: fix scanner deinitialization (Coverity)

parent 657e818d
......@@ -146,18 +146,28 @@ int zs_init(
return 0;
}
static void file_deinit(
static void input_deinit(
zs_scanner_t *s)
{
if (s->file.descriptor == -1) {
return;
// Deinit the file input.
if (s->file.descriptor != -1) {
// Unmap the file content.
if (s->input.start != NULL) {
munmap((void *)s->input.start,
s->input.end - s->input.start);
}
// Close the opened file.
close(s->file.descriptor);
s->file.descriptor = -1;
free(s->file.name);
}
// Clean-up the opened file.
munmap((void *)s->input.start, s->input.end - s->input.start);
close(s->file.descriptor);
free(s->file.name);
s->file.descriptor = -1;
// Unset the input limits.
s->input.start = NULL;
s->input.current = NULL;
s->input.end = NULL;
s->input.eof = false;
}
__attribute__((visibility("default")))
......@@ -168,7 +178,7 @@ void zs_deinit(
return;
}
file_deinit(s);
input_deinit(s);
free(s->path);
}
......@@ -188,13 +198,12 @@ int zs_set_input_string(
}
// Deinit possibly opened file.
file_deinit(s);
input_deinit(s);
// Set the scanner input limits.
s->input.start = input;
s->input.current = input;
s->input.end = input + size;
s->input.eof = false;
return 0;
}
......@@ -214,7 +223,7 @@ int zs_set_input_file(
}
// Deinit possibly opened file.
file_deinit(s);
input_deinit(s);
// Try to open the file.
s->file.descriptor = open(file_name, O_RDONLY);
......@@ -228,23 +237,28 @@ int zs_set_input_file(
if (fstat(s->file.descriptor, &file_stat) == -1 ||
!S_ISREG(file_stat.st_mode)) {
ERR(ZS_FILE_INVALID);
goto file_error;
input_deinit(s);
return -1;
}
char *start = NULL;
// Check for empty file (cannot mmap).
if (file_stat.st_size > 0) {
// Map the file to the memory.
start = mmap(0, file_stat.st_size, PROT_READ, MAP_SHARED,
s->file.descriptor, 0);
char *start = mmap(0, file_stat.st_size, PROT_READ, MAP_SHARED,
s->file.descriptor, 0);
if (start == MAP_FAILED) {
ERR(ZS_FILE_MMAP);
goto file_error;
input_deinit(s);
return -1;
}
// Try to set the mapped memory advise to sequential.
(void)madvise(start, file_stat.st_size, MADV_SEQUENTIAL);
// Set the scanner input limits.
s->input.start = start;
s->input.current = start;
s->input.end = start + file_stat.st_size;
}
// Get absolute path of the zone file.
......@@ -255,32 +269,23 @@ int zs_set_input_file(
free(full_name);
if (s->path == NULL) {
ERR(ZS_ENOMEM);
goto file_error;
input_deinit(s);
return -1;
}
} else {
ERR(ZS_FILE_PATH);
goto file_error;
input_deinit(s);
return -1;
}
s->file.name = strdup(file_name);
if (s->file.name == NULL) {
ERR(ZS_ENOMEM);
goto file_error;
input_deinit(s);
return -1;
}
// Set the scanner input limits.
s->input.start = start;
s->input.current = start;
s->input.end = start + file_stat.st_size;
s->input.eof = false;
return 0;
file_error:
close(s->file.descriptor);
s->file.descriptor = -1;
free(s->file.name);
return -1;
}
__attribute__((visibility("default")))
......@@ -5022,18 +5022,28 @@ int zs_init(
return 0;
}
static void file_deinit(
static void input_deinit(
zs_scanner_t *s)
{
if (s->file.descriptor == -1) {
return;
// Deinit the file input.
if (s->file.descriptor != -1) {
// Unmap the file content.
if (s->input.start != NULL) {
munmap((void *)s->input.start,
s->input.end - s->input.start);
}
// Close the opened file.
close(s->file.descriptor);
s->file.descriptor = -1;
free(s->file.name);
}
// Clean-up the opened file.
munmap((void *)s->input.start, s->input.end - s->input.start);
close(s->file.descriptor);
free(s->file.name);
s->file.descriptor = -1;
// Unset the input limits.
s->input.start = NULL;
s->input.current = NULL;
s->input.end = NULL;
s->input.eof = false;
}
__attribute__((visibility("default")))
......@@ -5044,7 +5054,7 @@ void zs_deinit(
return;
}
file_deinit(s);
input_deinit(s);
free(s->path);
}
......@@ -5064,13 +5074,12 @@ int zs_set_input_string(
}
// Deinit possibly opened file.
file_deinit(s);
input_deinit(s);
// Set the scanner input limits.
s->input.start = input;
s->input.current = input;
s->input.end = input + size;
s->input.eof = false;
return 0;
}
......@@ -5090,7 +5099,7 @@ int zs_set_input_file(
}
// Deinit possibly opened file.
file_deinit(s);
input_deinit(s);
// Try to open the file.
s->file.descriptor = open(file_name, O_RDONLY);
......@@ -5104,23 +5113,28 @@ int zs_set_input_file(
if (fstat(s->file.descriptor, &file_stat) == -1 ||
!S_ISREG(file_stat.st_mode)) {
ERR(ZS_FILE_INVALID);
goto file_error;
input_deinit(s);
return -1;
}
char *start = NULL;
// Check for empty file (cannot mmap).
if (file_stat.st_size > 0) {
// Map the file to the memory.
start = mmap(0, file_stat.st_size, PROT_READ, MAP_SHARED,
s->file.descriptor, 0);
char *start = mmap(0, file_stat.st_size, PROT_READ, MAP_SHARED,
s->file.descriptor, 0);
if (start == MAP_FAILED) {
ERR(ZS_FILE_MMAP);
goto file_error;
input_deinit(s);
return -1;
}
// Try to set the mapped memory advise to sequential.
(void)madvise(start, file_stat.st_size, MADV_SEQUENTIAL);
// Set the scanner input limits.
s->input.start = start;
s->input.current = start;
s->input.end = start + file_stat.st_size;
}
// Get absolute path of the zone file.
......@@ -5131,32 +5145,23 @@ int zs_set_input_file(
free(full_name);
if (s->path == NULL) {
ERR(ZS_ENOMEM);
goto file_error;
input_deinit(s);
return -1;
}
} else {
ERR(ZS_FILE_PATH);
goto file_error;
input_deinit(s);
return -1;
}
s->file.name = strdup(file_name);
if (s->file.name == NULL) {
ERR(ZS_ENOMEM);
goto file_error;
input_deinit(s);
return -1;
}
// Set the scanner input limits.
s->input.start = start;
s->input.current = start;
s->input.end = start + file_stat.st_size;
s->input.eof = false;
return 0;
file_error:
close(s->file.descriptor);
s->file.descriptor = -1;
free(s->file.name);
return -1;
}
__attribute__((visibility("default")))
......
......@@ -147,18 +147,28 @@ int zs_init(
return 0;
}
static void file_deinit(
static void input_deinit(
zs_scanner_t *s)
{
if (s->file.descriptor == -1) {
return;
// Deinit the file input.
if (s->file.descriptor != -1) {
// Unmap the file content.
if (s->input.start != NULL) {
munmap((void *)s->input.start,
s->input.end - s->input.start);
}
// Close the opened file.
close(s->file.descriptor);
s->file.descriptor = -1;
free(s->file.name);
}
// Clean-up the opened file.
munmap((void *)s->input.start, s->input.end - s->input.start);
close(s->file.descriptor);
free(s->file.name);
s->file.descriptor = -1;
// Unset the input limits.
s->input.start = NULL;
s->input.current = NULL;
s->input.end = NULL;
s->input.eof = false;
}
__attribute__((visibility("default")))
......@@ -169,7 +179,7 @@ void zs_deinit(
return;
}
file_deinit(s);
input_deinit(s);
free(s->path);
}
......@@ -189,13 +199,12 @@ int zs_set_input_string(
}
// Deinit possibly opened file.
file_deinit(s);
input_deinit(s);
// Set the scanner input limits.
s->input.start = input;
s->input.current = input;
s->input.end = input + size;
s->input.eof = false;
return 0;
}
......@@ -215,7 +224,7 @@ int zs_set_input_file(
}
// Deinit possibly opened file.
file_deinit(s);
input_deinit(s);
// Try to open the file.
s->file.descriptor = open(file_name, O_RDONLY);
......@@ -229,23 +238,28 @@ int zs_set_input_file(
if (fstat(s->file.descriptor, &file_stat) == -1 ||
!S_ISREG(file_stat.st_mode)) {
ERR(ZS_FILE_INVALID);
goto file_error;
input_deinit(s);
return -1;
}
char *start = NULL;
// Check for empty file (cannot mmap).
if (file_stat.st_size > 0) {
// Map the file to the memory.
start = mmap(0, file_stat.st_size, PROT_READ, MAP_SHARED,
s->file.descriptor, 0);
char *start = mmap(0, file_stat.st_size, PROT_READ, MAP_SHARED,
s->file.descriptor, 0);
if (start == MAP_FAILED) {
ERR(ZS_FILE_MMAP);
goto file_error;
input_deinit(s);
return -1;
}
// Try to set the mapped memory advise to sequential.
(void)madvise(start, file_stat.st_size, MADV_SEQUENTIAL);
// Set the scanner input limits.
s->input.start = start;
s->input.current = start;
s->input.end = start + file_stat.st_size;
}
// Get absolute path of the zone file.
......@@ -256,32 +270,23 @@ int zs_set_input_file(
free(full_name);
if (s->path == NULL) {
ERR(ZS_ENOMEM);
goto file_error;
input_deinit(s);
return -1;
}
} else {
ERR(ZS_FILE_PATH);
goto file_error;
input_deinit(s);
return -1;
}
s->file.name = strdup(file_name);
if (s->file.name == NULL) {
ERR(ZS_ENOMEM);
goto file_error;
input_deinit(s);
return -1;
}
// Set the scanner input limits.
s->input.start = start;
s->input.current = start;
s->input.end = start + file_stat.st_size;
s->input.eof = false;
return 0;
file_error:
close(s->file.descriptor);
s->file.descriptor = -1;
free(s->file.name);
return -1;
}
__attribute__((visibility("default")))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment