pike.git / src / operators.c

version» Context lines:

pike.git/src/operators.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: operators.c,v 1.240 2008/08/24 21:32:00 srb Exp $ + || $Id: operators.c,v 1.241 2008/08/26 18:34:44 srb Exp $   */      #include "global.h"   #include <math.h>   #include "interpret.h"   #include "svalue.h"   #include "multiset.h"   #include "mapping.h"   #include "array.h"   #include "stralloc.h"
pike.git/src/operators.c:41:      /* The destructive multiset merge code is broken.    * l->msd gets -1 refs.    *    * Disable it for now.    * /grubba 2008-07-08    */   #undef PIKE_MERGE_DESTR_A   #define PIKE_MERGE_DESTR_A 0    +  /* These calculations should always give some margin based on the size. */ +  /* One extra char for the sign. */ + #define MAX_INT_SPRINTF_LEN (1 + (SIZEOF_INT_TYPE * 5 + 1) / 2) +  + #define MAX_FLOAT_PREC_LEN ((SIZEOF_FLOAT_TYPE * 5 + 1) / 2) +  /* Six extra chars: Mantissa sign, decimal point, zero before the +  * decimal point, the 'e', exponent sign, and an extra digit due +  * to the mantissa/exponent split. */ + #define MAX_FLOAT_SPRINTF_LEN (6 + MAX_FLOAT_PREC_LEN) +  + #define MAX(a,b) ((a)>(b)?(a):(b)) +  +  /* Enough to hold a Pike float or int in textform including a trailing \0 +  */ + #define MAX_NUM_BUF (MAX(MAX_INT_SPRINTF_LEN,MAX_FLOAT_SPRINTF_LEN)+1) +    void index_no_free(struct svalue *to,struct svalue *what,struct svalue *ind)   {   #ifdef PIKE_SECURITY    if(what->type <= MAX_COMPLEX)    if(!CHECK_DATA_SECURITY(what->u.array, SECURITY_BIT_INDEX))    Pike_error("Index permission denied.\n");   #endif       switch(what->type)    {
pike.git/src/operators.c:336:    break;       default:    Pike_error("Cannot cast %s to int.\n", get_name_of_type(sp[-1].type));    }   }      /* Special case for casting to string. */   PMOD_EXPORT void o_cast_to_string(void)   { -  char buf[200]; +  char buf[MAX_NUM_BUF];    switch(sp[-1].type)    { -  case PIKE_T_STRING: -  return; -  +     case T_OBJECT:    if(!sp[-1].u.object->prog) {    /* Casting a destructed object should be like casting a zero. */    pop_stack();    push_constant_text("0");    } else {    {    struct object *o = sp[-1].u.object;    int f = FIND_LFUN(o->prog->inherits[sp[-1].subtype].prog, LFUN_CAST);    if(f == -1)
pike.git/src/operators.c:382:    pop_stack();    if(f) return;    }    }    Pike_error("Cast failed, wanted string, got %s\n",    get_name_of_type(sp[-1].type));    }    }    return;    -  case T_INT: -  sprintf(buf, "%"PRINTPIKEINT"d", sp[-1].u.integer); -  break; -  +     case T_ARRAY:    {    int i;    struct array *a = sp[-1].u.array;    struct pike_string *s;    int shift = 0;       for(i = a->size; i--; ) {    INT_TYPE val;    if (a->item[i].type != T_INT) {
pike.git/src/operators.c:457:    }    }    break;    }    s = end_shared_string(s);    pop_stack();    push_string(s);    }    return;    +  default: +  Pike_error("Cannot cast %s to string.\n", get_name_of_type(sp[-1].type)); +  +  case PIKE_T_STRING: +  return; +     case T_FLOAT: -  sprintf(buf, "%f", (double)sp[-1].u.float_number); +  sprintf(buf,"%.*"PRINTPIKEFLOAT"g", +  MAX_FLOAT_PREC_LEN, sp[-1].u.float_number);    break;    -  default: -  Pike_error("Cannot cast %s to string.\n", get_name_of_type(sp[-1].type)); +  case T_INT: +  sprintf(buf, "%"PRINTPIKEINT"d", sp[-1].u.integer); +  break;    }       sp[-1].type = PIKE_T_STRING;    sp[-1].u.string = make_shared_string(buf);   }      PMOD_EXPORT void o_cast(struct pike_type *type, INT32 run_time_type)   {    if(run_time_type != sp[-1].type)    {
pike.git/src/operators.c:1511:       break;    }       case BIT_STRING | BIT_INT:    case BIT_STRING | BIT_FLOAT:    case BIT_STRING | BIT_FLOAT | BIT_INT:    {    struct pike_string *r;    PCHARP buf; -  char buffer[50]; +  char buffer[MAX_NUM_BUF];    int max_shift=0;       if ((sp[-args].type != T_STRING) && (sp[1-args].type != T_STRING)) {    struct svalue *save_sp = sp;    /* We need to perform a normal addition first.    */    for (e=-args; e < 0; e++) {    if (save_sp[e].type == T_STRING)    break;    *(sp++) = save_sp[e];
pike.git/src/operators.c:1549:    }   #endif /* PIKE_DEBUG */    /* Adjust the stack. */    save_sp[-args] = sp[-1];    sp = save_sp + 1 - args;    return;    } else {    e = -args;    }    -  /* These calculations should always give some margin based on the size. */ -  /* One extra char for the sign. */ - #define MAX_INT_SPRINTF_LEN (1 + (SIZEOF_INT_TYPE * 5 + 1) / 2) -  /* Six extra chars: Mantissa sign, decimal point, zero before the -  * decimal point, the 'e', exponent sign, and an extra digit due -  * to the mantissa/exponent split. */ - #define MAX_FLOAT_SPRINTF_LEN (6 + (SIZEOF_FLOAT_TYPE * 5 + 1) / 2) -  +     size=0;    for(e=-args;e<0;e++)    {    switch(sp[e].type)    {    case T_STRING:    size+=sp[e].u.string->len;    if(sp[e].u.string->size_shift > max_shift)    max_shift=sp[e].u.string->size_shift;    break;
pike.git/src/operators.c:1602: Inside #if defined(PIKE_DEBUG)
   sprintf(buffer,"%"PRINTPIKEINT"d",sp[e].u.integer);   #ifdef PIKE_DEBUG    if (strlen (buffer) > MAX_INT_SPRINTF_LEN)    Pike_fatal ("Formatted integer %s is %"PRINTSIZET"u, "    "longer than assumed max %"PRINTSIZET"u.\n",    buffer, strlen (buffer), MAX_INT_SPRINTF_LEN);   #endif    goto append_buffer;       case T_FLOAT: -  sprintf(buffer,"%"PRINTPIKEFLOAT"f",sp[e].u.float_number); +  sprintf(buffer,"%.*"PRINTPIKEFLOAT"g", +  MAX_FLOAT_PREC_LEN, sp[e].u.float_number);   #ifdef PIKE_DEBUG    if (strlen (buffer) > MAX_FLOAT_SPRINTF_LEN)    Pike_fatal ("Formatted float %s is %"PRINTSIZET"u, "    "longer than assumed max %"PRINTSIZET"u.\n",    buffer, strlen (buffer), MAX_FLOAT_SPRINTF_LEN);   #endif       append_buffer:    switch(max_shift)    {