diff options
author | Thomas Haller <thaller@redhat.com> | 2019-07-08 19:28:01 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-07-08 22:13:17 +0200 |
commit | 3d35ea645e297d80968770b7663ce0ee1607f78a (patch) | |
tree | ed408bb7b85e61be096d271d3fee84d6716155a2 | |
parent | c6106672861f9a188469f7e490cc38af60943a10 (diff) | |
download | NetworkManager-3d35ea645e297d80968770b7663ce0ee1607f78a.tar.gz |
libnm/crypto: use memmem() instead of naive O(n*m) search in find_tag()
-rw-r--r-- | libnm-core/nm-crypto.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/libnm-core/nm-crypto.c b/libnm-core/nm-crypto.c index a708dce807..98ac6844de 100644 --- a/libnm-core/nm-crypto.c +++ b/libnm-core/nm-crypto.c @@ -116,21 +116,23 @@ find_tag (const char *tag, gsize start_at, gsize *out_pos) { - gsize i, taglen; - gsize len = data_len - start_at; + const guint8 *p; + gsize taglen; - g_return_val_if_fail (out_pos != NULL, FALSE); + nm_assert (out_pos); + nm_assert (start_at <= data_len); taglen = strlen (tag); - if (len >= taglen) { - for (i = 0; i < len - taglen + 1; i++) { - if (memcmp (data + start_at + i, tag, taglen) == 0) { - *out_pos = start_at + i; - return TRUE; - } - } - } - return FALSE; + + p = memmem (&data[start_at], data_len - start_at, tag, taglen); + if (!p) + return FALSE; + + *out_pos = p - data; + + nm_assert (memcmp (&data[*out_pos], tag, taglen) == 0); + + return TRUE; } #define DEK_INFO_TAG "DEK-Info: " |