diff options
author | Niels Martin Hansen <nielsm@indvikleren.dk> | 2021-02-21 17:03:19 +0100 |
---|---|---|
committer | Niels Martin Hansen <nielsm@indvikleren.dk> | 2021-04-07 09:31:47 +0200 |
commit | e0561dbded57f195e7842cf69764e3ee2c3a71da (patch) | |
tree | 10ae740118601eb102fa73a753bdcdca03607552 /src/os/unix | |
parent | 825867f2c50ce508fac442e6113da9cebbfccf75 (diff) | |
download | openttd-e0561dbded57f195e7842cf69764e3ee2c3a71da.tar.xz |
Fix #8713: Change OTTD2FS and FS2OTTD to return string objects instead of static buffers
Diffstat (limited to 'src/os/unix')
-rw-r--r-- | src/os/unix/unix.cpp | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 9ef2bc386..57edf1d38 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -146,9 +146,8 @@ static const char *GetLocalCode() * Convert between locales, which from and which to is set in the calling * functions OTTD2FS() and FS2OTTD(). */ -static const char *convert_tofrom_fs(iconv_t convd, const char *name) +static const char *convert_tofrom_fs(iconv_t convd, const char *name, char *outbuf, size_t outlen) { - static char buf[1024]; /* There are different implementations of iconv. The older ones, * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g. * IEEE 1003.1 (2004), pass a non-const pointer. */ @@ -158,9 +157,8 @@ static const char *convert_tofrom_fs(iconv_t convd, const char *name) const char *inbuf = name; #endif - char *outbuf = buf; - size_t outlen = sizeof(buf) - 1; size_t inlen = strlen(name); + char *buf = outbuf; strecpy(outbuf, name, outbuf + outlen); @@ -179,9 +177,10 @@ static const char *convert_tofrom_fs(iconv_t convd, const char *name) * @param name pointer to a valid string that will be converted * @return pointer to a new stringbuffer that contains the converted string */ -const char *OTTD2FS(const char *name) +std::string OTTD2FS(const std::string &name) { static iconv_t convd = (iconv_t)(-1); + char buf[1024] = {}; if (convd == (iconv_t)(-1)) { const char *env = GetLocalCode(); @@ -192,17 +191,18 @@ const char *OTTD2FS(const char *name) } } - return convert_tofrom_fs(convd, name); + return convert_tofrom_fs(convd, name.c_str(), buf, lengthof(buf)); } /** * Convert to OpenTTD's encoding from that of the local environment - * @param name pointer to a valid string that will be converted + * @param name valid string that will be converted * @return pointer to a new stringbuffer that contains the converted string */ -const char *FS2OTTD(const char *name) +std::string FS2OTTD(const std::string &name) { static iconv_t convd = (iconv_t)(-1); + char buf[1024] = {}; if (convd == (iconv_t)(-1)) { const char *env = GetLocalCode(); @@ -213,12 +213,9 @@ const char *FS2OTTD(const char *name) } } - return convert_tofrom_fs(convd, name); + return convert_tofrom_fs(convd, name.c_str(), buf, lengthof(buf)); } -#else -const char *FS2OTTD(const char *name) {return name;} -const char *OTTD2FS(const char *name) {return name;} #endif /* WITH_ICONV */ void ShowInfo(const char *str) |