Skip to content

Commit 853b020

Browse files
committed
Fix c-ext not support negative deltatimes
Inserts to TIME columns with negative deltatimes fails ramdomly, this due to the addition of some invalid characters that could produce invalid insert statements or wrong inserted values. This patch fixes this issue by changing the way the insert stament is formated.
1 parent bd2b02d commit 853b020

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/mysql_capi_conversion.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,8 @@ pytomy_timedelta(PyObject *obj)
171171
{
172172
int days= 0, secs= 0 , micro_secs= 0, total_secs= 0;
173173
int hours= 0, mins= 0, remainder= 0;
174-
char fmt[32]= "";
175-
char result[17]= "";
176-
char minus[1]= "";
174+
char fmt[32]= {0};
175+
char result[17]= {0};
177176

178177
PyDateTime_IMPORT;
179178

@@ -194,9 +193,11 @@ pytomy_timedelta(PyObject *obj)
194193
#pragma warning(push)
195194
// result of strncpy does not accept direct user input
196195
#pragma warning(disable: 4996)
196+
197+
197198
if (micro_secs)
198199
{
199-
strncpy(fmt, "%s%02d:%02d:%02d.%06d", 21);
200+
strncpy(fmt, "%02d:%02d:%02d.%06d", 19);
200201
if (days < 0)
201202
{
202203
micro_secs= 1000000 - micro_secs;
@@ -205,14 +206,17 @@ pytomy_timedelta(PyObject *obj)
205206
}
206207
else
207208
{
208-
strncpy(fmt, "%s%02d:%02d:%02d", 16);
209+
strncpy(fmt, "%02d:%02d:%02d", 14);
209210
}
210-
#pragma warning(pop)
211211

212212
if (days < 0)
213213
{
214-
minus[0]= '-';
214+
for(int index = 31; index > 0; index--){
215+
fmt[index] = fmt[index - 1];
216+
}
217+
fmt[0] = '-';
215218
}
219+
#pragma warning(pop)
216220

217221
hours= total_secs / 3600;
218222
remainder= total_secs % 3600;
@@ -221,11 +225,11 @@ pytomy_timedelta(PyObject *obj)
221225

222226
if (micro_secs)
223227
{
224-
PyOS_snprintf(result, 17, fmt, minus, hours, mins, secs, micro_secs);
228+
PyOS_snprintf(result, 17, fmt, hours, mins, secs, micro_secs);
225229
}
226230
else
227231
{
228-
PyOS_snprintf(result, 17, fmt, minus, hours, mins, secs);
232+
PyOS_snprintf(result, 17, fmt, hours, mins, secs);
229233
}
230234

231235
return PyBytesFromString(result);
@@ -249,7 +253,7 @@ pytomy_timedelta(PyObject *obj)
249253
PyObject*
250254
pytomy_time(PyObject *obj)
251255
{
252-
char result[17]= "";
256+
char result[17]= {0};
253257

254258
PyDateTime_IMPORT;
255259

@@ -297,7 +301,7 @@ pytomy_time(PyObject *obj)
297301
PyObject*
298302
pytomy_datetime(PyObject *obj)
299303
{
300-
char result[27]= "";
304+
char result[27]= {0};
301305
PyDateTime_IMPORT;
302306

303307
if (!obj || !PyDateTime_Check(obj))
@@ -328,7 +332,6 @@ pytomy_datetime(PyObject *obj)
328332
PyDateTime_DATE_GET_MINUTE(obj),
329333
PyDateTime_DATE_GET_SECOND(obj));
330334
}
331-
332335
return PyBytesFromString(result);
333336
}
334337

0 commit comments

Comments
 (0)