[Libreoffice-commits] core.git: shell/source

Stephan Bergmann sbergman at redhat.com
Tue Apr 4 12:05:58 UTC 2017


 shell/source/backends/localebe/localebackend.cxx |   46 +++++++++++++----------
 shell/source/backends/localebe/localebackend.hxx |    9 +++-
 2 files changed, 32 insertions(+), 23 deletions(-)

New commits:
commit 9a4b0beaa500320316cbb9147a3300fa2258e217
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Tue Apr 4 11:20:48 2017 +0200

    cid#1403658: Let ImplGetLocale return empty Optional for bad input
    
    ...like non-ASCII or too-long env var content on Linux
    
    Change-Id: Ic2c536fa92b88108e96a0fc1a657180bc1906f01
    Reviewed-on: https://gerrit.libreoffice.org/36067
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/shell/source/backends/localebe/localebackend.cxx b/shell/source/backends/localebe/localebackend.cxx
index 8f518e46de6a..32ab8e63c2bb 100644
--- a/shell/source/backends/localebe/localebackend.cxx
+++ b/shell/source/backends/localebe/localebackend.cxx
@@ -17,11 +17,15 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
+#include <sal/config.h>
+
+#include <limits>
 
 #include "localebackend.hxx"
 #include <com/sun/star/beans/Optional.hpp>
 #include <cppuhelper/supportsservice.hxx>
 #include <osl/time.h>
+#include <rtl/character.hxx>
 
 #include <stdio.h>
 
@@ -34,7 +38,7 @@
 #pragma warning(pop)
 #endif
 
-OUString ImplGetLocale(LCID lcid)
+css::beans::Optional<css::uno::Any> ImplGetLocale(LCID lcid)
 {
     CHAR buffer[8];
     PSTR cp = buffer;
@@ -46,10 +50,10 @@ OUString ImplGetLocale(LCID lcid)
             // #i50822# minus character must be written before cp
             *(cp - 1) = '-';
 
-        return OUString::createFromAscii(buffer);
+        return {true, css::uno::Any(OUString::createFromAscii(buffer))};
     }
 
-    return OUString();
+    return {false, {}};
 }
 
 #elif defined(MACOSX)
@@ -114,7 +118,7 @@ namespace /* private */
         return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
     }
 
-    OUString ImplGetLocale(const char* pref)
+    css::beans::Optional<css::uno::Any> ImplGetLocale(const char* pref)
     {
         CFStringRef sref = ImplGetAppPreference(pref);
         CFStringGuard srefGuard(sref);
@@ -146,7 +150,7 @@ namespace /* private */
                 }
             }
         }
-        return aLocaleBuffer.makeStringAndClear();
+        return {true, css::uno::Any(aLocaleBuffer.makeStringAndClear())};
     }
 
 } // namespace /* private */
@@ -157,7 +161,7 @@ namespace /* private */
 #include <cstdlib>
 #include <cstring>
 
-static OUString ImplGetLocale(char const * category)
+static css::beans::Optional<css::uno::Any> ImplGetLocale(char const * category)
 {
     const char *locale = std::getenv("LC_ALL");
     if (locale == nullptr || *locale == '\0') {
@@ -170,7 +174,7 @@ static OUString ImplGetLocale(char const * category)
     // Return "en-US" for C locales
     if( (locale == nullptr) || *locale == '\0' || std::strcmp(locale, "C") == 0
         || std::strcmp(locale, "POSIX") == 0 )
-        return OUString( "en-US"  );
+        return {true, css::uno::Any(OUString("en-US"))};
 
 
     const char *cp;
@@ -185,6 +189,14 @@ static OUString ImplGetLocale(char const * category)
             uscore = cp;
         if (*cp == '.' || *cp == '@')
             break;
+        if (!rtl::isAscii(static_cast<unsigned char>(*cp))) {
+            SAL_INFO("shell", "locale env var with non-ASCII content");
+            return {false, {}};
+        }
+    }
+    if (cp - locale > std::numeric_limits<sal_Int32>::max()) {
+        SAL_INFO("shell", "locale env var content too long");
+        return {false, {}};
     }
 
     OUStringBuffer aLocaleBuffer;
@@ -199,7 +211,7 @@ static OUString ImplGetLocale(char const * category)
         aLocaleBuffer.appendAscii(locale, cp - locale);
     }
 
