[Forensics-changes] [yara] 118/407: Implement yr_strndup
Hilko Bengen
bengen at moszumanska.debian.org
Sat Jul 1 10:28:16 UTC 2017
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to annotated tag v3.3.0
in repository yara.
commit 96e6d9e4db638e65a9d6ba6e8e601a27e55b18db
Author: Victor M. Alvarez <plusvic at gmail.com>
Date: Thu Oct 16 13:04:28 2014 +0200
Implement yr_strndup
---
libyara/include/yara/mem.h | 4 ++++
libyara/mem.c | 32 ++++++++++++++++++++++++++++----
2 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/libyara/include/yara/mem.h b/libyara/include/yara/mem.h
index 92abfb1..540131b 100644
--- a/libyara/include/yara/mem.h
+++ b/libyara/include/yara/mem.h
@@ -27,6 +27,7 @@ limitations under the License.
#define yr_realloc realloc
#define yr_free free
#define yr_strdup strdup
+#define yr_strndup strndup
#include <dmalloc.h>
@@ -49,6 +50,9 @@ void yr_free(
char* yr_strdup(
const char *str);
+char* yr_strndup(
+ const char *str, size_t n);
+
#endif
int yr_heap_alloc();
diff --git a/libyara/mem.c b/libyara/mem.c
index 98ce2d0..a9b7565 100644
--- a/libyara/mem.c
+++ b/libyara/mem.c
@@ -72,15 +72,33 @@ void yr_free(void* ptr)
char* yr_strdup(const char *str)
{
- size_t len = strlen(str) + 1;
- void *dup = yr_malloc(len);
+ size_t len = strlen(str);
+ void *dup = yr_malloc(len + 1);
- if (dup != NULL)
- memcpy(dup, str, len);
+ if (dup == NULL)
+ return NULL;
+
+ memcpy(dup, str, len);
+ dup[len] = '\0';
return (char*) dup;
}
+
+char* yr_strndup(const char *str, size_t n)
+{
+ size_t len = strnlen(str, n);
+ void *dup = yr_malloc(len + 1);
+
+ if (dup == NULL)
+ return NULL;
+
+ memcpy(dup, s, len);
+ dup[len] = '\0';
+
+ return (char *) dup;
+}
+
#else
#include <stdlib.h>
@@ -130,4 +148,10 @@ char* yr_strdup(const char *str)
return strdup(str);
}
+
+char* yr_strndup(const char *str, size_t n)
+{
+ return strndup(str, n);
+}
+
#endif
--
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