Skip to content

Commit

Permalink
Deprecate q(v)snprintf()
Browse files Browse the repository at this point in the history
[ChangeLog][Deprecation Notice][q(v)snprintf()] As warned in advance
in the 6.8 change-log, these functions have now been deprecated with
immediate effect. Due to an unfortunate fallback to
QString::asprintf().toLocal8Bit(), these functions introduce strictly
more platform dependencies than C++11's std::(v)snprintf(), which is
the suggested replacement.

Fixes: QTBUG-127110
Change-Id: Ieee4149ac6c4a9881e1445a77e62edd92c71b36c
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
marcmutz committed Aug 7, 2024
1 parent 73d7138 commit afd0bb2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/corelib/text/qbytearrayalgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,21 @@ Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len);
Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1);

#ifndef QT_NO_QSNPRINTF // use std::(v)snprintf() from <cstdio> instead
#if QT_DEPRECATED_SINCE(6, 9)
#define QSNPF_DEPR(vsn) \
QT_DEPRECATED_VERSION_X_6_9("Use C++11 std::" #vsn "printf() instead, taking care to " \
"ensure that you didn't rely on QString::asprintf() " \
"ideosyncrasies that q" #vsn "printf might, but " \
"std::" #vsn "printf() does not, support.")
// implemented in qvsnprintf.cpp
QSNPF_DEPR(vsn)
Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
Q_ATTRIBUTE_FORMAT_PRINTF(3, 0);
QSNPF_DEPR(sn)
Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...)
Q_ATTRIBUTE_FORMAT_PRINTF(3, 4);
#undef QSNPF_DEPR
#endif // QT_DEPRECATED_SINCE(6, 9)
#endif // QT_NO_QSNPRINTF

// qChecksum: Internet checksum
Expand Down
14 changes: 8 additions & 6 deletions src/corelib/text/qvsnprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ QT_BEGIN_NAMESPACE
\sa qsnprintf(), qvsnprintf().
*/

#if QT_DEPRECATED_SINCE(6, 9)

#if !defined(QT_VSNPRINTF) || defined(Q_QDOC)

/*!
\fn int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
\relates QByteArray
\obsolete
Use C++11's \c{std::vsnprintf()} from \c{<cstdio>} instead.
\deprecated [6.9] Use C++11's \c{std::vsnprintf()} from \c{<cstdio>} instead.
A portable \c vsnprintf() function. Will call \c ::vsnprintf(), \c
::_vsnprintf(), or \c ::vsnprintf_s depending on the system, or
Expand Down Expand Up @@ -89,9 +89,7 @@ int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
\target bytearray-qsnprintf
\relates QByteArray
\obsolete
Use C++11's \c{std::snprintf()} from \c{<cstdio>} instead.
\deprecated [6.9] Use C++11's \c{std::snprintf()} from \c{<cstdio>} instead.
A portable snprintf() function, calls qvsnprintf.
Expand All @@ -111,10 +109,14 @@ int qsnprintf(char *str, size_t n, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);

QT_IGNORE_DEPRECATIONS(
int ret = qvsnprintf(str, n, fmt, ap);
)
va_end(ap);

return ret;
}

#endif // QT_DEPRECATED_SINCE(6, 9)

QT_END_NAMESPACE
7 changes: 7 additions & 0 deletions tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ private slots:
void base64();
void fromBase64_data();
void fromBase64();
#if QT_DEPRECATED_SINCE(6, 9)
void qvsnprintf();
#endif
void qstrlen();
void qstrnlen();
void qstrcpy();
Expand Down Expand Up @@ -679,6 +681,9 @@ void tst_QByteArray::fromBase64()
}
}

#if QT_DEPRECATED_SINCE(6, 9)
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
void tst_QByteArray::qvsnprintf()
{
char buf[20];
Expand Down Expand Up @@ -723,6 +728,8 @@ void tst_QByteArray::qvsnprintf()
QT_WARNING_POP
#endif
}
QT_WARNING_POP
#endif // QT_DEPRECATED_SINCE(6, 9)


void tst_QByteArray::qstrlen()
Expand Down

0 comments on commit afd0bb2

Please sign in to comment.