Skip to content

Commit

Permalink
Require C99 conformant vsnprintf().
Browse files Browse the repository at this point in the history
MinGW-w64 provides one when building with -std=gnu99.
  • Loading branch information
skullernet authored and Paril committed Dec 10, 2021
1 parent 23ef7b7 commit 47a63f8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
8 changes: 4 additions & 4 deletions inc/shared/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <unistd.h>
#endif

#ifdef _WIN32
#define PRIz "Iu"
#else
#define PRIz "zu"
#endif

#ifdef _WIN32
#define LIBSUFFIX ".dll"
Expand Down Expand Up @@ -103,7 +99,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#ifdef __GNUC__

#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
#define q_printf(f, a) __attribute__((format(gnu_printf, f, a)))
#else
#define q_printf(f, a) __attribute__((format(printf, f, a)))
#endif
#define q_noreturn __attribute__((noreturn))
#define q_noinline __attribute__((noinline))
#define q_malloc __attribute__((malloc))
Expand Down
4 changes: 2 additions & 2 deletions src/common/net/win.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ static ioentry_t *os_add_io(qsocket_t fd)

if (i == io_numfds) {
if (++io_numfds > FD_SETSIZE)
Com_Error(ERR_FATAL, "%s: no more space for fd: %Id", __func__, fd);
Com_Error(ERR_FATAL, "%s: no more space for fd: %" PRIdPTR, __func__, fd);
}

e->fd = fd;
Expand All @@ -360,7 +360,7 @@ static ioentry_t *os_get_io(qsocket_t fd)
return e;
}

Com_Error(ERR_FATAL, "%s: fd not found: %Id", __func__, fd);
Com_Error(ERR_FATAL, "%s: fd not found: %" PRIdPTR, __func__, fd);
return NULL;
}

Expand Down
10 changes: 2 additions & 8 deletions src/common/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,10 @@ typedef struct {
const char *res;
} snprintf_test_t;

#ifdef _WIN32
#define OV SIZE_MAX
#else
#define OV 11
#endif

static const snprintf_test_t snprintf_tests[] = {
{ 12, 11, 11, 0, 0, "hello world" },
{ 11, OV, 10, 1, 0, "hello worl" },
{ 10, OV, 9, 1, 0, "hello wor" },
{ 11, 11, 10, 1, 0, "hello worl" },
{ 10, 11, 9, 1, 0, "hello wor" },
{ 0, 11, 0, 1, 1, "xxxxxxxxxxxxxxx" },
};

Expand Down
20 changes: 2 additions & 18 deletions src/shared/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,6 @@ Q_vsnprintf
Returns number of characters that would be written into the buffer,
excluding trailing '\0'. If the returned value is equal to or greater than
buffer size, resulting string is truncated.
WARNING: On Win32, until MinGW-w64 vsnprintf() bug is fixed, this may return
SIZE_MAX on overflow. Only use return value to test for overflow, don't use
it to allocate memory.
===============
*/
size_t Q_vsnprintf(char *dest, size_t size, const char *fmt, va_list argptr)
Expand All @@ -749,17 +745,9 @@ size_t Q_vsnprintf(char *dest, size_t size, const char *fmt, va_list argptr)
if (size > INT_MAX)
Com_Error(ERR_FATAL, "%s: bad buffer size", __func__);

#ifdef _WIN32
if (size) {
ret = _vsnprintf(dest, size - 1, fmt, argptr);
if (ret < 0 || ret >= size - 1)
dest[size - 1] = 0;
} else {
ret = _vscprintf(fmt, argptr);
}
#else
ret = vsnprintf(dest, size, fmt, argptr);
#endif
if (ret < 0)
Com_Error(ERR_FATAL, "%s: bad return value", __func__);

return ret;
}
Expand Down Expand Up @@ -790,10 +778,6 @@ Q_snprintf
Returns number of characters that would be written into the buffer,
excluding trailing '\0'. If the returned value is equal to or greater than
buffer size, resulting string is truncated.
WARNING: On Win32, until MinGW-w64 vsnprintf() bug is fixed, this may return
SIZE_MAX on overflow. Only use return value to test for overflow, don't use
it to allocate memory.
===============
*/
size_t Q_snprintf(char *dest, size_t size, const char *fmt, ...)
Expand Down

0 comments on commit 47a63f8

Please sign in to comment.