Changeset 409

Show
Ignore:
Timestamp:
07/18/06 18:07:49 (2 years ago)
Author:
erikd
Message:

sweep : Replace all usage of sprintf with snprintf.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sweep/trunk/intl/localcharset.c

    r183 r409  
    327327 
    328328  /* Woe32 has a function returning the locale's codepage as a number.  */ 
    329   sprintf (buf, "CP%u", GetACP ()); 
     329  snprintf (buf, sizeof (buf), "CP%u", GetACP ()); 
    330330  codeset = buf; 
    331331 
     
    378378      else 
    379379        { 
    380           sprintf (buf, "CP%u", cp[0]); 
     380          snprintf (buf, sizeof (buf), "CP%u", cp[0]); 
    381381          codeset = buf; 
    382382        } 
  • sweep/trunk/intl/printf.c

    r183 r409  
    126126  va_start (args, format); 
    127127  retval = libintl_vprintf (format, args); 
    128   va_end (args); 
    129   return retval; 
    130 } 
    131  
    132 DLL_EXPORTED 
    133 int 
    134 libintl_vsprintf (char *resultbuf, const char *format, va_list args) 
    135 { 
    136   if (strchr (format, '$') == NULL) 
    137     return vsprintf (resultbuf, format, args); 
    138   else 
    139     { 
    140       size_t length = (size_t) ~0 / (4 * sizeof (char)); 
    141       char *result = libintl_vasnprintf (resultbuf, &length, format, args); 
    142       if (result != resultbuf) 
    143         { 
    144           free (result); 
    145           return -1; 
    146         } 
    147       else 
    148         return length; 
    149     } 
    150 } 
    151  
    152 DLL_EXPORTED 
    153 int 
    154 libintl_sprintf (char *resultbuf, const char *format, ...) 
    155 { 
    156   va_list args; 
    157   int retval; 
    158  
    159   va_start (args, format); 
    160   retval = libintl_vsprintf (resultbuf, format, args); 
    161128  va_end (args); 
    162129  return retval; 
  • sweep/trunk/intl/vasnprintf.c

    r183 r409  
    1 /* vsprintf with automatic memory allocation. 
     1/* vasnprintf with automatic memory allocation. 
    22   Copyright (C) 1999, 2002-2005 Free Software Foundation, Inc. 
    33 
     
    3838#endif 
    3939 
    40 #include <stdio.h>      /* snprintf(), sprintf() */ 
     40#include <stdio.h>      /* snprintf() */ 
    4141#include <stdlib.h>     /* abort(), malloc(), realloc(), free() */ 
    4242#include <string.h>     /* memcpy(), strlen() */ 
     
    149149 
    150150    /* Allocate a small buffer that will hold a directive passed to 
    151        sprintf or snprintf.  */ 
     151       snprintf.  */ 
    152152    buf_neededlength = 
    153153      xsum4 (7, d.max_width_length, d.max_precision_length, 6); 
     
    272272                unsigned int prefix_count; 
    273273                int prefixes[2]; 
    274 #if !USE_SNPRINTF 
    275                 size_t tmp_length; 
    276                 CHAR_T tmpbuf[700]; 
    277                 CHAR_T *tmp; 
    278  
    279                 /* Allocate a temporary buffer of sufficient size for calling 
    280                    sprintf.  */ 
    281                 { 
    282                   size_t width; 
    283                   size_t precision; 
    284  
    285                   width = 0; 
    286                   if (dp->width_start != dp->width_end) 
    287                     { 
    288                       if (dp->width_arg_index != ARG_NONE) 
    289                         { 
    290                           int arg; 
    291  
    292                           if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 
    293                             abort (); 
    294                           arg = a.arg[dp->width_arg_index].a.a_int; 
    295                           width = (arg < 0 ? (unsigned int) (-arg) : arg); 
    296                         } 
    297                       else 
    298                         { 
    299                           const CHAR_T *digitp = dp->width_start; 
    300  
    301                           do 
    302                             width = xsum (xtimes (width, 10), *digitp++ - '0'); 
    303                           while (digitp != dp->width_end); 
    304                         } 
    305                     } 
    306  
    307                   precision = 6; 
    308                   if (dp->precision_start != dp->precision_end) 
    309                     { 
    310                       if (dp->precision_arg_index != ARG_NONE) 
    311                         { 
    312                           int arg; 
    313  
    314                           if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 
    315                             abort (); 
    316                           arg = a.arg[dp->precision_arg_index].a.a_int; 
    317                           precision = (arg < 0 ? 0 : arg); 
    318                         } 
    319                       else 
    320                         { 
    321                           const CHAR_T *digitp = dp->precision_start + 1; 
    322  
    323                           precision = 0; 
    324                           while (digitp != dp->precision_end) 
    325                             precision = xsum (xtimes (precision, 10), *digitp++ - '0'); 
    326                         } 
    327                     } 
    328  
    329                   switch (dp->conversion) 
    330                     { 
    331  
    332                     case 'd': case 'i': case 'u': 
    333 # ifdef HAVE_LONG_LONG 
    334                       if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) 
    335                         tmp_length = 
    336                           (unsigned int) (sizeof (unsigned long long) * CHAR_BIT 
    337                                           * 0.30103 /* binary -> decimal */ 
    338                                           * 2 /* estimate for FLAG_GROUP */ 
    339                                          ) 
    340                           + 1 /* turn floor into ceil */ 
    341                           + 1; /* account for leading sign */ 
    342                       else 
    343 # endif 
    344                       if (type == TYPE_LONGINT || type == TYPE_ULONGINT) 
    345                         tmp_length = 
    346                           (unsigned int) (sizeof (unsigned long) * CHAR_BIT 
    347                                           * 0.30103 /* binary -> decimal */ 
    348                                           * 2 /* estimate for FLAG_GROUP */ 
    349                                          ) 
    350                           + 1 /* turn floor into ceil */ 
    351                           + 1; /* account for leading sign */ 
    352                       else 
    353                         tmp_length = 
    354                           (unsigned int) (sizeof (unsigned int) * CHAR_BIT 
    355                                           * 0.30103 /* binary -> decimal */ 
    356                                           * 2 /* estimate for FLAG_GROUP */ 
    357                                          ) 
    358                           + 1 /* turn floor into ceil */ 
    359                           + 1; /* account for leading sign */ 
    360                       break; 
    361  
    362                     case 'o': 
    363 # ifdef HAVE_LONG_LONG 
    364                       if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) 
    365                         tmp_length = 
    366                           (unsigned int) (sizeof (unsigned long long) * CHAR_BIT 
    367                                           * 0.333334 /* binary -> octal */ 
    368                                          ) 
    369                           + 1 /* turn floor into ceil */ 
    370                           + 1; /* account for leading sign */ 
    371                       else 
    372 # endif 
    373                       if (type == TYPE_LONGINT || type == TYPE_ULONGINT) 
    374                         tmp_length = 
    375                           (unsigned int) (sizeof (unsigned long) * CHAR_BIT 
    376                                           * 0.333334 /* binary -> octal */ 
    377                                          ) 
    378                           + 1 /* turn floor into ceil */ 
    379                           + 1; /* account for leading sign */ 
    380                       else 
    381                         tmp_length = 
    382                           (unsigned int) (sizeof (unsigned int) * CHAR_BIT 
    383                                           * 0.333334 /* binary -> octal */ 
    384                                          ) 
    385                           + 1 /* turn floor into ceil */ 
    386                           + 1; /* account for leading sign */ 
    387                       break; 
    388  
    389                     case 'x': case 'X': 
    390 # ifdef HAVE_LONG_LONG 
    391                       if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) 
    392                         tmp_length = 
    393                           (unsigned int) (sizeof (unsigned long long) * CHAR_BIT 
    394                                           * 0.25 /* binary -> hexadecimal */ 
    395                                          ) 
    396                           + 1 /* turn floor into ceil */ 
    397                           + 2; /* account for leading sign or alternate form */ 
    398                       else 
    399 # endif 
    400                       if (type == TYPE_LONGINT || type == TYPE_ULONGINT) 
    401                         tmp_length = 
    402                           (unsigned int) (sizeof (unsigned long) * CHAR_BIT 
    403                                           * 0.25 /* binary -> hexadecimal */ 
    404                                          ) 
    405                           + 1 /* turn floor into ceil */ 
    406                           + 2; /* account for leading sign or alternate form */ 
    407                       else 
    408                         tmp_length = 
    409                           (unsigned int) (sizeof (unsigned int) * CHAR_BIT 
    410                                           * 0.25 /* binary -> hexadecimal */ 
    411                                          ) 
    412                           + 1 /* turn floor into ceil */ 
    413                           + 2; /* account for leading sign or alternate form */ 
    414                       break; 
    415  
    416                     case 'f': case 'F': 
    417 # ifdef HAVE_LONG_DOUBLE 
    418                       if (type == TYPE_LONGDOUBLE) 
    419                         tmp_length = 
    420                           (unsigned int) (LDBL_MAX_EXP 
    421                                           * 0.30103 /* binary -> decimal */ 
    422                                           * 2 /* estimate for FLAG_GROUP */ 
    423                                          ) 
    424                           + 1 /* turn floor into ceil */ 
    425                           + 10; /* sign, decimal point etc. */ 
    426                       else 
    427 # endif 
    428                         tmp_length = 
    429                           (unsigned int) (DBL_MAX_EXP 
    430                                           * 0.30103 /* binary -> decimal */ 
    431                                           * 2 /* estimate for FLAG_GROUP */ 
    432                                          ) 
    433                           + 1 /* turn floor into ceil */ 
    434                           + 10; /* sign, decimal point etc. */ 
    435                       tmp_length = xsum (tmp_length, precision); 
    436                       break; 
    437  
    438                     case 'e': case 'E': case 'g': case 'G': 
    439                     case 'a': case 'A': 
    440                       tmp_length = 
    441                         12; /* sign, decimal point, exponent etc. */ 
    442                       tmp_length = xsum (tmp_length, precision); 
    443                       break; 
    444  
    445                     case 'c': 
    446 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION 
    447                       if (type == TYPE_WIDE_CHAR) 
    448                         tmp_length = MB_CUR_MAX; 
    449                       else 
    450 # endif 
    451                         tmp_length = 1; 
    452                       break; 
    453  
    454                     case 's': 
    455 # ifdef HAVE_WCHAR_T 
    456                       if (type == TYPE_WIDE_STRING) 
    457                         { 
    458                           tmp_length = 
    459                             local_wcslen (a.arg[dp->arg_index].a.a_wide_string); 
    460  
    461 #  if !WIDE_CHAR_VERSION 
    462                           tmp_length = xtimes (tmp_length, MB_CUR_MAX); 
    463 #  endif 
    464                         } 
    465                       else 
    466 # endif 
    467                         tmp_length = strlen (a.arg[dp->arg_index].a.a_string); 
    468                       break; 
    469  
    470                     case 'p': 
    471                       tmp_length = 
    472                         (unsigned int) (sizeof (void *) * CHAR_BIT 
    473                                         * 0.25 /* binary -> hexadecimal */ 
    474                                        ) 
    475                           + 1 /* turn floor into ceil */ 
    476                           + 2; /* account for leading 0x */ 
    477                       break; 
    478  
    479                     default: 
    480                       abort (); 
    481                     } 
    482  
    483                   if (tmp_length < width) 
    484                     tmp_length = width; 
    485  
    486                   tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ 
    487                 } 
    488  
    489                 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T)) 
    490                   tmp = tmpbuf; 
    491                 else 
    492                   { 
    493                     size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T)); 
    494  
    495                     if (size_overflow_p (tmp_memsize)) 
    496                       /* Overflow, would lead to out of memory.  */ 
    497                       goto out_of_memory; 
    498                     tmp = (CHAR_T *) malloc (tmp_memsize); 
    499                     if (tmp == NULL) 
    500                       /* Out of memory.  */ 
    501                       goto out_of_memory; 
    502                   } 
    503 #endif 
    504  
    505                 /* Construct the format string for calling snprintf or 
    506                    sprintf.  */ 
     274 
     275                /* Construct the format string for calling snprintf.  */ 
    507276                p = buf; 
    508277                *p++ = '%'; 
     
    567336#endif 
    568337 
    569                 /* Construct the arguments for calling snprintf or sprintf.  */ 
     338                /* Construct the arguments for calling snprintf.  */ 
    570339                prefix_count = 0; 
    571340                if (dp->width_arg_index != ARG_NONE) 
     
    615384                                             prefixes[0], prefixes[1], arg, \ 
    616385                                             &count);                       \ 
    617                         break;                                              \ 
    618                       default:                                              \ 
    619                         abort ();                                           \ 
    620                       } 
    621 #else 
    622 # define SNPRINTF_BUF(arg) \ 
    623                     switch (prefix_count)                                   \ 
    624                       {                                                     \ 
    625                       case 0:                                               \ 
    626                         count = sprintf (tmp, buf, arg);                    \ 
    627                         break;                                              \ 
    628                       case 1:                                               \ 
    629                         count = sprintf (tmp, buf, prefixes[0], arg);       \ 
    630                         break;                                              \ 
    631                       case 2:                                               \ 
    632                         count = sprintf (tmp, buf, prefixes[0], prefixes[1],\ 
    633                                          arg);                              \ 
    634386                        break;                                              \ 
    635387                      default:                                              \ 
     
    835587                      } 
    836588 
    837 #if USE_SNPRINTF 
    838                     /* The snprintf() result did fit.  */ 
    839 #else 
    840                     /* Append the sprintf() result.  */ 
    841                     memcpy (result + length, tmp, count * sizeof (CHAR_T)); 
    842                     if (tmp != tmpbuf) 
    843                       free (tmp); 
    844 #endif 
    845  
    846589                    length += count; 
    847590                    break; 
  • sweep/trunk/src/about_dialog.c

    r353 r409  
    8585    gtk_box_pack_start(GTK_BOX(vbox), about_ebox, TRUE, TRUE, 0); 
    8686    gtk_widget_show(about_ebox); 
    87     sprintf(buf2, "%s/sweep_splash.png", PACKAGE_DATA_DIR); 
     87    snprintf(buf2, sizeof (buf2), "%s/sweep_splash.png", PACKAGE_DATA_DIR); 
    8888    about_image = gtk_image_new_from_file (buf2); 
    8989    gtk_container_add(GTK_CONTAINER(about_ebox), about_image);