diff --git a/common/dict.c b/common/dict.c index 62a7816..b7ab00d 100644 --- a/common/dict.c +++ b/common/dict.c @@ -122,7 +122,7 @@ lookup_or_create_bucket (p11_dict *dict, return bucketp; /* add a new entry for non-NULL val */ - (*bucketp) = calloc (1, sizeof (dictbucket)); + (*bucketp) = calloc (sizeof (dictbucket), 1); if (*bucketp != NULL) { (*bucketp)->key = (void*)key; @@ -175,7 +175,7 @@ p11_dict_set (p11_dict *dict, /* check that the collision rate isn't too high */ if (dict->num_items > dict->num_buckets) { num_buckets = dict->num_buckets * 2 + 1; - new_buckets = (dictbucket **)calloc (num_buckets, sizeof (dictbucket *)); + new_buckets = (dictbucket **)calloc (sizeof (dictbucket *), num_buckets); /* Ignore failures, maybe we can expand later */ if(new_buckets) { @@ -283,7 +283,7 @@ p11_dict_new (p11_dict_hasher hash_func, dict->value_destroy_func = value_destroy_func; dict->num_buckets = 9; - dict->buckets = (dictbucket **)calloc (dict->num_buckets, sizeof (dictbucket *)); + dict->buckets = (dictbucket **)calloc (sizeof (dictbucket *), dict->num_buckets); if (!dict->buckets) { free (dict); return NULL; diff --git a/common/path.c b/common/path.c index d0d1893..17a6230 100644 --- a/common/path.c +++ b/common/path.c @@ -94,21 +94,15 @@ p11_path_base (const char *path) } static inline bool -is_path_separator (char ch) +is_path_component_or_null (char ch) { - return (ch == '/' + return (ch == '\0' || ch == '/' #ifdef OS_WIN32 || ch == '\\' #endif ); } -static inline bool -is_path_separator_or_null (char ch) -{ - return is_path_separator (ch) || ch == '\0'; -} - static char * expand_homedir (const char *remainder) { @@ -119,7 +113,7 @@ expand_homedir (const char *remainder) return NULL; } - while (is_path_separator (remainder[0])) + while (remainder[0] && is_path_component_or_null (remainder[0])) remainder++; if (remainder[0] == '\0') remainder = NULL; @@ -127,7 +121,7 @@ expand_homedir (const char *remainder) /* Expand $XDG_CONFIG_HOME */ if (remainder != NULL && strncmp (remainder, ".config", 7) == 0 && - is_path_separator_or_null (remainder[7])) { + is_path_component_or_null (remainder[7])) { env = getenv ("XDG_CONFIG_HOME"); if (env && env[0]) return p11_path_build (env, remainder + 8, NULL); @@ -180,7 +174,7 @@ p11_path_expand (const char *path) return_val_if_fail (path != NULL, NULL); if (strncmp (path, "~", 1) == 0 && - is_path_separator_or_null (path[1])) { + is_path_component_or_null (path[1])) { return expand_homedir (path + 1); } else { @@ -241,27 +235,15 @@ p11_path_build (const char *path, while (path != NULL) { num = strlen (path); - /* Trim beginning of path */ - while (is_path_separator (path[0])) { - /* But preserve the leading path component */ - if (!at && !is_path_separator (path[1])) - break; - path++; - num--; - } - /* Trim end of the path */ until = (at > 0) ? 0 : 1; - while (num > until && is_path_separator_or_null (path[num - 1])) + while (num > until && is_path_component_or_null (path[num - 1])) num--; if (at != 0) { - if (num == 0) { - path = va_arg (va, const char *); + if (num == 0) continue; - } - if (built[at - 1] != delim) - built[at++] = delim; + built[at++] = delim; } assert (at + num < len); @@ -269,6 +251,10 @@ p11_path_build (const char *path, at += num; path = va_arg (va, const char *); + + /* Trim beginning of path */ + while (path && path[0] && is_path_component_or_null (path[0])) + path++; } va_end (va); @@ -288,17 +274,17 @@ p11_path_parent (const char *path) /* Find the end of the last component */ e = path + strlen (path); - while (e != path && is_path_separator_or_null (*e)) + while (e != path && is_path_component_or_null (*e)) e--; /* Find the beginning of the last component */ - while (e != path && !is_path_separator_or_null (*e)) { + while (e != path && !is_path_component_or_null (*e)) { had = true; e--; } /* Find the end of the last component */ - while (e != path && is_path_separator_or_null (*e)) + while (e != path && is_path_component_or_null (*e)) e--; if (e == path) { @@ -327,7 +313,7 @@ p11_path_prefix (const char *string, return a > b && strncmp (string, prefix, b) == 0 && - is_path_separator_or_null (string[b]); + is_path_component_or_null (string[b]); } void diff --git a/common/test-path.c b/common/test-path.c index cf4a8e3..2eb5444 100644 --- a/common/test-path.c +++ b/common/test-path.c @@ -88,49 +88,23 @@ static void test_build (void) { #ifdef OS_UNIX - assert_str_eq_free ("/", - p11_path_build ("/", NULL)); - assert_str_eq_free ("/", - p11_path_build ("", "//", NULL)); - assert_str_eq_free ("/root", - p11_path_build ("///root///", NULL)); - assert_str_eq_free ("/root", - p11_path_build ("/", "root", NULL)); - assert_str_eq_free ("/root", - p11_path_build ("", "/root", NULL)); - assert_str_eq_free ("/root", - p11_path_build ("/root", "", NULL)); assert_str_eq_free ("/root/second", p11_path_build ("/root", "second", NULL)); assert_str_eq_free ("/root/second", p11_path_build ("/root", "/second", NULL)); assert_str_eq_free ("/root/second", p11_path_build ("/root/", "second", NULL)); - assert_str_eq_free ("/root/second", - p11_path_build ("/root//", "//second/", NULL)); - assert_str_eq_free ("/root/second", - p11_path_build ("/root//", "", "//second/", NULL)); assert_str_eq_free ("/root/second/third", p11_path_build ("/root", "second", "third", NULL)); assert_str_eq_free ("/root/second/third", p11_path_build ("/root", "/second/third", NULL)); #else /* OS_WIN32 */ - assert_str_eq_free ("C:\\root", - p11_path_build ("C:\\", "root", NULL)); - assert_str_eq_free ("C:\\root", - p11_path_build ("", "C:\\root", NULL)); - assert_str_eq_free ("C:\\root", - p11_path_build ("C:\\root", "", NULL)); assert_str_eq_free ("C:\\root\\second", p11_path_build ("C:\\root", "second", NULL)); assert_str_eq_free ("C:\\root\\second", p11_path_build ("C:\\root", "\\second", NULL)); assert_str_eq_free ("C:\\root\\second", p11_path_build ("C:\\root\\", "second", NULL)); - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root\\\\", "\\\\second", NULL)); - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root\\\\", "", "\\\\second", NULL)); assert_str_eq_free ("C:\\root\\second\\third", p11_path_build ("C:\\root", "second", "third", NULL)); assert_str_eq_free ("C:\\root\\second/third", diff --git a/p11-kit/proxy.c b/p11-kit/proxy.c index df18ac0..97c9b09 100644 --- a/p11-kit/proxy.c +++ b/p11-kit/proxy.c @@ -265,7 +265,7 @@ proxy_list_slots (Proxy *py, Mapping *mappings, unsigned int n_mappings) /* Ask module for its slots */ rv = (funcs->C_GetSlotList) (FALSE, NULL, &count); if (rv == CKR_OK && count) { - slots = calloc (count, sizeof (CK_SLOT_ID)); + slots = calloc (sizeof (CK_SLOT_ID), count); rv = (funcs->C_GetSlotList) (FALSE, slots, &count); } @@ -744,7 +744,7 @@ proxy_C_CloseAllSessions (CK_X_FUNCTION_LIST *self, CK_SLOT_ID id) { State *state = (State *)self; - CK_SESSION_HANDLE_PTR to_close = NULL; + CK_SESSION_HANDLE_PTR to_close; CK_RV rv = CKR_OK; Session *sess; CK_ULONG i, count = 0; @@ -756,7 +756,7 @@ proxy_C_CloseAllSessions (CK_X_FUNCTION_LIST *self, rv = CKR_CRYPTOKI_NOT_INITIALIZED; } else { assert (state->px->sessions != NULL); - to_close = calloc (p11_dict_size (state->px->sessions) + 1, sizeof (CK_SESSION_HANDLE)); + to_close = calloc (sizeof (CK_SESSION_HANDLE), p11_dict_size (state->px->sessions)); if (!to_close) { rv = CKR_HOST_MEMORY; } else { diff --git a/trust/anchor.c b/trust/anchor.c index 5ba5065..fab9cf6 100644 --- a/trust/anchor.c +++ b/trust/anchor.c @@ -64,10 +64,9 @@ create_arg_file_parser (void) return_val_if_fail (parser != NULL, NULL); p11_parser_formats (parser, - p11_parser_format_persist, - p11_parser_format_x509, - p11_parser_format_pem, - NULL); + p11_parser_format_x509, + p11_parser_format_pem, + NULL); return parser; }