[m-rev.] for review: Do not use _snprintf functions directly in place of snprintf functions.
jfischer at opturion.com
Thu Jul 19 16:44:34 AEST 2018
On Thu, 19 Jul 2018, Peter Wang wrote:
> Not yet tested, any objections to the basic idea?
> The Windows _snprintf family of functions do not guarantee null
> termination when the output is truncated so cannot be used as direct
> replacements for the snprintf functions. Also, the _snprintf functions
> have different return values from the C99 snprintf functions when output
> is truncated (like some older snprintf implementations).
> Furthermore, on Windows snprintf/vsnprintf may be synonyms for
> _snprintf/_vsnprintf so cannot be relied upon to terminate their outputs
> either, even if the functions exist.
> Define MR_snprintf and MR_vsnprintf as macro synonyms for
> snprintf/vsnprintf ONLY if _snprintf/_vsnprintf do not exist.
> Otherwise, implement MR_snprintf and MR_vsnprintf functions
> that behave like the C99 functions, in terms of _vsnprintf.
> Require that either snprintf/vsnprintf or _snprintf/_vsnprintf
> are available. This should be true on all systems still in use.
> diff --git a/runtime/mercury_string.h b/runtime/mercury_string.h
> index e60b6846e..c2f33bc53 100644
> --- a/runtime/mercury_string.h
> +++ b/runtime/mercury_string.h
> @@ -6,20 +6,43 @@
> // mercury_string.h - string handling
> #ifndef MERCURY_STRING_H
> #define MERCURY_STRING_H
> #include "mercury_heap.h" // for MR_offset_incr_hp_atomic
> #include <string.h> // for strcmp() etc.
> #include <stdarg.h>
> +#include <stdio.h>
> +// On Windows, snprintf/vsnprintf may be synonyms for _snprintf/_vsnprintf and
> +// thus not conform to the C99 specification. Since it is next to impossible to
> +// tell at compile time which implementation we are getting, just define a
> +// wrapper over _vsnprintf if it exists.
It (apparently) does conform to C99 from Windows 10 / VS2015. You may
want to add an note to the effect that we can drop all this nonsense
when support for older Windows is eventually dropped.
More information about the reviews