Commit ad396f7c authored by jln's avatar jln Committed by Commit bot

SafeSPrintf: use C++ variadic template

Revert 543be095 and always use C++11
variadic template.

Also add mdempsky@ and jln@ as owners.

Review URL: https://codereview.chromium.org/665483002

Cr-Commit-Position: refs/heads/master@{#299977}
parent fefcac1e
per-file safe_sprintf*=markus@chromium.org
per-file safe_sprintf*=jln@chromium.org
per-file safe_sprintf*=mdempsky@chromium.org
......@@ -218,220 +218,20 @@ BASE_EXPORT size_t GetSafeSPrintfSSizeMaxForTest();
} // namespace internal
// TODO(markus): C++11 has a much more concise and readable solution for
// expressing what we are doing here.
template<class T0, class T1, class T2, class T3, class T4,
class T5, class T6, class T7, class T8, class T9>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N,
class T0, class T1, class T2, class T3, class T4,
class T5, class T6, class T7, class T8, class T9>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2, class T3, class T4,
class T5, class T6, class T7, class T8>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6, T7 arg7, T8 arg8) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N,
class T0, class T1, class T2, class T3, class T4, class T5,
class T6, class T7, class T8>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6, T7 arg7, T8 arg8) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2, class T3, class T4, class T5,
class T6, class T7>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6, T7 arg7) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N,
class T0, class T1, class T2, class T3, class T4, class T5,
class T6, class T7>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6, T7 arg7) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2, class T3, class T4, class T5,
class T6>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
T5 arg5, T6 arg6) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N,
class T0, class T1, class T2, class T3, class T4, class T5,
class T6>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5,
T6 arg6) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = {
arg0, arg1, arg2, arg3, arg4, arg5, arg6
};
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2, class T3, class T4, class T5>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4, arg5 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N,
class T0, class T1, class T2, class T3, class T4, class T5>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4, arg5 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2, class T3, class T4>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N, class T0, class T1, class T2, class T3, class T4>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0, T1 arg1,
T2 arg2, T3 arg3, T4 arg4) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3, arg4 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2, class T3>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N, class T0, class T1, class T2, class T3>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt,
T0 arg0, T1 arg1, T2 arg2, T3 arg3) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2, arg3 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1, class T2>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt,
T0 arg0, T1 arg1, T2 arg2) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N, class T0, class T1, class T2>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0, T1 arg1,
T2 arg2) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1, arg2 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0, class T1>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, T0 arg0, T1 arg1) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<size_t N, class T0, class T1>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0, T1 arg1) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0, arg1 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
}
template<class T0>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, T0 arg0) {
template<typename... Args>
ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt, Args... args) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
const internal::Arg arg_array[] = { args... };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, sizeof...(args));
}
template<size_t N, class T0>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, T0 arg0) {
template<size_t N, typename... Args>
ssize_t SafeSPrintf(char (&buf)[N], const char* fmt, Args... args) {
// Use Arg() object to record type information and then copy arguments to an
// array to make it easier to iterate over them.
const internal::Arg arg_array[] = { arg0 };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, arraysize(arg_array));
const internal::Arg arg_array[] = { args... };
return internal::SafeSNPrintf(buf, N, fmt, arg_array, sizeof...(args));
}
// Fast-path when we don't actually need to substitute any arguments.
......
......@@ -264,6 +264,13 @@ TEST(SafeSPrintfTest, NArgs) {
EXPECT_EQ(10, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c%c%c",
1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf));
EXPECT_EQ(11, SafeSPrintf(buf, "%c%c%c%c%c%c%c%c%c%c%c",
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf));
EXPECT_EQ(11, SafeSNPrintf(buf, 12, "%c%c%c%c%c%c%c%c%c%c%c",
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf));
}
TEST(SafeSPrintfTest, DataTypes) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment