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

« back to all changes in this revision

Viewing changes to sql/sql_trigger.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) 2004-2005 MySQL AB
 
1
/*
 
2
   Copyright (c) 2004, 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
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
16
*/
15
17
 
16
18
 
17
19
#define MYSQL_LEX 1
19
21
#include "sp_head.h"
20
22
#include "sql_trigger.h"
21
23
#include "parse_file.h"
 
24
#include <mysys_err.h>
22
25
 
23
26
/*************************************************************************/
24
27
 
293
296
 
294
297
 
295
298
/**
 
299
  An error handler that catches all non-OOM errors which can occur during
 
300
  parsing of trigger body. Such errors are ignored and corresponding error
 
301
  message is used to construct a more verbose error message which contains
 
302
  name of problematic trigger. This error message is later emitted when
 
303
  one tries to perform DML or some of DDL on this table.
 
304
  Also, if possible, grabs name of the trigger being parsed so it can be
 
305
  used to correctly drop problematic trigger.
 
306
*/
 
307
class Deprecated_trigger_syntax_handler : public Internal_error_handler 
 
308
{
 
309
private:
 
310
 
 
311
  char m_message[MYSQL_ERRMSG_SIZE];
 
312
  LEX_STRING *m_trigger_name;
 
313
 
 
314
public:
 
315
 
 
316
  Deprecated_trigger_syntax_handler() : m_trigger_name(NULL) {}
 
317
 
 
318
  virtual bool handle_error(uint sql_errno, const char *message,
 
319
                            MYSQL_ERROR::enum_warning_level level, THD *thd)
 
320
  {
 
321
    if (sql_errno != EE_OUTOFMEMORY &&
 
322
        sql_errno != ER_OUT_OF_RESOURCES)
 
323
    {
 
324
      if(thd->lex->spname)
 
325
        m_trigger_name= &thd->lex->spname->m_name;
 
326
      if (m_trigger_name)
 
327
        my_snprintf(m_message, sizeof(m_message),
 
328
                    "Trigger '%s' has an error in its body: '%s'",
 
329
                    m_trigger_name->str, message);
 
330
      else
 
331
        my_snprintf(m_message, sizeof(m_message),
 
332
                    "Unknown trigger has an error in its body: '%s'",
 
333
                    message);
 
334
      return true;
 
335
    }
 
336
    return false;
 
337
  }
 
338
 
 
339
  LEX_STRING *get_trigger_name() { return m_trigger_name; }
 
340
  char *get_error_message() { return m_message; }
 
341
};
 
342
 
 
343
 
 
344
/**
296
345
  Create or drop trigger for table.
297
346
 
298
347
  @param thd     current thread context (including trigger definition in LEX)
575
624
  LEX_STRING *trg_connection_cl_name;
576
625
  LEX_STRING *trg_db_cl_name;
577
626
 
 
627
  if (check_for_broken_triggers())
 
628
    return true;
578
629
 
579
630
  /* Trigger must be in the same schema as target table. */
