liblivemedia (2010.04.09-1) 022_safe_setlocale.diff

Summary

 liveMedia/Locale.cpp        |   13 ++++++-------
 liveMedia/RTSPClient.cpp    |    8 ++++----
 liveMedia/RTSPCommon.cpp    |    2 +-
 liveMedia/include/Locale.hh |   17 ++++++++++-------
 4 files changed, 21 insertions(+), 19 deletions(-)

    
download this patch

Patch contents

Copyright (C) 2008 Rémi Denis-Courmont, adaptation by Felix Kühne (C) 2009.
Licensed under GNU General Public License version 2 or higher.
Index: liblivemedia/liveMedia/include/Locale.hh
===================================================================
--- liblivemedia.orig/liveMedia/include/Locale.hh	2010-04-20 23:23:59.000000000 +0100
+++ liblivemedia/liveMedia/include/Locale.hh	2010-04-20 23:24:30.000000000 +0100
@@ -27,23 +27,26 @@
 
 #ifndef LOCALE_NOT_USED
 #include <locale.h>
+#ifdef __APPLE__
+#include <xlocale.h>
+#endif
 #else
-#ifndef LC_ALL
-#define LC_ALL 0
+#ifndef LC_ALL_MASK
+#define LC_ALL_MASK 0
 #endif
-#ifndef LC_NUMERIC
-#define LC_NUMERIC 4
+#ifndef LC_NUMERIC_MASK
+#define LC_NUMERIC_MASK 0
 #endif
+typedef int locale_t;
 #endif
 
 class Locale {
 public:
-  Locale(char const* newLocale, int category = LC_ALL);
+  Locale(char const* newLocale, int category = LC_ALL_MASK);
   virtual ~Locale();
 
 private:
-  int fCategory;
-  char* fPrevLocale;
+  locale_t fLocale, fPrevLocale;
 };
 
 #endif
Index: liblivemedia/liveMedia/Locale.cpp
===================================================================
--- liblivemedia.orig/liveMedia/Locale.cpp	2010-04-20 23:23:59.000000000 +0100
+++ liblivemedia/liveMedia/Locale.cpp	2010-04-20 23:24:30.000000000 +0100
@@ -22,19 +22,18 @@
 #include "Locale.hh"
 #include <strDup.hh>
 
-Locale::Locale(char const* newLocale, int category)
-  : fCategory(category) {
+Locale::Locale(char const* newLocale, int category) {
 #ifndef LOCALE_NOT_USED
-  fPrevLocale = strDup(setlocale(category, NULL));
-  setlocale(category, newLocale);
+  fLocale = newlocale(category, newLocale, NULL);
+  fPrevLocale = uselocale(fLocale);
 #endif
 }
 
 Locale::~Locale() {
 #ifndef LOCALE_NOT_USED
-  if (fPrevLocale != NULL) {
-    setlocale(fCategory, fPrevLocale);
-    delete[] fPrevLocale;
+  if (fLocale != (locale_t)0) {
+    uselocale(fPrevLocale);
+    freelocale(fLocale);
   }
 #endif
 }
Index: liblivemedia/liveMedia/RTSPClient.cpp
===================================================================
--- liblivemedia.orig/liveMedia/RTSPClient.cpp	2010-04-20 23:23:59.000000000 +0100
+++ liblivemedia/liveMedia/RTSPClient.cpp	2010-04-20 23:24:30.000000000 +0100
@@ -924,7 +924,7 @@
     // This is the default value; we don't need a "Scale:" header:
     buf[0] = '\0';
   } else {
-    Locale l("C", LC_NUMERIC);
+    Locale l("C", LC_NUMERIC_MASK);
     sprintf(buf, "Scale: %f\r\n", scale);
   }
 
@@ -938,11 +938,11 @@
     buf[0] = '\0';
   } else if (end < 0) {
     // There's no end time:
-    Locale l("C", LC_NUMERIC);
+    Locale l("C", LC_NUMERIC_MASK);
     sprintf(buf, "Range: npt=%.3f-\r\n", start);
   } else {
     // There's both a start and an end time; include them both in the "Range:" hdr
-    Locale l("C", LC_NUMERIC);
+    Locale l("C", LC_NUMERIC_MASK);
     sprintf(buf, "Range: npt=%.3f-%.3f\r\n", start, end);
   }
 
@@ -2225,7 +2225,7 @@
   if (_strncasecmp(line, "Scale: ", 7) != 0) return False;
   line += 7;
 
-  Locale l("C", LC_NUMERIC);
+  Locale l("C", LC_NUMERIC_MASK);
   return sscanf(line, "%f", &scale) == 1;
 }
 
Index: liblivemedia/liveMedia/RTSPCommon.cpp
===================================================================
--- liblivemedia.orig/liveMedia/RTSPCommon.cpp	2010-04-20 23:23:59.000000000 +0100
+++ liblivemedia/liveMedia/RTSPCommon.cpp	2010-04-20 23:24:30.000000000 +0100
@@ -146,7 +146,7 @@
   char const* fields = buf + 7;
   while (*fields == ' ') ++fields;
   double start, end;
-  Locale l("C", LC_NUMERIC);
+  Locale l("C", LC_NUMERIC_MASK);
   if (sscanf(fields, "npt = %lf - %lf", &start, &end) == 2) {
     rangeStart = start;
     rangeEnd = end;