My favorites | Sign in
Project Home Downloads Wiki
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 296 attachment: pyodbc-select-long-strings.patch (1.5 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
diff --git a/src/getdata.cpp b/src/getdata.cpp
old mode 100644
new mode 100755
index bd4232a..fb4c515
--- a/src/getdata.cpp
+++ b/src/getdata.cpp
@@ -329,8 +329,15 @@ static PyObject* GetDataString(Cursor* cur, Py_ssize_t iCol)

char tempBuffer[1026]; // Pad with 2 bytes for driver bugs
DataBuffer buffer(nTargetType, tempBuffer, sizeof(tempBuffer)-2);
+ SQLLEN cbResizeIncrement = 2048;

- for (int iDbg = 0; iDbg < 10; iDbg++) // failsafe
+ /*
+ max_resizes==10 means cbResizeIncrement is doubled 9 times, allowing the
+ buffer to grow to almost 2MiB. Larger reads will fail with MemoryError.
+ */
+ const int max_resizes = 10;
+
+ for (int iDbg = 0; iDbg < max_resizes; iDbg++) // failsafe
{
SQLRETURN ret;
SQLLEN cbData = 0;
@@ -374,7 +381,8 @@ static PyObject* GetDataString(Cursor* cur, Py_ssize_t iCol)
{
// We don't know how much more, so just guess.
cbRead = cbBuffer - buffer.null_size;
- cbMore = 2048;
+ cbMore = cbResizeIncrement;
+ cbResizeIncrement *= 2;
}
else if (cbData >= cbBuffer)
{
@@ -391,6 +399,12 @@ static PyObject* GetDataString(Cursor* cur, Py_ssize_t iCol)
cbMore = 0;
}

+ if(iDbg+1 == max_resizes)
+ {
+ PyErr_SetString(PyExc_MemoryError, "multi-megabyte string");
+ return 0 /* NULL */;
+ }
+
buffer.AddUsed(cbRead);
if (!buffer.AllocateMore(cbMore))
return PyErr_NoMemory();
Powered by Google Project Hosting