~ubuntu-branches/ubuntu/natty/mysql-5.1/natty-proposed

« back to all changes in this revision

Viewing changes to sql/item_strfunc.cc

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 08:30:45 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120222083045-2rd53r4bnyx7qus4
Tags: 5.1.61-0ubuntu0.11.04.1
* SECURITY UPDATE: Update to 5.1.61 to fix multiple security issues
  (LP: #937869)
  - http://www.oracle.com/technetwork/topics/security/cpujan2012-366304.html
  - CVE-2011-2262
  - CVE-2012-0075
  - CVE-2012-0112
  - CVE-2012-0113
  - CVE-2012-0114
  - CVE-2012-0115
  - CVE-2012-0116
  - CVE-2012-0117
  - CVE-2012-0118
  - CVE-2012-0119
  - CVE-2012-0120
  - CVE-2012-0484
  - CVE-2012-0485
  - CVE-2012-0486
  - CVE-2012-0487
  - CVE-2012-0488
  - CVE-2012-0489
  - CVE-2012-0490
  - CVE-2012-0491
  - CVE-2012-0492
  - CVE-2012-0493
  - CVE-2012-0494
  - CVE-2012-0495
  - CVE-2012-0496

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
 
1
/*
 
2
   Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
3
 
3
4
   This program is free software; you can redistribute it and/or modify
4
5
   it under the terms of the GNU General Public License as published by
11
12
 
12
13
   You should have received a copy of the GNU General Public License
13
14
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
 
15
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
16
*/
16
17
 
17
18
/**
18
19
  @file
39
40
#include "../mysys/my_static.h"                 // For soundex_map
40
41
C_MODE_END
41
42
 
 
43
/**
 
44
   @todo Remove this. It is not safe to use a shared String object.
 
45
 */
42
46
String my_empty_string("",default_charset_info);
43
47
 
44
48
 
461
465
  if ((null_value= args[0]->null_value))
462
466
    return 0;                                   // ENCRYPT(NULL) == NULL
463
467
  if ((res_length=res->length()) == 0)
464
 
    return &my_empty_string;
 
468
    return make_empty_result();
465
469
 
466
470
  if (arg_count == 1)
467
471
  {
516
520
  tmp_arg[res_length-1]=tail;                   // save extra length
517
521
  tmp_value.realloc(res_length+1);
518
522
  tmp_value.length(res_length+1);
 
523
  tmp_value.set_charset(&my_charset_bin);
519
524
  tmp_value[0]=(char) (128 | key_number);
520
525
  // Real encryption
521
526
  bzero((char*) &ivec,sizeof(ivec));
603
608
  if ((tail=(uint) (uchar) tmp_value[length-2]) > 8)
604
609
    goto wrong_key;                                  // Wrong key
605
610
  tmp_value.length(length-1-tail);
 
611
  tmp_value.set_charset(&my_charset_bin);
606
612
  return &tmp_value;
607
613
 
608
614
error:
652
658
    }
653
659
 
654
660
  if (i ==  arg_count)
655
 
    return &my_empty_string;
 
661
    return make_empty_result();
656
662
 
657
663
  for (i++; i < arg_count ; i++)
658
664
  {
803
809
    return 0;
804
810
  /* An empty string is a special case as the string pointer may be null */
805
811
  if (!res->length())
806
 
    return &my_empty_string;
 
812
    return make_empty_result();
807
813
  if (tmp_value.alloced_length() < res->length() &&
808
814
      tmp_value.realloc(res->length()))
809
815
  {
1143
1149
 
1144
1150
  /* if "unsigned_flag" is set, we have a *huge* positive number. */
1145
1151
  if ((length <= 0) && (!args[1]->unsigned_flag))
1146
 
    return &my_empty_string;
1147
 
 
 
1152
    return make_empty_result();
1148
1153
  if ((res->length() <= (ulonglong) length) ||
1149
1154
      (res->length() <= (char_pos= res->charpos((int) length))))
1150
1155
    return res;
1187
1192
 
1188
1193
  /* if "unsigned_flag" is set, we have a *huge* positive number. */
1189
1194
  if ((length <= 0) && (!args[1]->unsigned_flag))
1190
 
    return &my_empty_string; /* purecov: inspected */
 
1195
    return make_empty_result(); /* purecov: inspected */
1191
1196
 
1192
1197
  if (res->length() <= (ulonglong) length)
1193
1198
    return res; /* purecov: inspected */
1226
1231
  /* Negative or zero length, will return empty string. */
1227
1232
  if ((arg_count == 3) && (length <= 0) && 
1228
1233
      (length == 0 || !args[2]->unsigned_flag))
1229
 
    return &my_empty_string;
 
1234
    return make_empty_result();
1230
1235
 
1231
1236
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1232
1237
  /* Set here so that rest of code sees out-of-bound value as such. */
1237
1242
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1238
1243
  if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
1239
1244
      (args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
1240
 
    return &my_empty_string;
 
1245
    return make_empty_result();
1241
1246
 
1242
1247
  start= ((start < 0) ? res->numchars() + start : start - 1);
1243
1248
  start= res->charpos((int) start);
1244
1249
  if ((start < 0) || ((uint) start + 1 > res->length()))
1245
 
    return &my_empty_string;
 
1250
    return make_empty_result();
1246
1251
 
1247
1252
  length= res->charpos((int) length, (uint32) start);
1248
1253
  tmp_length= res->length() - start;
1305
1310
  null_value=0;
1306
1311
  uint delimiter_length= delimiter->length();
1307
1312
  if (!res->length() || !delimiter_length || !count)
1308
 
    return &my_empty_string;            // Wrong parameters
 
1313
    return make_empty_result();         // Wrong parameters
1309
1314
 
1310
1315
  res->set_charset(collation.collation);
1311
1316
 
1654
1659
  if ((null_value=args[0]->null_value))
1655
1660
    return 0;
1656
1661
  if (res->length() == 0)
1657
 
    return &my_empty_string;
 
1662
    return make_empty_result();
1658
1663
  my_make_scrambled_password(tmp_value, res->ptr(), res->length());
1659
1664
  str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, res->charset());
1660
1665
  return str;
1678
1683
  if ((null_value=args[0]->null_value))
1679
1684
    return 0;
1680
1685
  if (res->length() == 0)
1681
 
    return &my_empty_string;
 
1686
    return make_empty_result();
1682
1687
  my_make_scrambled_password_323(tmp_value, res->ptr(), res->length());
1683
1688
  str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, res->charset());
1684
1689
  return str;
1706
1711
  if ((null_value=args[0]->null_value))
1707
1712
    return 0;
1708
1713
  if (res->length() == 0)
1709
 
    return &my_empty_string;
1710
 
 
 
1714
    return make_empty_result();
1711
1715
  if (arg_count == 1)
1712
1716
  {                                     // generate random salt
1713
1717
    time_t timestamp=current_thd->query_start();
1967
1971
  for ( ; ; ) /* Skip pre-space */
1968
1972
  {
1969
1973
    if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
1970
 
      return &my_empty_string; /* EOL or invalid byte sequence */
 
1974
      return make_empty_result(); /* EOL or invalid byte sequence */
1971
1975
    
1972
1976
    if (rc == 1 && cs->ctype)
1973
1977
    {
1992
1996
        {
1993
1997
          /* Extra safety - should not really happen */
1994
1998
          DBUG_ASSERT(false);
1995
 
          return &my_empty_string;
 
1999
          return make_empty_result();
1996
2000
        }
1997
2001
        to+= rc;
1998
2002
        break;
2289
2293
          else
2290
2294
          {
2291
2295
            if (tmp_str.copy(*res))             // Don't use 'str'
2292
 
              return &my_empty_string;
 
2296
              return make_empty_result();
2293
2297
            result= &tmp_str;
2294
2298
          }
2295
2299
        }
2299
2303
          {                                     // Copy data to tmp_str
2300
2304
            if (tmp_str.alloc(result->length()+res->length()+1) ||
2301
2305
                tmp_str.copy(*result))
2302
 
              return &my_empty_string;
 
2306
              return make_empty_result();
2303
2307
            result= &tmp_str;
2304
2308
          }
2305
2309
          if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
2306
 
            return &my_empty_string;
 
2310
            return make_empty_result();
2307
2311
        }
2308
2312
      }
2309
2313
    }
2442
2446
  null_value= 0;
2443
2447
 
2444
2448
  if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
2445
 
    return &my_empty_string;
 
2449
    return make_empty_result();
2446
2450
 
2447
2451
  /* Assumes that the maximum length of a String is < INT_MAX32. */
2448
2452
  /* Bounds check on count:  If this is triggered, we will error. */
2750
2754
 
2751
2755
  ptr= longlong2str(dec, ans, to_base);
2752
2756
  if (str->copy(ans, (uint32) (ptr-ans), default_charset()))
2753
 
    return &my_empty_string;
 
2757
    return make_empty_result();
2754
2758
  return str;
2755
2759
}
2756
2760
 