580
631
  if (my_strcasecmp(table_alias_charset, table->s->db.str,
848
899
  @param path         char buffer of size FN_REFLEN to be used
849
900
                      for constructing path to .TRN file.
850
901
  @param db           trigger's database name
851
 
  @param table_name   trigger's name
 
902
  @param trigger_name trigger's name
852
903
 
853
904
  @retval
854
905
    False   success
1312
1363
        lex_start(thd);
1313
1364
        thd->spcont= NULL;
1314
1365
 
1315
 
        if (parse_sql(thd, & parser_state, creation_ctx))
1316
 
        {
1317
 
          /* Currently sphead is always deleted in case of a parse error */
1318
 
          DBUG_ASSERT(lex.sphead == 0);
1319
 
          goto err_with_lex_cleanup;
1320
 
        }
 
1366
        Deprecated_trigger_syntax_handler error_handler;
 
1367
        thd->push_internal_handler(&error_handler);
 
1368
        bool parse_error= parse_sql(thd, & parser_state, creation_ctx);
 
1369
        thd->pop_internal_handler();
 
1370
 
1321
1371
        /*
1322
1372
          Not strictly necessary to invoke this method here, since we know
1323
1373
          that we've parsed CREATE TRIGGER and not an
1328
1378
        */
1329
1379
        lex.set_trg_event_type_for_tables();
1330
1380
 
 
1381
        if (parse_error)
 
1382
        {
 
1383
          if (!triggers->m_has_unparseable_trigger)
 
1384
            triggers->set_parse_error_message(error_handler.get_error_message());
 
1385
          /* Currently sphead is always set to NULL in case of a parse error */
 
1386
          DBUG_ASSERT(lex.sphead == 0);
 
1387
          if (error_handler.get_trigger_name())
 
1388
          {
 
1389
            LEX_STRING *trigger_name;
 
1390
            const LEX_STRING *orig_trigger_name= error_handler.get_trigger_name();
 
1391
 
 
1392
            if (!(trigger_name= alloc_lex_string(&table->mem_root)) ||
 
1393
                !(trigger_name->str= strmake_root(&table->mem_root,
 
1394
                                                  orig_trigger_name->str,
 
1395
                                                  orig_trigger_name->length)))
 
1396
              goto err_with_lex_cleanup;
 
1397
 
 
1398
            trigger_name->length= orig_trigger_name->length;
 
1399
 
 
1400
            if (triggers->names_list.push_back(trigger_name,
 
1401
                                               &table->mem_root))
 
1402
              goto err_with_lex_cleanup;
 
1403
          }
 
1404
          else
 
1405
          {
 
1406
            /* 
 
1407
               The Table_triggers_list is not constructed as a list of
 
1408
               trigger objects as one would expect, but rather of lists of
 
1409
               properties of equal length. Thus, even if we don't get the
 
1410
               trigger name, we still fill all in all the lists with
 
1411
               placeholders as we might otherwise create a skew in the
 
1412
               lists. Obviously, this has to be refactored.
 
1413
            */
 
1414
            LEX_STRING *empty= alloc_lex_string(&table->mem_root);
 
1415
            if (!empty)
 
1416
              goto err_with_lex_cleanup;
 
1417
 
 
1418
            empty->str= const_cast<char*>("");
 
1419
            empty->length= 0;
 
1420
            if (triggers->names_list.push_back(empty, &table->mem_root))
 
1421
              goto err_with_lex_cleanup;
 
1422
          }
 
1423
          lex_end(&lex);
 
1424
          continue;
 
1425
        }
 
1426
 
1331
1427
        lex.sphead->set_info(0, 0, &lex.sp_chistics, (ulong) *trg_sql_mode);
1332
1428
 
1333
1429
        int event= lex.trg_chistics.event;
1368
1464
 
1369
1465
        if (triggers->names_list.push_back(&lex.sphead->m_name,
1370
1466
                                           &table->mem_root))
1371
 
            goto err_with_lex_cleanup;
1372
 
 
 
1467
          goto err_with_lex_cleanup;
 
1468
        
1373
1469
        if (!(on_table_name= alloc_lex_string(&table->mem_root)))
1374
1470
          goto err_with_lex_cleanup;
1375
1471
 
1394
1490
        char fname[NAME_LEN + 1];
1395
1491
        DBUG_ASSERT((!my_strcasecmp(table_alias_charset, lex.query_tables->db, db) ||
1396
1492
                     (check_n_cut_mysql50_prefix(db, fname, sizeof(fname)) &&
1397
 
                      !my_strcasecmp(table_alias_charset, lex.query_tables->db, fname))) &&
1398
 
                    (!my_strcasecmp(table_alias_charset, lex.query_tables->table_name,
1399
 
                                    table_name) ||
 
1493
                      !my_strcasecmp(table_alias_charset, lex.query_tables->db, fname))));
 
1494
        DBUG_ASSERT((!my_strcasecmp(table_alias_charset, lex.query_tables->table_name, table_name) ||
1400
1495
                     (check_n_cut_mysql50_prefix(table_name, fname, sizeof(fname)) &&
1401
1496
                      !my_strcasecmp(table_alias_charset, lex.query_tables->table_name, fname))));
1402
1497
#endif
1680
1775
 
1681
1776
    while ((trigger= it_name++))
1682
1777
    {
 
1778
      /*
 
1779
        Trigger, which body we failed to parse during call
 
1780
        Table_triggers_list::check_n_load(), might be missing name.
 
1781
        Such triggers have zero-length name and are skipped here.
 
1782
      */
 
1783
      if (trigger->length == 0)
 
1784
        continue;
1683
1785
      if (rm_trigname_file(path, db, trigger->str))
1684
1786
      {
1685
1787
        /*
1903
2005
  }
1904
2006
  if (table.triggers)
1905
2007
  {
 
2008
    if (table.triggers->check_for_broken_triggers())
 
2009
    {
 
2010
      result= 1;
 
2011
      goto end;
 
2012
    }
1906
2013
    LEX_STRING old_table_name= { (char *) old_table, strlen(old_table) };
1907
2014
    LEX_STRING new_table_name= { (char *) new_table, strlen(new_table) };
1908
2015
    /*
1991
2098
  sp_head *sp_trigger= bodies[event][time_type];
1992
2099
  SELECT_LEX *save_current_select;
1993
2100
 
 
2101
  if (check_for_broken_triggers())
 
2102
    return true;
 
2103
 
1994
2104
  if (sp_trigger == NULL)
1995
2105
    return FALSE;
1996
2106
 
2070
2180
 
2071
2181
 
2072
2182
/**
 
2183
   Signals to the Table_triggers_list that a parse error has occured when
 
2184
   reading a trigger from file. This makes the Table_triggers_list enter an
 
2185
   error state flagged by m_has_unparseable_trigger == true. The error message
 
2186
   will be used whenever a statement invoking or manipulating triggers is
 
2187
   issued against the Table_triggers_list's table.
 
2188
 
 
2189
   @param error_message The error message thrown by the parser.
 
2190
 */
 
2191
void Table_triggers_list::set_parse_error_message(char *error_message)
 
2192
{
 
2193
  m_has_unparseable_trigger= true;
 
2194
  strcpy(m_parse_error_message, error_message);
 
2195
}
 
2196
 
 
2197
 
 
2198
/**
2073
2199
  Trigger BUG#14090 compatibility hook.
2074
2200
 
2075
2201
  @param[in,out] unknown_key       reference on the line with unknown