annotate Modules/_sqlite/util.c @ 82251:bb5a8564e186 2.7

Issue #13153: Tkinter functions now raise TclError instead of ValueError when a unicode argument contains non-BMP character.
author Serhiy Storchaka <storchaka@gmail.com>
date Mon, 18 Feb 2013 13:00:08 +0200
parents 649937bb8f1c
children 44fe1f5b07e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
1 /* util.c - various utility functions
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
2 *
59362
04d3ae52ff73 Merged code from pysqlite 2.6.0.
Gerhard Häring <gh@ghaering.de>
parents: 49154
diff changeset
3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
4 *
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
5 * This file is part of pysqlite.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
6 *
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
7 * This software is provided 'as-is', without any express or implied
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
8 * warranty. In no event will the authors be held liable for any damages
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
9 * arising from the use of this software.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
10 *
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
11 * Permission is granted to anyone to use this software for any purpose,
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
12 * including commercial applications, and to alter it and redistribute it
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
13 * freely, subject to the following restrictions:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
14 *
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
15 * 1. The origin of this software must not be misrepresented; you must not
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
16 * claim that you wrote the original software. If you use this software
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
17 * in a product, an acknowledgment in the product documentation would be
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
18 * appreciated but is not required.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
19 * 2. Altered source versions must be plainly marked as such, and must not be
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
20 * misrepresented as being the original software.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
21 * 3. This notice may not be removed or altered from any source distribution.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
22 */
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
23
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
24 #include "module.h"
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
25 #include "connection.h"
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
26
49154
4c77d1016f60 Fixes issue #3103. In the sqlite3 module, made one more function static. All renaming public symbos now have the pysqlite prefix to avoid name clashes. This at least once created problems where the same symbol name appeared somewhere in Apache and the sqlite3 module was used from mod_python.
Gerhard Häring <gh@ghaering.de>
parents: 45580
diff changeset
27 int pysqlite_step(sqlite3_stmt* statement, pysqlite_Connection* connection)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
28 {
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
29 int rc;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
30
44022
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
31 if (statement == NULL) {
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
32 /* this is a workaround for SQLite 3.5 and later. it now apparently
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
33 * returns NULL for "no-operation" statements */
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
34 rc = SQLITE_OK;
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
35 } else {
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
36 Py_BEGIN_ALLOW_THREADS
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
37 rc = sqlite3_step(statement);
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
38 Py_END_ALLOW_THREADS
52bbe3365c72 - Backported a workaround for a bug in SQLite 3.2.x/3.3.x versions where a
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
39 }
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
40
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
41 return rc;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
42 }
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
43
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
44 /**
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
45 * Checks the SQLite error code and sets the appropriate DB-API exception.
39251
c4ac6ea497a1 Fix spelling.
Georg Brandl <georg@python.org>
parents: 37458
diff changeset
46 * Returns the error code (0 means no error occurred).
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
47 */
45580
b7ee9074ca0e Updated to pysqlite 2.4.1. Documentation additions will come later.
Gerhard Häring <gh@ghaering.de>
parents: 44274
diff changeset
48 int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
49 {
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
50 int errorcode;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
51
45580
b7ee9074ca0e Updated to pysqlite 2.4.1. Documentation additions will come later.
Gerhard Häring <gh@ghaering.de>
parents: 44274
diff changeset
52 /* SQLite often doesn't report anything useful, unless you reset the statement first */
b7ee9074ca0e Updated to pysqlite 2.4.1. Documentation additions will come later.
Gerhard Häring <gh@ghaering.de>
parents: 44274
diff changeset
53 if (st != NULL) {
b7ee9074ca0e Updated to pysqlite 2.4.1. Documentation additions will come later.
Gerhard Häring <gh@ghaering.de>
parents: 44274
diff changeset
54 (void)sqlite3_reset(st);
b7ee9074ca0e Updated to pysqlite 2.4.1. Documentation additions will come later.
Gerhard Häring <gh@ghaering.de>
parents: 44274
diff changeset
55 }
b7ee9074ca0e Updated to pysqlite 2.4.1. Documentation additions will come later.
Gerhard Häring <gh@ghaering.de>
parents: 44274
diff changeset
56
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
57 errorcode = sqlite3_errcode(db);
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
58
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
59 switch (errorcode)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
60 {
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
61 case SQLITE_OK:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
62 PyErr_Clear();
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
63 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
64 case SQLITE_INTERNAL:
37458
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
65 case SQLITE_NOTFOUND:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
66 PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db));
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
67 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
68 case SQLITE_NOMEM:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
69 (void)PyErr_NoMemory();
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
70 break;
37458
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
71 case SQLITE_ERROR:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
72 case SQLITE_PERM:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
73 case SQLITE_ABORT:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
74 case SQLITE_BUSY:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
75 case SQLITE_LOCKED:
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
76 case SQLITE_READONLY:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
77 case SQLITE_INTERRUPT:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
78 case SQLITE_IOERR:
37458
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
79 case SQLITE_FULL:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
80 case SQLITE_CANTOPEN:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
81 case SQLITE_PROTOCOL:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
82 case SQLITE_EMPTY:
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
83 case SQLITE_SCHEMA:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
84 PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db));
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
85 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
86 case SQLITE_CORRUPT:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
87 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
88 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
89 case SQLITE_TOOBIG:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
90 PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db));
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
91 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
92 case SQLITE_CONSTRAINT:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
93 case SQLITE_MISMATCH:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
94 PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db));
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
95 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
96 case SQLITE_MISUSE:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
97 PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db));
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
98 break;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
99 default:
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 39251
diff changeset
100 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
37458
d27058c409df Update to pysqlite 2.2.0
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37362
diff changeset
101 break;
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
102 }
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
103
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
104 return errorcode;
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
105 }
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
106
82054
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
107 #ifdef WORDS_BIGENDIAN
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
108 # define IS_LITTLE_ENDIAN 0
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
109 #else
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
110 # define IS_LITTLE_ENDIAN 1
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
111 #endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
112
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
113 PyObject *
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
114 _pysqlite_long_from_int64(sqlite3_int64 value)
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
115 {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
116 #ifdef HAVE_LONG_LONG
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
117 # if SIZEOF_LONG_LONG < 8
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
118 if (value > PY_LLONG_MAX || value < PY_LLONG_MIN) {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
119 return _PyLong_FromByteArray(&value, sizeof(value),
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
120 IS_LITTLE_ENDIAN, 1 /* signed */);
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
121 }
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
122 # endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
123 # if SIZEOF_LONG < SIZEOF_LONG_LONG
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
124 if (value > LONG_MAX || value < LONG_MIN)
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
125 return PyLong_FromLongLong(value);
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
126 # endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
127 #else
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
128 # if SIZEOF_LONG < 8
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
129 if (value > LONG_MAX || value < LONG_MIN) {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
130 return _PyLong_FromByteArray(&value, sizeof(value),
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
131 IS_LITTLE_ENDIAN, 1 /* signed */);
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
132 }
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
133 # endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
134 #endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
135 return PyInt_FromLong(value);
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
136 }
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
137
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
138 sqlite3_int64
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
139 _pysqlite_long_as_int64(PyObject * py_val)
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
140 {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
141 int overflow;
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
142 #ifdef HAVE_LONG_LONG
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
143 PY_LONG_LONG value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
144 #else
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
145 long value = PyLong_AsLongAndOverflow(py_val, &overflow);
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
146 #endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
147 if (value == -1 && PyErr_Occurred())
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
148 return -1;
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
149 if (!overflow) {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
150 #ifdef HAVE_LONG_LONG
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
151 # if SIZEOF_LONG_LONG > 8
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
152 if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
153 # endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
154 #else
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
155 # if SIZEOF_LONG > 8
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
156 if (-0x8000000000000000L <= value && value <= 0x7FFFFFFFFFFFFFFFL)
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
157 # endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
158 #endif
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
159 return value;
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
160 }
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
161 else if (sizeof(value) < sizeof(sqlite3_int64)) {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
162 sqlite3_int64 int64val;
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
163 if (_PyLong_AsByteArray((PyLongObject *)py_val,
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
164 (unsigned char *)&int64val, sizeof(int64val),
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
165 IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) {
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
166 return int64val;
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
167 }
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
168 }
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
169 PyErr_SetString(PyExc_OverflowError,
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
170 "Python int too large to convert to SQLite INTEGER");
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
171 return -1;
649937bb8f1c Issue #17073: Fix some integer overflows in sqlite3 module.
Serhiy Storchaka <storchaka@gmail.com>
parents: 68256
diff changeset
172 }