diff --git a/Makefile.am b/Makefile.am index 82bba2cdc..d6b70219b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -426,6 +426,7 @@ libutil_a_SOURCES = \ src/util/StringView.cxx src/util/StringView.hxx \ src/util/ConcatString.hxx \ src/util/AllocatedString.cxx src/util/AllocatedString.hxx \ + src/util/TruncateString.cxx src/util/TruncateString.hxx \ src/util/StringUtil.cxx src/util/StringUtil.hxx \ src/util/StringCompare.cxx src/util/StringCompare.hxx \ src/util/WStringCompare.cxx src/util/WStringCompare.hxx \ diff --git a/src/input/plugins/CdioParanoiaInputPlugin.cxx b/src/input/plugins/CdioParanoiaInputPlugin.cxx index 011ba6751..38de6e125 100644 --- a/src/input/plugins/CdioParanoiaInputPlugin.cxx +++ b/src/input/plugins/CdioParanoiaInputPlugin.cxx @@ -25,7 +25,7 @@ #include "CdioParanoiaInputPlugin.hxx" #include "../InputStream.hxx" #include "../InputPlugin.hxx" -#include "util/StringUtil.hxx" +#include "util/TruncateString.hxx" #include "util/StringCompare.hxx" #include "util/RuntimeError.hxx" #include "util/Domain.hxx" diff --git a/src/java/String.cxx b/src/java/String.cxx index c2525cc20..aff63fcd3 100644 --- a/src/java/String.cxx +++ b/src/java/String.cxx @@ -28,7 +28,7 @@ */ #include "String.hxx" -#include "util/StringUtil.hxx" +#include "util/TruncateString.hxx" char * Java::String::CopyTo(JNIEnv *env, jstring value, diff --git a/src/output/plugins/httpd/IcyMetaDataServer.cxx b/src/output/plugins/httpd/IcyMetaDataServer.cxx index e13521371..2a21dcccd 100644 --- a/src/output/plugins/httpd/IcyMetaDataServer.cxx +++ b/src/output/plugins/httpd/IcyMetaDataServer.cxx @@ -22,7 +22,7 @@ #include "tag/Tag.hxx" #include "util/FormatString.hxx" #include "util/AllocatedString.hxx" -#include "util/StringUtil.hxx" +#include "util/TruncateString.hxx" #include "util/Macros.hxx" #include diff --git a/src/tag/Format.cxx b/src/tag/Format.cxx index 4a35a0692..7a076d50b 100644 --- a/src/tag/Format.cxx +++ b/src/tag/Format.cxx @@ -22,7 +22,7 @@ #include "Tag.hxx" #include "ParseName.hxx" #include "util/format.h" -#include "util/StringUtil.hxx" +#include "util/TruncateString.hxx" #include diff --git a/src/util/StringUtil.cxx b/src/util/StringUtil.cxx index b8851edb6..db6e13e38 100644 --- a/src/util/StringUtil.cxx +++ b/src/util/StringUtil.cxx @@ -21,24 +21,9 @@ #include "CharUtil.hxx" #include "ASCII.hxx" -#include - #include #include -char * -CopyString(char *gcc_restrict dest, const char *gcc_restrict src, - size_t size) noexcept -{ - size_t length = strlen(src); - if (length >= size) - length = size - 1; - - char *p = std::copy_n(src, length, dest); - *p = '\0'; - return p; -} - const char * StripLeft(const char *p) noexcept { diff --git a/src/util/StringUtil.hxx b/src/util/StringUtil.hxx index 0fe131f25..bec90e2bd 100644 --- a/src/util/StringUtil.hxx +++ b/src/util/StringUtil.hxx @@ -24,18 +24,6 @@ #include -/** - * Copy a string. If the buffer is too small, then the string is - * truncated. This is a safer version of strncpy(). - * - * @param size the size of the destination buffer (including the null - * terminator) - * @return a pointer to the null terminator - */ -gcc_nonnull_all -char * -CopyString(char *dest, const char *src, size_t size) noexcept; - /** * Returns a pointer to the first non-whitespace character in the * string, or to the end of the string. diff --git a/src/util/TruncateString.cxx b/src/util/TruncateString.cxx new file mode 100644 index 000000000..16f9cf9fe --- /dev/null +++ b/src/util/TruncateString.cxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2009-2017 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "TruncateString.hxx" + +#include + +#include + +char * +CopyString(char *gcc_restrict dest, const char *gcc_restrict src, + size_t size) noexcept +{ + size_t length = strlen(src); + if (length >= size) + length = size - 1; + + char *p = std::copy_n(src, length, dest); + *p = '\0'; + return p; +} diff --git a/src/util/TruncateString.hxx b/src/util/TruncateString.hxx new file mode 100644 index 000000000..293590f43 --- /dev/null +++ b/src/util/TruncateString.hxx @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2009-2017 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TRUNCATE_STRING_HXX +#define TRUNCATE_STRING_HXX + +#include "Compiler.h" + +#include + +/** + * Copy a string. If the buffer is too small, then the string is + * truncated. This is a safer version of strncpy(). + * + * @param size the size of the destination buffer (including the null + * terminator) + * @return a pointer to the null terminator + */ +gcc_nonnull_all +char * +CopyString(char *dest, const char *src, size_t size) noexcept; + +#endif