]> granicus.if.org Git - icu/commitdiff
ICU-12042 ures_getUnicodeString() and variants return bogus strings when an error...
authorMarkus Scherer <markus.icu@gmail.com>
Sat, 9 Jan 2016 00:58:15 +0000 (00:58 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Sat, 9 Jan 2016 00:58:15 +0000 (00:58 +0000)
X-SVN-Rev: 38160

icu4c/source/common/unicode/ures.h
icu4c/source/test/intltest/restsnew.cpp

index c91f030b1004541d13f83d88725f5de983bc4b38..8ceb92d104549048aa765537b9dc2063a4681569 100644 (file)
@@ -1,6 +1,6 @@
 /*
 **********************************************************************
-*   Copyright (C) 1997-2012,2014, International Business Machines
+*   Copyright (C) 1997-2016, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
 *
@@ -799,13 +799,17 @@ U_NAMESPACE_BEGIN
  * @return        a UnicodeString object. If there is an error, string is bogus
  * @stable ICU 2.0
  */
-inline UnicodeString 
-ures_getUnicodeString(const UResourceBundle *resB, 
-                      UErrorCode* status) 
-{
+inline UnicodeString
+ures_getUnicodeString(const UResourceBundle *resB, UErrorCode* status) {
+    UnicodeString result;
     int32_t len = 0;
     const UChar *r = ures_getString(resB, &len, status);
-    return UnicodeString(TRUE, r, len);
+    if(U_SUCCESS(*status)) {
+        result.setTo(TRUE, r, len);
+    } else {
+        result.setToBogus();
+    }
+    return result;
 }
 
 /**
@@ -818,14 +822,17 @@ ures_getUnicodeString(const UResourceBundle *resB,
  * @return an UnicodeString object.
  * @stable ICU 2.0
  */
-inline UnicodeString 
-ures_getNextUnicodeString(UResourceBundle *resB, 
-                          const char ** key, 
-                          UErrorCode* status) 
-{
+inline UnicodeString
+ures_getNextUnicodeString(UResourceBundle *resB, const char ** key, UErrorCode* status) {
+    UnicodeString result;
     int32_t len = 0;
     const UChar* r = ures_getNextString(resB, &len, key, status);
-    return UnicodeString(TRUE, r, len);
+    if(U_SUCCESS(*status)) {
+        result.setTo(TRUE, r, len);
+    } else {
+        result.setToBogus();
+    }
+    return result;
 }
 
 /**
@@ -837,14 +844,17 @@ ures_getNextUnicodeString(UResourceBundle *resB,
  * @return                  an UnicodeString object. If there is an error, string is bogus
  * @stable ICU 2.0
  */
-inline UnicodeString 
-ures_getUnicodeStringByIndex(const UResourceBundle *resB, 
-                             int32_t indexS, 
-                             UErrorCode* status) 
-{
+inline UnicodeString
+ures_getUnicodeStringByIndex(const UResourceBundle *resB, int32_t indexS, UErrorCode* status) {
+    UnicodeString result;
     int32_t len = 0;
     const UChar* r = ures_getStringByIndex(resB, indexS, &len, status);
-    return UnicodeString(TRUE, r, len);
+    if(U_SUCCESS(*status)) {
+        result.setTo(TRUE, r, len);
+    } else {
+        result.setToBogus();
+    }
+    return result;
 }
 
 /**
@@ -857,14 +867,17 @@ ures_getUnicodeStringByIndex(const UResourceBundle *resB,
  * @return                  an UnicodeString object. If there is an error, string is bogus
  * @stable ICU 2.0
  */
-inline UnicodeString 
-ures_getUnicodeStringByKey(const UResourceBundle *resB, 
-                           const char* key, 
-                           UErrorCode* status) 
-{
+inline UnicodeString
+ures_getUnicodeStringByKey(const UResourceBundle *resB, const char* key, UErrorCode* status) {
+    UnicodeString result;
     int32_t len = 0;
     const UChar* r = ures_getStringByKey(resB, key, &len, status);
-    return UnicodeString(TRUE, r, len);
+    if(U_SUCCESS(*status)) {
+        result.setTo(TRUE, r, len);
+    } else {
+        result.setToBogus();
+    }
+    return result;
 }
 
 U_NAMESPACE_END
index e6240c0907ef1bf5cb457918063fa8cf345c387b..e1c78e7851ca76939d56c377c9c6870f2aa09494 100644 (file)
@@ -1,5 +1,5 @@
 /********************************************************************
- * Copyright (c) 1997-2014, International Business Machines
+ * Copyright (c) 1997-2016, International Business Machines
  * Corporation and others. All Rights Reserved.
  ********************************************************************/
 
@@ -597,6 +597,20 @@ NewResourceBundleTest::TestOtherAPI(){
                 }
             }
         }
+
+        // Check that ures_getUnicodeString() & variants return a bogus string if failure.
+        // Same relevant code path whether the failure code is passed in
+        // or comes from a lookup error.
+        UErrorCode failure = U_INTERNAL_PROGRAM_ERROR;
+        assertTrue("ures_getUnicodeString(failure).isBogus()",
+                   ures_getUnicodeString(testCAPI, &failure).isBogus());
+        assertTrue("ures_getNextUnicodeString(failure).isBogus()",
+                   ures_getNextUnicodeString(testCAPI, NULL, &failure).isBogus());
+        assertTrue("ures_getUnicodeStringByIndex(failure).isBogus()",
+                   ures_getUnicodeStringByIndex(testCAPI, 999, &failure).isBogus());
+        assertTrue("ures_getUnicodeStringByKey(failure).isBogus()",
+                   ures_getUnicodeStringByKey(testCAPI, "bogus key", &failure).isBogus());
+
         ures_close(temp);
         ures_close(rowbundle);
         ures_close(bundle);