]> granicus.if.org Git - icu/commitdiff
ICU-21143 Applying non-zero offset to null pointer is undefined behaviour.
authorFredrik Roubert <roubert@google.com>
Tue, 2 Jun 2020 20:35:20 +0000 (22:35 +0200)
committerFredrik Roubert <fredrik@roubert.name>
Thu, 4 Jun 2020 13:13:36 +0000 (15:13 +0200)
The result of pointer end + 1 will not be used if end is nullptr so it
doesn't really matter that the result of this operation is undefined,
but it's therefore also unnecessary to perform the operation at all.

Changing this removes this unnecessary operation and by doing so gives
the undefined behaviour sanitizer one thing less to worry about.

icu4c/source/common/locid.cpp

index 753a452120ee6246e00214e59acd930ee2819807..19b61cd50e2787c117c1879a26b8034af553d10e 100644 (file)
@@ -708,7 +708,7 @@ Locale& Locale::init(const char* localeID, UBool canonicalize)
                     const char* end = begin;
                     // We may have multiple variants, need to look at each of
                     // them.
-                    do {
+                    for (;;) {
                         status = U_ZERO_ERROR;
                         end = uprv_strchr(begin, '_');
                         int32_t len = (end == nullptr) ? int32_t(uprv_strlen(begin)) : int32_t(end - begin);
@@ -739,8 +739,9 @@ Locale& Locale::init(const char* localeID, UBool canonicalize)
                                              uprv_strchr(fullName, '@'), status).data(), false);
                             break;
                         }
+                        if (end == nullptr) break;
                         begin = end + 1;
-                    } while (end != nullptr);
+                    }
                 }  // End of handle language _ variant
                 // Handle cases of key pattern "language _ Script _ REGION"
                 // ex: Map "ks_Arab_IN" to "ks_IN"