Skip to content

Commit 655d84c

Browse files
committed
Improve register_hash()
Only go once through `hash_descriptor[]` when calling `register_hash()` Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 278171a commit 655d84c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/misc/crypt/crypt_register_hash.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@
1414
*/
1515
int register_hash(const struct ltc_hash_descriptor *hash)
1616
{
17-
int x;
17+
int x, blank = -1;
1818

1919
LTC_ARGCHK(hash != NULL);
2020

21+
if (hash->name == NULL)
22+
return -1;
23+
2124
/* is it already registered? */
2225
LTC_MUTEX_LOCK(&ltc_hash_mutex);
2326
for (x = 0; x < TAB_SIZE; x++) {
2427
if (XMEMCMP(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) {
2528
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
2629
return x;
2730
}
31+
if (hash_descriptor[x].name == NULL && blank == -1) {
32+
blank = x;
33+
}
2834
}
2935

30-
/* find a blank spot */
31-
for (x = 0; x < TAB_SIZE; x++) {
32-
if (hash_descriptor[x].name == NULL) {
33-
XMEMCPY(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor));
34-
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
35-
return x;
36-
}
36+
/* fill in blank spot */
37+
if (blank != -1) {
38+
XMEMCPY(&hash_descriptor[blank], hash, sizeof(struct ltc_hash_descriptor));
39+
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
40+
return x;
3741
}
3842

3943
/* no spot */

0 commit comments

Comments
 (0)