Skip to content

Commit c9f8542

Browse files
committed
Dynamic stream reallocation if required
Fixed #25
1 parent 78fc7ac commit c9f8542

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

c/iscpython.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <Python.h>
1717
#include <stdbool.h>
1818

19-
#ifdef __linux__
19+
#ifdef __linux__
2020
#include <dlfcn.h>
2121
#endif
2222

@@ -56,7 +56,7 @@ void *libHandle = NULL;
5656
int Initialize(char *file) {
5757
if (isInitialized == false) {
5858
if ((file) && (!libHandle)) {
59-
#ifdef __linux__
59+
#ifdef __linux__
6060
//linux code goes here
6161
//http://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html
6262
libHandle = dlopen(file, RTLD_LAZY |RTLD_GLOBAL);
@@ -75,14 +75,14 @@ int Finalize() {
7575
Py_DECREF(mainModule);
7676
Py_Finalize();
7777
}
78-
78+
7979
if (libHandle) {
80-
#ifdef __linux__
80+
#ifdef __linux__
8181
dlclose(libHandle);
8282
#endif
8383
libHandle = NULL;
8484
}
85-
85+
8686
return ZF_SUCCESS;
8787
}
8888

@@ -195,14 +195,18 @@ int SimpleString(CACHE_EXSTRP command, char *resultVar, int serialization, CACHE
195195
// Init incoming stream (inStream) to length bytes + 1
196196
int StreamInit(int length)
197197
{
198-
if (!inStream) {
198+
// Free previous stream, if any.
199+
if (inStream) {
199200
free(inStream);
200201
inStream = NULL;
201202
}
203+
204+
// Allocate stream
202205
inStream = calloc(length + 1, sizeof(char));
203206
curpos = 0;
204207
maxpos = length;
205208

209+
// Return failure if allocation failed
206210
if (!inStream) {
207211
return ZF_FAILURE;
208212
}
@@ -213,10 +217,26 @@ int StreamInit(int length)
213217
// Write piece of inStream
214218
int StreamWrite(CACHE_EXSTRP command)
215219
{
216-
if ((!inStream) || ((int)command->len + curpos > maxpos)) {
220+
// Stream should be initiate first
221+
if (!inStream) {
217222
return ZF_FAILURE;
218223
}
219224

225+
// We want to write more bytes, then available.
226+
// Need to extend the pointer first
227+
if ((int)command->len + curpos > maxpos) {
228+
maxpos = (int)command->len + curpos + 1;
229+
char *inStreamTemp = realloc(inStream, maxpos);
230+
231+
if (inStreamTemp) {
232+
inStream = inStreamTemp;
233+
memset(inStream + curpos, '0', maxpos - curpos);
234+
} else {
235+
// Reallocation failed
236+
return ZF_FAILURE;
237+
}
238+
}
239+
220240
memcpy(inStream + curpos, command->str.ch, command->len);
221241
curpos += command->len;
222242
return ZF_SUCCESS;

isc/py/Callout.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ ClassMethod StreamWrite(code As %String = "") As %Status
165165
} catch ex {
166166
#dim ex As %Exception.General
167167
if (ex.Name = "<FUNCTION>") {
168-
set sc = $$$ERROR($$$GeneralError, "Call StreamInit before calling StreamWrite. Or attempted to write into stream more than allocated data.")
168+
set sc = $$$ERROR($$$GeneralError, "Call StreamInit before calling StreamWrite. Or reallocation failed as initial allocation in StreamInit could not hold all the data.")
169169
} else {
170170
set sc = ex.AsStatus()
171171
}

0 commit comments

Comments
 (0)