X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/4e4e5a6f2694187498445a6ac6f1634ce8141119..1df5f87f1309a8daa30dabdee855f48ae40d14ab:/wtf/StringExtras.h diff --git a/wtf/StringExtras.h b/wtf/StringExtras.h index 28e80b8..371e33b 100644 --- a/wtf/StringExtras.h +++ b/wtf/StringExtras.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Apple Inc. All rights reserved. + * Copyright (C) 2006, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -28,6 +28,7 @@ #include #include +#include #if HAVE(STRINGS_H) #include @@ -43,17 +44,30 @@ inline int snprintf(char* buffer, size_t count, const char* format, ...) va_start(args, format); result = _vsnprintf(buffer, count, format, args); va_end(args); + + // In the case where the string entirely filled the buffer, _vsnprintf will not + // null-terminate it, but snprintf must. + if (count > 0) + buffer[count - 1] = '\0'; + return result; } -#if COMPILER(MSVC7_OR_LOWER) || OS(WINCE) - -inline int vsnprintf(char* buffer, size_t count, const char* format, va_list args) +inline double wtf_vsnprintf(char* buffer, size_t count, const char* format, va_list args) { - return _vsnprintf(buffer, count, format, args); + int result = _vsnprintf(buffer, count, format, args); + + // In the case where the string entirely filled the buffer, _vsnprintf will not + // null-terminate it, but vsnprintf must. + if (count > 0) + buffer[count - 1] = '\0'; + + return result; } -#endif +// Work around a difference in Microsoft's implementation of vsnprintf, where +// vsnprintf does not null terminate the buffer. WebKit can rely on the null termination. +#define vsnprintf(buffer, count, format, args) wtf_vsnprintf(buffer, count, format, args) #if OS(WINCE) @@ -86,8 +100,7 @@ inline int strcasecmp(const char* s1, const char* s2) #endif -#if OS(WINDOWS) || OS(LINUX) || OS(SOLARIS) -// FIXME: should check HAVE_STRNSTR +#if !HAVE(STRNSTR) inline char* strnstr(const char* buffer, const char* target, size_t bufferLength) {