Skip to content
Snippets Groups Projects
Commit f104f43a authored by chrfranke's avatar chrfranke
Browse files

Added safe_v?snprintf() for platforms with unsafe versions.

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@2021 4ea69e1a-61f1-4043-bf83-b5c94c648137
parent 29e76afe
Branches
No related tags found
No related merge requests found
CHANGELOG for smartmontools
$Id: CHANGELOG,v 1.488 2004/11/06 15:58:53 chrfranke Exp $
$Id: CHANGELOG,v 1.489 2004/11/06 17:51:28 chrfranke Exp $
The most recent version of this file is:
http://cvs.sourceforge.net/viewcvs.py/smartmontools/sm5/CHANGELOG?sortby=date&view=markup
......@@ -31,6 +31,9 @@ NOTES FOR FUTURE RELEASES: see TODO file.
<ADDITIONS TO THE CHANGE LOG SHOULD BE ADDED JUST BELOW HERE, PLEASE>
[CF] Added safe_v?snprintf() for platforms using v?snprintf()
with non standard behaviour on overflow (Windows, old Linux)
[CF] smartd: Added message if check power mode spins up disk.
[CF] Windows: Added support for READ_LOG on WinNT4 using undocumented
......
#
# $Id: configure.in,v 1.108 2004/11/05 21:42:34 chrfranke Exp $
# $Id: configure.in,v 1.109 2004/11/06 17:51:28 chrfranke Exp $
#
dnl Process this file with autoconf to produce a configure script.
AC_PREREQ(2.50)
......@@ -7,7 +7,7 @@ AC_INIT(smartmontools, 5.34, smartmontools-support@lists.sourceforge.net)
AC_CONFIG_SRCDIR(smartctl.c)
smartmontools_configure_date=`date -u +"%Y/%m/%d %T %Z"`
smartmontools_cvs_tag=`echo '$Id: configure.in,v 1.108 2004/11/05 21:42:34 chrfranke Exp $'`
smartmontools_cvs_tag=`echo '$Id: configure.in,v 1.109 2004/11/06 17:51:28 chrfranke Exp $'`
smartmontools_release_date=2004/09/10
smartmontools_release_time="04:11:35 UTC"
......@@ -70,11 +70,23 @@ AC_CHECK_FUNCS([sigset])
AC_CHECK_FUNCS([strtoull])
AC_CHECK_FUNCS([uname])
# Check whether snprintf appends null char and returns expected length on overflow
AH_TEMPLATE(HAVE_WORKING_SNPRINTF, [Define to 1 if the `snprintf' function is sane])
AC_MSG_CHECKING([for working snprintf])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]], [[ char buf[]="ABCDEFGHI";
int i=snprintf(buf,8,"12345678"); return !(!buf[7] && i==8); ]])],
[libc_have_working_snprintf=yes], [libc_have_working_snprintf=no])
AC_SUBST(libc_have_working_snprintf)
if test "$libc_have_working_snprintf" = "yes"; then
AC_DEFINE(HAVE_WORKING_SNPRINTF)
fi
AC_MSG_RESULT([$libc_have_working_snprintf])
# check for __attribute__((packed))
AH_TEMPLATE(HAVE_ATTR_PACKED, [Define to 1 if C compiler supports __attribute__((packed))])
AC_MSG_CHECKING([whether C compiler supports __attribute__((packed))])
AC_TRY_COMPILE(, struct a { int b; } __attribute__((packed));,
gcc_have_attr_packed=yes, gcc_have_attr_packed=no)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(, [[struct a { int b; } __attribute__((packed));]])],
[gcc_have_attr_packed=yes], [gcc_have_attr_packed=no])
AC_SUBST(gcc_have_attr_packed)
if test "$gcc_have_attr_packed" = "yes"; then
AC_DEFINE(HAVE_ATTR_PACKED)
......
......@@ -41,7 +41,7 @@
#include "utility.h"
// Any local header files should be represented by a CVSIDX just below.
const char* utility_c_cvsid="$Id: utility.c,v 1.55 2004/09/16 08:46:04 ballen4705 Exp $"
const char* utility_c_cvsid="$Id: utility.c,v 1.56 2004/11/06 17:51:28 chrfranke Exp $"
CONFIG_H_CVSID INT64_H_CVSID UTILITY_H_CVSID;
const char * packet_types[] = {
......@@ -592,4 +592,32 @@ void MsecToText(unsigned int msec, char *txt){
}
#ifndef HAVE_WORKING_SNPRINTF
// Some versions of (v)snprintf() don't append null char on overflow (MSVCRT.DLL),
// and/or return -1 on overflow (old Linux).
// Below are sane replacements substituted by #define in utility.h.
#undef vsnprintf
#if defined(_WIN32) && defined(_MSC_VER)
#define vsnprintf _vsnprintf
#endif
int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap)
{
if (size <= 0)
return 0;
vsnprintf(buf, size, fmt, ap);
buf[size-1] = 0;
return strlen(buf); // Note: cannot detect for overflow, not necessary here.
}
int safe_snprintf(char *buf, int size, const char *fmt, ...)
{
int i; va_list ap;
va_start(ap, fmt);
i = safe_vsnprintf(buf, size, fmt, ap);
va_end(ap);
return i;
}
#endif
......@@ -41,7 +41,7 @@
#include "utility.h"
// Any local header files should be represented by a CVSIDX just below.
const char* utility_c_cvsid="$Id: utility.cpp,v 1.55 2004/09/16 08:46:04 ballen4705 Exp $"
const char* utility_c_cvsid="$Id: utility.cpp,v 1.56 2004/11/06 17:51:28 chrfranke Exp $"
CONFIG_H_CVSID INT64_H_CVSID UTILITY_H_CVSID;
const char * packet_types[] = {
......@@ -592,4 +592,32 @@ void MsecToText(unsigned int msec, char *txt){
}
#ifndef HAVE_WORKING_SNPRINTF
// Some versions of (v)snprintf() don't append null char on overflow (MSVCRT.DLL),
// and/or return -1 on overflow (old Linux).
// Below are sane replacements substituted by #define in utility.h.
#undef vsnprintf
#if defined(_WIN32) && defined(_MSC_VER)
#define vsnprintf _vsnprintf
#endif
int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap)
{
if (size <= 0)
return 0;
vsnprintf(buf, size, fmt, ap);
buf[size-1] = 0;
return strlen(buf); // Note: cannot detect for overflow, not necessary here.
}
int safe_snprintf(char *buf, int size, const char *fmt, ...)
{
int i; va_list ap;
va_start(ap, fmt);
i = safe_vsnprintf(buf, size, fmt, ap);
va_end(ap);
return i;
}
#endif
......@@ -25,15 +25,19 @@
#ifndef UTILITY_H_
#define UTILITY_H_
#define UTILITY_H_CVSID "$Id: utility.h,v 1.40 2004/10/13 20:18:03 chrfranke Exp $\n"
#define UTILITY_H_CVSID "$Id: utility.h,v 1.41 2004/11/06 17:51:28 chrfranke Exp $\n"
#include <time.h>
#include <sys/types.h> // for regex.h (according to POSIX)
#include <regex.h>
#if defined(_WIN32) && defined(_MSC_VER)
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#ifndef HAVE_WORKING_SNPRINTF
// Substitute by safe replacement functions
#include <stdarg.h>
int safe_snprintf(char *buf, int size, const char *fmt, ...);
int safe_vsnprintf(char *buf, int size, const char *fmt, va_list ap);
#define snprintf safe_snprintf
#define vsnprintf safe_vsnprintf
#endif
// Utility function prints current date and time and timezone into a
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment