[Forensics-changes] [yara] 114/135: Handle zero-length files as normal files and remove zero-length errors.
Hilko Bengen
bengen at moszumanska.debian.org
Sat Jul 1 10:27:38 UTC 2017
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to annotated tag v3.1.0
in repository yara.
commit 75139e2fb4f1a025ac2d41c24d6cd5c0964317f5
Author: Victor M. Alvarez <plusvic at gmail.com>
Date: Fri Aug 22 12:34:09 2014 +0200
Handle zero-length files as normal files and remove zero-length errors.
---
libyara/filemap.c | 103 +++++++++++++++++++++++--------------------
libyara/include/yara/error.h | 1 -
yara-python/yara-python.c | 5 ---
yara.c | 3 --
4 files changed, 56 insertions(+), 56 deletions(-)
diff --git a/libyara/filemap.c b/libyara/filemap.c
index 5b6cbd7..a532460 100755
--- a/libyara/filemap.c
+++ b/libyara/filemap.c
@@ -69,38 +69,40 @@ int yr_filemap_map(
return ERROR_COULD_NOT_OPEN_FILE;
}
- if (pmapped_file->size == 0)
+ if (pmapped_file->size != 0)
{
- CloseHandle(pmapped_file->file);
- return ERROR_ZERO_LENGTH_FILE;
- }
-
- pmapped_file->mapping = CreateFileMapping(
- pmapped_file->file,
- NULL,
- PAGE_READONLY,
- 0,
- 0,
- NULL);
-
- if (pmapped_file->mapping == NULL)
- {
- CloseHandle(pmapped_file->file);
- return ERROR_COULD_NOT_MAP_FILE;
+ pmapped_file->mapping = CreateFileMapping(
+ pmapped_file->file,
+ NULL,
+ PAGE_READONLY,
+ 0,
+ 0,
+ NULL);
+
+ if (pmapped_file->mapping == NULL)
+ {
+ CloseHandle(pmapped_file->file);
+ return ERROR_COULD_NOT_MAP_FILE;
+ }
+
+ pmapped_file->data = (uint8_t*) MapViewOfFile(
+ pmapped_file->mapping,
+ FILE_MAP_READ,
+ 0,
+ 0,
+ 0);
+
+ if (pmapped_file->data == NULL)
+ {
+ CloseHandle(pmapped_file->mapping);
+ CloseHandle(pmapped_file->file);
+ return ERROR_COULD_NOT_MAP_FILE;
+ }
}
-
- pmapped_file->data = (uint8_t*) MapViewOfFile(
- pmapped_file->mapping,
- FILE_MAP_READ,
- 0,
- 0,
- 0);
-
- if (pmapped_file->data == NULL)
+ else
{
- CloseHandle(pmapped_file->mapping);
- CloseHandle(pmapped_file->file);
- return ERROR_COULD_NOT_MAP_FILE;
+ pmapped_file->mapping = NULL;
+ pmapped_file->data = NULL;
}
return ERROR_SUCCESS;
@@ -109,8 +111,12 @@ int yr_filemap_map(
void yr_filemap_unmap(
YR_MAPPED_FILE* pmapped_file)
{
- UnmapViewOfFile(pmapped_file->data);
- CloseHandle(pmapped_file->mapping);
+ if (pmapped_file->data != NULL)
+ UnmapViewOfFile(pmapped_file->data);
+
+ if (pmapped_file->mapping != NULL)
+ CloseHandle(pmapped_file->mapping);
+
CloseHandle(pmapped_file->file);
}
@@ -139,24 +145,25 @@ int yr_filemap_map(
pmapped_file->size = fstat.st_size;
- if (pmapped_file->size == 0)
+ if (pmapped_file->size != 0)
{
- close(pmapped_file->file);
- return ERROR_ZERO_LENGTH_FILE;
+ pmapped_file->data = (uint8_t*) mmap(
+ 0,
+ pmapped_file->size,
+ PROT_READ,
+ MAP_PRIVATE,
+ pmapped_file->file,
+ 0);
+
+ if (pmapped_file->data == MAP_FAILED)
+ {
+ close(pmapped_file->file);
+ return ERROR_COULD_NOT_MAP_FILE;
+ }
}
-
- pmapped_file->data = (uint8_t*) mmap(
- 0,
- pmapped_file->size,
- PROT_READ,
- MAP_PRIVATE,
- pmapped_file->file,
- 0);
-
- if (pmapped_file->data == MAP_FAILED)
+ else
{
- close(pmapped_file->file);
- return ERROR_COULD_NOT_MAP_FILE;
+ pmapped_file->data = NULL;
}
return ERROR_SUCCESS;
@@ -165,7 +172,9 @@ int yr_filemap_map(
void yr_filemap_unmap(
YR_MAPPED_FILE* pmapped_file)
{
- munmap(pmapped_file->data, pmapped_file->size);
+ if (pmapped_file->data != NULL)
+ munmap(pmapped_file->data, pmapped_file->size);
+
close(pmapped_file->file);
}
diff --git a/libyara/include/yara/error.h b/libyara/include/yara/error.h
index d66d17a..6a8d343 100644
--- a/libyara/include/yara/error.h
+++ b/libyara/include/yara/error.h
@@ -25,7 +25,6 @@ limitations under the License.
#define ERROR_COULD_NOT_ATTACH_TO_PROCESS 2
#define ERROR_COULD_NOT_OPEN_FILE 3
#define ERROR_COULD_NOT_MAP_FILE 4
-#define ERROR_ZERO_LENGTH_FILE 5
#define ERROR_INVALID_FILE 6
#define ERROR_CORRUPT_FILE 7
#define ERROR_UNSUPPORTED_FILE_VERSION 8
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index 74e288f..a7a784e 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -624,11 +624,6 @@ PyObject* handle_error(
YaraError,
"could not map file \"%s\" into memory",
extra);
- case ERROR_ZERO_LENGTH_FILE:
- return PyErr_Format(
- YaraError,
- "zero length file \"%s\"",
- extra);
case ERROR_INVALID_FILE:
return PyErr_Format(
YaraError,
diff --git a/yara.c b/yara.c
index 79519da..546bdc0 100644
--- a/yara.c
+++ b/yara.c
@@ -410,9 +410,6 @@ void print_scanner_error(int error)
case ERROR_COULD_NOT_OPEN_FILE:
fprintf(stderr, "could not open file\n");
break;
- case ERROR_ZERO_LENGTH_FILE:
- fprintf(stderr, "zero length file\n");
- break;
case ERROR_UNSUPPORTED_FILE_VERSION:
fprintf(stderr, "rules were compiled with a newer version of YARA.\n");
break;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git
More information about the forensics-changes
mailing list