Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

snprintf.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: snprintf.c,v 12.1 2005/06/16 20:20:50 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <unistd.h>                             /* Declare STDERR_FILENO. */
00019 #endif
00020 
00021 #include "db_int.h"
00022 
00023 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
00024 static void sprintf_overflow __P((void));
00025 static int  sprintf_retcharpnt __P((void));
00026 #endif
00027 
00028 /*
00029  * snprintf --
00030  *      Bounded version of sprintf.
00031  *
00032  * PUBLIC: #ifndef HAVE_SNPRINTF
00033  * PUBLIC: int snprintf __P((char *, size_t, const char *, ...));
00034  * PUBLIC: #endif
00035  */
00036 #ifndef HAVE_SNPRINTF
00037 int
00038 #ifdef STDC_HEADERS
00039 snprintf(char *str, size_t n, const char *fmt, ...)
00040 #else
00041 snprintf(str, n, fmt, va_alist)
00042         char *str;
00043         size_t n;
00044         const char *fmt;
00045         va_dcl
00046 #endif
00047 {
00048         static int ret_charpnt = -1;
00049         va_list ap;
00050         size_t len;
00051 
00052         if (ret_charpnt == -1)
00053                 ret_charpnt = sprintf_retcharpnt();
00054 
00055 #ifdef STDC_HEADERS
00056         va_start(ap, fmt);
00057 #else
00058         va_start(ap);
00059 #endif
00060         len = (size_t)vsprintf(str, fmt, ap);
00061         if (ret_charpnt)
00062                 len = strlen(str);
00063 
00064         va_end(ap);
00065 
00066         if (len >= n) {
00067                 sprintf_overflow();
00068                 /* NOTREACHED */
00069         }
00070         return ((int)len);
00071 }
00072 #endif
00073 
00074 /*
00075  * vsnprintf --
00076  *      Bounded version of vsprintf.
00077  *
00078  * PUBLIC: #ifndef HAVE_VSNPRINTF
00079  * PUBLIC: int vsnprintf __P((char *, size_t, const char *, va_list));
00080  * PUBLIC: #endif
00081  */
00082 #ifndef HAVE_VSNPRINTF
00083 int
00084 vsnprintf(str, n, fmt, ap)
00085         char *str;
00086         size_t n;
00087         const char *fmt;
00088         va_list ap;
00089 {
00090         static int ret_charpnt = -1;
00091         size_t len;
00092 
00093         if (ret_charpnt == -1)
00094                 ret_charpnt = sprintf_retcharpnt();
00095 
00096         len = (size_t)vsprintf(str, fmt, ap);
00097         if (ret_charpnt)
00098                 len = strlen(str);
00099 
00100         if (len >= n) {
00101                 sprintf_overflow();
00102                 /* NOTREACHED */
00103         }
00104         return ((int)len);
00105 }
00106 #endif
00107 
00108 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
00109 static void
00110 sprintf_overflow()
00111 {
00112         /*
00113          * !!!
00114          * We're potentially manipulating strings handed us by the application,
00115          * and on systems without a real snprintf() the sprintf() calls could
00116          * have overflowed the buffer.  We can't do anything about it now, but
00117          * we don't want to return control to the application, we might have
00118          * overwritten the stack with a Trojan horse.  We're not trying to do
00119          * anything recoverable here because systems without snprintf support
00120          * are pretty rare anymore.
00121          */
00122 #define OVERFLOW_ERROR  "internal buffer overflow, process ended\n"
00123 #ifndef STDERR_FILENO
00124 #define STDERR_FILENO   2
00125 #endif
00126         (void)write(STDERR_FILENO, OVERFLOW_ERROR, sizeof(OVERFLOW_ERROR) - 1);
00127 
00128         /* Be polite. */
00129         exit(1);
00130 
00131         /* But firm. */
00132         abort();
00133 
00134         /* NOTREACHED */
00135 }
00136 
00137 static int
00138 sprintf_retcharpnt()
00139 {
00140         int ret_charpnt;
00141         char buf[10];
00142 
00143         /*
00144          * Some old versions of sprintf return a pointer to the first argument
00145          * instead of a character count.  Assume the return value of snprintf,
00146          * vsprintf, etc. will be the same as sprintf, and check the easy one.
00147          *
00148          * We do this test at run-time because it's not a test we can do in a
00149          * cross-compilation environment.
00150          */
00151 
00152         ret_charpnt =
00153             (int)sprintf(buf, "123") != 3 ||
00154             (int)sprintf(buf, "123456789") != 9 ||
00155             (int)sprintf(buf, "1234") != 4;
00156 
00157         return (ret_charpnt);
00158 }
00159 #endif

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2