summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorStef Walter <stefw@gnome.org>2013-03-20 20:59:26 +0100
committerStef Walter <stefw@gnome.org>2013-03-20 22:22:22 +0100
commit57d8f36a6cfbde5a9a783f11f2b75f19005c23e1 (patch)
treed21f862a9698ef6066ce2b14ef82fbd8c370f9ac /common
parent9cf89e4b43e5e018bb3103be1873a3993769ce4a (diff)
Fix invalid memory accesses reported by 'make memcheck'
These are things that showed up in valgrind while running the tests.
Diffstat (limited to 'common')
-rw-r--r--common/compat.c11
-rw-r--r--common/tests/test-compat.c16
-rw-r--r--common/tests/test-hash.c22
3 files changed, 31 insertions, 18 deletions
diff --git a/common/compat.c b/common/compat.c
index 2548459..2cda460 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -459,15 +459,12 @@ strndup (const char *data,
size_t length)
{
char *ret;
- size_t len;
-
- len = strlen (data);
- if (length > len)
- length = len;
- ret = memdup (data, length + 1);
- if (ret != NULL)
+ ret = malloc (length + 1);
+ if (ret != NULL) {
+ strncpy (ret, data, length);
ret[length] = 0;
+ }
return ret;
}
diff --git a/common/tests/test-compat.c b/common/tests/test-compat.c
index 13a7a33..a94aaeb 100644
--- a/common/tests/test-compat.c
+++ b/common/tests/test-compat.c
@@ -72,6 +72,21 @@ test_basename (CuTest *tc)
}
}
+static void
+test_strndup (CuTest *tc)
+{
+ char unterminated[] = { 't', 'e', 's', 't', 'e', 'r', 'o', 'n', 'i', 'o' };
+ char *res;
+
+ res = strndup (unterminated, 6);
+ CuAssertStrEquals (tc, res, "tester");
+ free (res);
+
+ res = strndup ("test", 6);
+ CuAssertStrEquals (tc, res, "test");
+ free (res);
+}
+
int
main (void)
{
@@ -80,6 +95,7 @@ main (void)
int ret;
SUITE_ADD_TEST (suite, test_basename);
+ SUITE_ADD_TEST (suite, test_strndup);
CuSuiteRun (suite);
CuSuiteSummary (suite, output);
diff --git a/common/tests/test-hash.c b/common/tests/test-hash.c
index f57988e..d6d7990 100644
--- a/common/tests/test-hash.c
+++ b/common/tests/test-hash.c
@@ -87,7 +87,7 @@ test_sha1_long (CuTest *cu)
CuAssertTrue (cu, input != NULL);
memset (input, 'a', 1000000);
- p11_hash_sha1 (checksum, input, strlen (input), NULL);
+ p11_hash_sha1 (checksum, input, 1000000, NULL);
CuAssertTrue (cu, memcmp (expected, checksum, P11_HASH_SHA1_LEN) == 0);
}
@@ -179,19 +179,19 @@ test_murmur2_incr (CuTest *cu)
uint32_t first, second;
p11_hash_murmur2 ((unsigned char *)&first,
- "this is the long input!", 23,
+ "this is the long input!", (size_t)23,
NULL);
p11_hash_murmur2 ((unsigned char *)&second,
- "this", 4,
- " ", 1,
- "is ", 3,
- "the long ", 9,
- "in", 2,
- "p", 1,
- "u", 1,
- "t", 1,
- "!", 1,
+ "this", (size_t)4,
+ " ", (size_t)1,
+ "is ", (size_t)3,
+ "the long ", (size_t)9,
+ "in", (size_t)2,
+ "p", (size_t)1,
+ "u", (size_t)1,
+ "t", (size_t)1,
+ "!", (size_t)1,
NULL);
CuAssertIntEquals (cu, first, second);