Changeset 4300

Show
Ignore:
Timestamp:
03/09/10 20:43:13 (6 months ago)
Author:
matwad
Message:

Move extended strtod to string.c, also renamed it from strntod to strtod_ex as it does not have any limit argument.
Fixed buffer overrun when copying into temp buffer

Location:
trunk/showtime/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/showtime/src/htsmsg/htsmsg_json.c

    r3591 r4300  
    2626#include "htsmsg_json.h" 
    2727#include "htsbuf.h" 
     28#include "misc/string.h" 
    2829 
    2930/** 
     
    331332} 
    332333 
    333 /*  
    334  * locale independent strtod. 
    335  * does not support hex floats as the standard strtod 
    336  */ 
    337 static double 
    338 _strntod(const char *s, char decimal_point_char, char **ep) 
    339 { 
    340   static char locale_decimal_point_char = 0; 
    341   char buf[64]; 
    342   const char *c; 
    343   double d; 
    344    
    345   /* ugly but very portable way to get decimal point char */  
    346   if(locale_decimal_point_char == 0) { 
    347     snprintf(buf, sizeof(buf), "%f", 0.0); 
    348     locale_decimal_point_char = buf[1]; 
    349     assert(locale_decimal_point_char != 0); 
    350   } 
    351    
    352   for(c = s;  
    353       *c != '\0' && 
    354       ((*c > 0 && *c < 33) || /* skip whitespace */ 
    355        (*c == decimal_point_char || strchr("+-0123456789", *c) != NULL)); c++) 
    356     ; 
    357    
    358   strncpy(buf, s, c - s);  
    359   buf[c - s] = '\0'; 
    360    
    361   /* replace if specified char is not same as current locale */ 
    362   if(decimal_point_char != locale_decimal_point_char) { 
    363     char *r = strchr(buf, decimal_point_char); 
    364     if(r != NULL) 
    365       *r = locale_decimal_point_char; 
    366   } 
    367    
    368   d = strtod(buf, ep); 
    369    
    370   /* figure out offset in original string */ 
    371   if(ep != NULL) 
    372     *ep = (char *)s + (*ep - buf); 
    373    
    374   return d; 
    375 } 
    376  
    377334/** 
    378335 * 
     
    382339{ 
    383340  char *ep; 
    384   double d = _strntod(s, '.', &ep); 
     341  double d = strtod_ex(s, '.', &ep); 
    385342 
    386343  if(ep == s) 
  • trunk/showtime/src/misc/string.c

    r4296 r4300  
    2121#include <stdlib.h> 
    2222#include <string.h> 
     23#include <assert.h> 
    2324 
    2425#include <libavutil/avstring.h> 
     
    489490} 
    490491 
     492/*  
     493 * locale independent strtod. 
     494 * does not support hex floats as the standard strtod 
     495 */ 
     496double 
     497strtod_ex(const char *s, char decimal_point_char, char **ep) 
     498{ 
     499  static char locale_decimal_point_char = 0; 
     500  char buf[64]; 
     501  const char *c; 
     502  double d; 
     503   
     504  /* ugly but very portable way to get local decimal point char */  
     505  if(locale_decimal_point_char == 0) { 
     506    snprintf(buf, sizeof(buf), "%f", 0.0); 
     507    locale_decimal_point_char = buf[1]; 
     508    assert(locale_decimal_point_char != 0); 
     509  } 
     510   
     511  for(c = s;  
     512      *c != '\0' && 
     513      ((*c > 0 && *c < 33) || /* skip whitespace */ 
     514       (*c == decimal_point_char || strchr("+-0123456789", *c) != NULL)); c++) 
     515    ; 
     516   
     517  int n = MIN(sizeof(buf) - 1, c - s); 
     518  strncpy(buf, s, n);  
     519  buf[n] = '\0'; 
     520   
     521  /* replace if specified char is not same as current locale */ 
     522  if(decimal_point_char != locale_decimal_point_char) { 
     523    char *r = strchr(buf, decimal_point_char); 
     524    if(r != NULL) 
     525      *r = locale_decimal_point_char; 
     526  } 
     527   
     528  d = strtod(buf, ep); 
     529   
     530  /* figure out offset in original string */ 
     531  if(ep != NULL) 
     532    *ep = (char *)s + (*ep - buf); 
     533   
     534  return d; 
     535} 
     536 
  • trunk/showtime/src/misc/string.h

    r4296 r4300  
    1818          const char *url); 
    1919 
     20double strtod_ex(const char *s, char decimal_point_char, char **ep); 
     21 
     22 
    2023#endif