]> git.sesse.net Git - vlc/blob - extras/contrib/src/Patches/live-uselocale.patch
live: fix unsafe use of setlocale()
[vlc] / extras / contrib / src / Patches / live-uselocale.patch
1 Copyright (C) 2008 RĂ©mi Denis-Courmont.
2 Licensed under GNU General Public License version 2 or higher.
3 diff -ru live.old/liveMedia/include/Locale.hh live/liveMedia/include/Locale.hh
4 --- live.old/liveMedia/include/Locale.hh        2008-07-06 04:10:57.000000000 +0300
5 +++ live/liveMedia/include/Locale.hh    2008-07-13 12:54:21.000000000 +0300
6 @@ -27,23 +27,26 @@
7  
8  #ifndef LOCALE_NOT_USED
9  #include <locale.h>
10 +#ifdef __APPLE__
11 +#include <xlocale.h>
12 +#endif
13  #else
14 -#ifndef LC_ALL
15 -#define LC_ALL 0
16 +#ifndef LC_ALL_MASK
17 +#define LC_ALL_MASK 0
18  #endif
19 -#ifndef LC_NUMERIC
20 -#define LC_NUMERIC 4
21 +#ifndef LC_NUMERIC_MASK
22 +#define LC_NUMERIC_MASK 0
23  #endif
24 +typedef int locale_t;
25  #endif
26  
27  class Locale {
28  public:
29 -  Locale(char const* newLocale, int category = LC_ALL);
30 +  Locale(char const* newLocale, int category = LC_ALL_MASK);
31    virtual ~Locale();
32  
33  private:
34 -  int fCategory;
35 -  char* fPrevLocale;
36 +  locale_t fLocale, fPrevLocale;
37  };
38  
39  #endif
40 diff -ru live.old/liveMedia/Locale.cpp live/liveMedia/Locale.cpp
41 --- live.old/liveMedia/Locale.cpp       2008-07-06 04:10:57.000000000 +0300
42 +++ live/liveMedia/Locale.cpp   2008-07-13 12:55:32.000000000 +0300
43 @@ -22,19 +22,18 @@
44  #include "Locale.hh"
45  #include <strDup.hh>
46  
47 -Locale::Locale(char const* newLocale, int category)
48 -  : fCategory(category) {
49 +Locale::Locale(char const* newLocale, int category) {
50  #ifndef LOCALE_NOT_USED
51 -  fPrevLocale = strDup(setlocale(category, NULL));
52 -  setlocale(category, newLocale);
53 +  fLocale = newlocale(category, newLocale, NULL);
54 +  fPrevLocale = uselocale(fLocale);
55  #endif
56  }
57  
58  Locale::~Locale() {
59  #ifndef LOCALE_NOT_USED
60 -  if (fPrevLocale != NULL) {
61 -    setlocale(fCategory, fPrevLocale);
62 -    delete[] fPrevLocale;
63 +  if (fLocale != (locale_t)0) {
64 +    uselocale(fPrevLocale);
65 +    freelocale(fLocale);
66    }
67  #endif
68  }
69 diff -ru live.old/liveMedia/RTSPClient.cpp live/liveMedia/RTSPClient.cpp
70 --- live.old/liveMedia/RTSPClient.cpp   2008-07-06 04:10:57.000000000 +0300
71 +++ live/liveMedia/RTSPClient.cpp       2008-07-13 12:53:35.000000000 +0300
72 @@ -1017,7 +1017,7 @@
73      // This is the default value; we don't need a "Scale:" header:
74      buf[0] = '\0';
75    } else {
76 -    Locale("C", LC_NUMERIC);
77 +    Locale("C", LC_NUMERIC_MASK);
78      sprintf(buf, "Scale: %f\r\n", scale);
79    }
80  
81 @@ -1031,11 +1031,11 @@
82      buf[0] = '\0';
83    } else if (end < 0) {
84      // There's no end time:
85 -    Locale("C", LC_NUMERIC);
86 +    Locale("C", LC_NUMERIC_MASK);
87      sprintf(buf, "Range: npt=%.3f-\r\n", start);
88    } else {
89      // There's both a start and an end time; include them both in the "Range:" hdr
90 -    Locale("C", LC_NUMERIC);
91 +    Locale("C", LC_NUMERIC_MASK);
92      sprintf(buf, "Range: npt=%.3f-%.3f\r\n", start, end);
93    }
94  
95 @@ -2306,7 +2306,7 @@
96    if (_strncasecmp(line, "Scale: ", 7) != 0) return False;
97    line += 7;
98  
99 -  Locale("C", LC_NUMERIC);
100 +  Locale("C", LC_NUMERIC_MASK);
101    return sscanf(line, "%f", &scale) == 1;
102  }
103  
104 diff -ru live.old/liveMedia/RTSPCommon.cpp live/liveMedia/RTSPCommon.cpp
105 --- live.old/liveMedia/RTSPCommon.cpp   2008-07-06 04:10:57.000000000 +0300
106 +++ live/liveMedia/RTSPCommon.cpp       2008-07-13 12:53:20.000000000 +0300
107 @@ -146,7 +146,7 @@
108    char const* fields = buf + 7;
109    while (*fields == ' ') ++fields;
110    float start, end;
111 -  Locale("C", LC_NUMERIC);
112 +  Locale("C", LC_NUMERIC_MASK);
113    if (sscanf(fields, "npt = %f - %f", &start, &end) == 2) {
114      rangeStart = start;
115      rangeEnd = end;