-    return aLocaleBuffer.makeStringAndClear();
+    return {true, css::uno::Any(aLocaleBuffer.makeStringAndClear())};
 }
 
 #endif
@@ -221,7 +233,7 @@ LocaleBackend* LocaleBackend::createInstance()
 }
 
 
-OUString LocaleBackend::getLocale()
+css::beans::Optional<css::uno::Any> LocaleBackend::getLocale()
 {
 #if defined(_WIN32)
     return ImplGetLocale( GetUserDefaultLCID() );
@@ -233,7 +245,7 @@ OUString LocaleBackend::getLocale()
 }
 
 
-OUString LocaleBackend::getUILocale()
+css::beans::Optional<css::uno::Any> LocaleBackend::getUILocale()
 {
 #if defined(_WIN32)
     return ImplGetLocale( MAKELCID(GetUserDefaultUILanguage(), SORT_DEFAULT) );
@@ -245,7 +257,7 @@ OUString LocaleBackend::getUILocale()
 }
 
 
-OUString LocaleBackend::getSystemLocale()
+css::beans::Optional<css::uno::Any> LocaleBackend::getSystemLocale()
 {
 // note: the implementation differs from getLocale() only on Windows
 #if defined(_WIN32)
@@ -268,19 +280,13 @@ css::uno::Any LocaleBackend::getPropertyValue(
     OUString const & PropertyName)
 {
     if ( PropertyName == "Locale" ) {
-        return css::uno::makeAny(
-            css::beans::Optional< css::uno::Any >(
-                true, css::uno::makeAny(getLocale())));
+        return css::uno::Any(getLocale());
     } else if (PropertyName == "SystemLocale")
     {
-        return css::uno::makeAny(
-            css::beans::Optional< css::uno::Any >(
-                true, css::uno::makeAny(getSystemLocale())));
+        return css::uno::Any(getSystemLocale());
     } else if (PropertyName == "UILocale")
     {
-        return css::uno::makeAny(
-            css::beans::Optional< css::uno::Any >(
-                true, css::uno::makeAny(getUILocale())));
+        return css::uno::Any(getUILocale());
     } else {
         throw css::beans::UnknownPropertyException(
             PropertyName, static_cast< cppu::OWeakObject * >(this));
diff --git a/shell/source/backends/localebe/localebackend.hxx b/shell/source/backends/localebe/localebackend.hxx
index 2a91d5f3fb44..e9bfcd3ad04c 100644
--- a/shell/source/backends/localebe/localebackend.hxx
+++ b/shell/source/backends/localebe/localebackend.hxx
@@ -20,6 +20,9 @@
 #ifndef INCLUDED_SHELL_SOURCE_BACKENDS_LOCALEBE_LOCALEBACKEND_HXX
 #define INCLUDED_SHELL_SOURCE_BACKENDS_LOCALEBE_LOCALEBACKEND_HXX
 
+#include <sal/config.h>
+
+#include <com/sun/star/beans/Optional.hpp>
 #include <com/sun/star/beans/XPropertySet.hpp>
 #include <com/sun/star/lang/XServiceInfo.hpp>
 #include <cppuhelper/implbase.hxx>
@@ -104,13 +107,13 @@ class LocaleBackend : public ::cppu::WeakImplHelper <
 
     private:
         // Returns the user locale
-        static OUString getLocale();
+        static css::beans::Optional<css::uno::Any> getLocale();
 
         // Returns the user UI locale
-        static OUString getUILocale();
+        static css::beans::Optional<css::uno::Any> getUILocale();
 
         // Returns the system default locale
-        static OUString getSystemLocale();
+        static css::beans::Optional<css::uno::Any> getSystemLocale();
 } ;
 
 


More information about the Libreoffice-commits mailing list