2018-06-22 11:18:19 -04:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#ifndef TOR_COMPAT_STRING_H
|
|
|
|
#define TOR_COMPAT_STRING_H
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include "lib/cc/compat_compiler.h"
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/* ===== String compatibility */
|
|
|
|
#ifdef _WIN32
|
Don't redefine str(n)casecmp on windows unless they're missing
When we do redefine them, use inline functions instead of #define.
This fixes a latent code problem in our redefinition of these
functions, which was exposed by our refactoring: Previously, we
would #define strcasecmp after string.h was included, so nothing bad
would happen. But when we refactored, we would sometimes #define it
first, which was a problem on mingw, whose headers contain
(approximately):
inline int strcasecmp (const char *a, const char *b)
{ return _stricmp(a,b); }
Our define turned this into:
inline int _stricmp(const char *a, const char *b)
{ return _stricmp(a,b); }
And GCC would correctly infer that this function would loop forever,
rather than actually comparing anything. This caused bug 26594.
Fixes bug 26594; bug not in any released version of Tor.
2018-07-02 11:50:17 -04:00
|
|
|
/* Windows doesn't have str(n)casecmp, but mingw defines it: only define it
|
|
|
|
* ourselves if it's missing. */
|
|
|
|
#ifndef HAVE_STRNCASECMP
|
|
|
|
static inline int strncasecmp(const char *a, const char *b, size_t n);
|
|
|
|
static inline int strncasecmp(const char *a, const char *b, size_t n) {
|
2018-07-05 13:51:48 -04:00
|
|
|
return _strnicmp(a,b,n);
|
Don't redefine str(n)casecmp on windows unless they're missing
When we do redefine them, use inline functions instead of #define.
This fixes a latent code problem in our redefinition of these
functions, which was exposed by our refactoring: Previously, we
would #define strcasecmp after string.h was included, so nothing bad
would happen. But when we refactored, we would sometimes #define it
first, which was a problem on mingw, whose headers contain
(approximately):
inline int strcasecmp (const char *a, const char *b)
{ return _stricmp(a,b); }
Our define turned this into:
inline int _stricmp(const char *a, const char *b)
{ return _stricmp(a,b); }
And GCC would correctly infer that this function would loop forever,
rather than actually comparing anything. This caused bug 26594.
Fixes bug 26594; bug not in any released version of Tor.
2018-07-02 11:50:17 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifndef HAVE_STRCASECMP
|
2018-07-09 11:17:23 -04:00
|
|
|
static inline int strcasecmp(const char *a, const char *b);
|
|
|
|
static inline int strcasecmp(const char *a, const char *b) {
|
2018-07-05 13:51:48 -04:00
|
|
|
return _stricmp(a,b);
|
Don't redefine str(n)casecmp on windows unless they're missing
When we do redefine them, use inline functions instead of #define.
This fixes a latent code problem in our redefinition of these
functions, which was exposed by our refactoring: Previously, we
would #define strcasecmp after string.h was included, so nothing bad
would happen. But when we refactored, we would sometimes #define it
first, which was a problem on mingw, whose headers contain
(approximately):
inline int strcasecmp (const char *a, const char *b)
{ return _stricmp(a,b); }
Our define turned this into:
inline int _stricmp(const char *a, const char *b)
{ return _stricmp(a,b); }
And GCC would correctly infer that this function would loop forever,
rather than actually comparing anything. This caused bug 26594.
Fixes bug 26594; bug not in any released version of Tor.
2018-07-02 11:50:17 -04:00
|
|
|
}
|
|
|
|
#endif
|
2018-06-22 11:18:19 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined __APPLE__
|
|
|
|
/* On OSX 10.9 and later, the overlap-checking code for strlcat would
|
|
|
|
* appear to have a severe bug that can sometimes cause aborts in Tor.
|
|
|
|
* Instead, use the non-checking variants. This is sad.
|
|
|
|
*
|
|
|
|
* See https://trac.torproject.org/projects/tor/ticket/15205
|
|
|
|
*/
|
|
|
|
#undef strlcat
|
|
|
|
#undef strlcpy
|
|
|
|
#endif /* defined __APPLE__ */
|
|
|
|
|
|
|
|
#ifndef HAVE_STRLCAT
|
2018-07-03 12:33:09 +03:00
|
|
|
size_t strlcat(char *dst, const char *src, size_t siz);
|
2018-06-22 11:18:19 -04:00
|
|
|
#endif
|
|
|
|
#ifndef HAVE_STRLCPY
|
2018-07-03 12:33:09 +03:00
|
|
|
size_t strlcpy(char *dst, const char *src, size_t siz);
|
2018-06-22 11:18:19 -04:00
|
|
|
#endif
|
|
|
|
|
2018-06-27 15:28:55 -04:00
|
|
|
char *tor_strtok_r_impl(char *str, const char *sep, char **lasts);
|
|
|
|
#ifdef HAVE_STRTOK_R
|
|
|
|
#define tor_strtok_r(str, sep, lasts) strtok_r(str, sep, lasts)
|
|
|
|
#else
|
|
|
|
#define tor_strtok_r(str, sep, lasts) tor_strtok_r_impl(str, sep, lasts)
|
|
|
|
#endif
|
|
|
|
|
2018-06-22 11:18:19 -04:00
|
|
|
#endif
|