]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-id128: avoid one memcpy
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 26 Aug 2023 10:34:34 +0000 (12:34 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 29 Aug 2023 14:06:47 +0000 (17:06 +0300)
By aligning the output buffer, we can just use the result directly, no need to
copy stuff around.

src/libsystemd/sd-id128/sd-id128.c

index 6a82a7f7b890b8b1db308927a806ca0a17907e07..be6da9c88aafa27b9e396aaba8a73f0f85bda818 100644 (file)
@@ -339,17 +339,18 @@ _public_ int sd_id128_randomize(sd_id128_t *ret) {
 }
 
 static int get_app_specific(sd_id128_t base, sd_id128_t app_id, sd_id128_t *ret) {
-        uint8_t hmac[SHA256_DIGEST_SIZE];
-        sd_id128_t result;
+        assert_cc(sizeof(sd_id128_t) < SHA256_DIGEST_SIZE); /* Check that we don't need to pad with zeros. */
+        union {
+                uint8_t hmac[SHA256_DIGEST_SIZE];
+                sd_id128_t result;
+        } buf;
 
         assert(ret);
 
-        hmac_sha256(&base, sizeof(base), &app_id, sizeof(app_id), hmac);
+        hmac_sha256(&base, sizeof(base), &app_id, sizeof(app_id), buf.hmac);
 
         /* Take only the first half. */
-        memcpy(&result, hmac, MIN(sizeof(hmac), sizeof(result)));
-
-        *ret = id128_make_v4_uuid(result);
+        *ret = id128_make_v4_uuid(buf.result);
         return 0;
 }