2760
2764
  DBUG_ASSERT(fixed == 1);
2761
2765
  if (use_cached_value)
2762
2766
    return null_value ? 0 : &str_value;
2763
 
  /* 
2764
 
    Here we don't pass 'str' as a parameter to args[0]->val_str()
2765
 
    as 'str' may point to 'str_value' (e.g. see Item::save_in_field()),
2766
 
    which we use below to convert string. 
2767
 
    Use argument's 'str_value' instead.
2768
 
  */
2769
 
  String *arg= args[0]->val_str(&args[0]->str_value);
 
2767
  String *arg= args[0]->val_str(str);
2770
2768
  uint dummy_errors;
2771
2769
  if (!arg)
2772
2770
  {
2773
2771
    null_value=1;
2774
2772
    return 0;
2775
2773
  }
2776
 
  null_value= str_value.copy(arg->ptr(),arg->length(),arg->charset(),
 
2774
  null_value= tmp_value.copy(arg->ptr(), arg->length(), arg->charset(),
2777
2775
                             conv_charset, &dummy_errors);
2778
 
  return null_value ? 0 : check_well_formed_result(&str_value);
 
2776
  return null_value ? 0 : check_well_formed_result(&tmp_value);
2779
2777
}
2780
2778
 
2781
2779
void Item_func_conv_charset::fix_length_and_dec()
2917
2915
      return 0;
