]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved: avoid passing unitialized variable 19175/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 1 Apr 2021 09:12:57 +0000 (11:12 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 1 Apr 2021 10:02:25 +0000 (12:02 +0200)
The issue was introduced in the refactoring in 775ae35403f8f3c01b7ac13387fe8aac1759993f.
We would pass an initialized value to a helper function. We would only *use*
it if it was initialized. But the mere passing of an unitialized variable is
UB, so let's not do that. This silences a gcc warning.

src/resolve/resolved-dns-cache.c

index b74141e641558263711bc1e644b720455ad8378e..f73ead872dd616efe7c3b799d1118da7dd2bd5e4 100644 (file)
@@ -938,6 +938,8 @@ static int answer_add_clamp_ttl(
         if (FLAGS_SET(query_flags, SD_RESOLVED_CLAMP_TTL)) {
                 uint32_t left_ttl;
 
+                assert(current > 0);
+
                 /* Let's determine how much time is left for this cache entry. Note that we round down, but
                  * clamp this to be 1s at minimum, since we usually want records to remain cached better too
                  * short a time than too long a time, but otoh don't want to return 0 ever, since that has
@@ -988,7 +990,7 @@ int dns_cache_lookup(
         bool nxdomain = false;
         DnsCacheItem *j, *first, *nsec = NULL;
         bool have_authenticated = false, have_non_authenticated = false, have_confidential = false, have_non_confidential = false;
-        usec_t current;
+        usec_t current = 0;
         int found_rcode = -1;
         DnssecResult dnssec_result = -1;
         int have_dnssec_result = -1;
@@ -1014,8 +1016,12 @@ int dns_cache_lookup(
                 goto miss;
         }
 
-        if (FLAGS_SET(query_flags, SD_RESOLVED_CLAMP_TTL))
+        if (FLAGS_SET(query_flags, SD_RESOLVED_CLAMP_TTL)) {
+                /* 'current' is always passed to answer_add_clamp_ttl(), but is only used conditionally.
+                 * We'll do the same assert there to make sure that it was initialized properly. */
                 current = now(clock_boottime_or_monotonic());
+                assert(current > 0);
+        }
 
         LIST_FOREACH(by_key, j, first) {
                 /* If the caller doesn't allow us to answer questions from cache data learned from