2918
2916
    ptr= longlong2str(dec,ans,16);
2919
2917
    if (str->copy(ans,(uint32) (ptr-ans),default_charset()))
2920
 
      return &my_empty_string;                  // End of memory
 
2918
      return make_empty_result();                       // End of memory
2921
2919
    return str;
2922
2920
  }
2923
2921
 
3217
3215
  }
3218
3216
 
3219
3217
  arg_length= arg->length();
3220
 
  new_length= arg_length+2; /* for beginning and ending ' signs */
3221
3218
 
3222
 
  for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
3223
 
    new_length+= get_esc_bit(escmask, (uchar) *from);
 
3219
  if (collation.collation->mbmaxlen == 1)
 
3220
  {
 
3221
    new_length= arg_length + 2; /* for beginning and ending ' signs */
 
3222
    for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
 
3223
      new_length+= get_esc_bit(escmask, (uchar) *from);
 
3224
  }
 
3225
  else
 
3226
  {
 
3227
    new_length= (arg_length * 2) +  /* For string characters */
 
3228
                (2 * collation.collation->mbmaxlen); /* For quotes */
 
3229
  }
3224
3230
 
3225
3231
  if (tmp_value.alloc(new_length))
3226
3232
    goto null;
3227
3233
 
 
3234
  if (collation.collation->mbmaxlen > 1)
 
3235
  {
 
3236
    CHARSET_INFO *cs= collation.collation;
 
3237
    int mblen;
 
3238
    uchar *to_end;
 
3239
    to= (char*) tmp_value.ptr();
 
3240
    to_end= (uchar*) to + new_length;
 
3241
 
 
3242
    /* Put leading quote */
 
3243
    if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
 
3244
      goto null;
 
3245
    to+= mblen;
 
3246
 
 
3247
    for (start= (char*) arg->ptr(), end= start + arg_length; start < end; )
 
3248
    {
 
3249
      my_wc_t wc;
 
3250
      bool escape;
 
3251
      if ((mblen= cs->cset->mb_wc(cs, &wc, (uchar*) start, (uchar*) end)) <= 0)
 
3252
        goto null;
 
3253
      start+= mblen;
 
3254
      switch (wc) {
 
3255
        case 0:      escape= 1; wc= '0'; break;
 
3256
        case '\032': escape= 1; wc= 'Z'; break;
 
3257
        case '\'':   escape= 1; break;
 
3258
        case '\\':   escape= 1; break;
 
3259
        default:     escape= 0; break;
 
3260
      }
 
3261
      if (escape)
 
3262
      {
 
3263
        if ((mblen= cs->cset->wc_mb(cs, '\\', (uchar*) to, to_end)) <= 0)
 
3264
          goto null;
 
3265
        to+= mblen;
 
3266
      }
 
3267
      if ((mblen= cs->cset->wc_mb(cs, wc, (uchar*) to, to_end)) <= 0)
 
3268
        goto null;
 
3269
      to+= mblen;
 
3270
    }
 
3271
 
 
3272
    /* Put trailing quote */
 
3273
    if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
 
3274
      goto null;
 
3275
    to+= mblen;
 
3276
    new_length= to - tmp_value.ptr();
 
3277
    goto ret;
 
3278
  }
 
3279
 
3228
3280
  /*
3229
3281
    We replace characters from the end to the beginning
3230
3282
  */
3256
3308
    }
3257
3309
  }
3258
3310
  *to= '\'';
 
3311
 
 
3312
ret:
3259
3313
  tmp_value.length(new_length);
3260
3314
  tmp_value.set_charset(collation.collation);
3261
3315
  null_value= 0;
3412
3466
    push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR,
3413
3467
                        ER_TOO_BIG_FOR_UNCOMPRESS,
3414
3468
                        ER(ER_TOO_BIG_FOR_UNCOMPRESS),
3415
 
                        current_thd->variables.max_allowed_packet);
 
3469
                        static_cast<int>(current_thd->variables.
 
3470
                                         max_allowed_packet));
3416
3471
    goto err;
3417
3472
  }
3418
3473
  if (buffer.realloc((uint32)new_size))