Skip to content

Commit db00f42

Browse files
committed
Fix(kernel): insertDate use curtom format segmentation fault
1 parent 28605a6 commit db00f42

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

kernel/excel.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ PHP_METHOD(vtiful_xls, insertText)
602602

603603
SHEET_LINE_SET(obj, row);
604604

605-
if (format_handle) {
605+
if (format_handle != NULL) {
606606
type_writer(data, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
607607
} else {
608608
type_writer(data, row, column, &obj->write_ptr, format, obj->format_ptr.format);
@@ -640,13 +640,13 @@ PHP_METHOD(vtiful_xls, insertDate)
640640
}
641641

642642
// Default datetime format
643-
if (format == NULL) {
643+
if (format == NULL || (format != NULL && ZSTR_LEN(format) == 0)) {
644644
format = zend_string_init(ZEND_STRL("yyyy-mm-dd hh:mm:ss"), 0);
645645
}
646646

647647
lxw_datetime datetime = timestamp_to_datetime(data->value.lval);
648648

649-
if (format_handle) {
649+
if (format_handle != NULL) {
650650
datetime_writer(&datetime, row, column, format, &obj->write_ptr, zval_get_format(format_handle));
651651
} else {
652652
datetime_writer(&datetime, row, column, format, &obj->write_ptr, obj->format_ptr.format);
@@ -771,7 +771,7 @@ PHP_METHOD(vtiful_xls, insertFormula)
771771
formula_writer(formula, row, column, &obj->write_ptr, obj->format_ptr.format);
772772
}
773773

774-
if (argc == 4) {
774+
if (argc == 4 && format_handle != NULL) {
775775
formula_writer(formula, row, column, &obj->write_ptr, zval_get_format(format_handle));
776776
}
777777
}
@@ -860,7 +860,7 @@ PHP_METHOD(vtiful_xls, mergeCells)
860860
merge_cells(range, data, &obj->write_ptr, obj->format_ptr.format);
861861
}
862862

863-
if (argc == 3) {
863+
if (argc == 3 && format_handle != NULL) {
864864
merge_cells(range, data, &obj->write_ptr, zval_get_format(format_handle));
865865
}
866866
}
@@ -889,7 +889,7 @@ PHP_METHOD(vtiful_xls, setColumn)
889889

890890
WORKBOOK_NOT_INITIALIZED(obj);
891891

892-
if (argc == 3) {
892+
if (argc == 3 && format_handle != NULL) {
893893
set_column(range, width, &obj->write_ptr, zval_get_format(format_handle));
894894
}
895895

@@ -922,7 +922,7 @@ PHP_METHOD(vtiful_xls, setRow)
922922

923923
WORKBOOK_NOT_INITIALIZED(obj);
924924

925-
if (argc == 3) {
925+
if (argc == 3 && format_handle != NULL) {
926926
set_row(range, height, &obj->write_ptr, zval_get_format(format_handle));
927927
}
928928

kernel/resource.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ xls_resource_write_t * zval_get_resource(zval *handle)
2828
/* {{{ */
2929
lxw_format * zval_get_format(zval *handle)
3030
{
31-
lxw_format *res;
31+
lxw_format *res = NULL;
32+
33+
if (handle == NULL) {
34+
return NULL;
35+
}
36+
37+
if (zval_get_type(handle) != IS_RESOURCE) {
38+
return NULL;
39+
}
3240

3341
if((res = (lxw_format *)zend_fetch_resource(Z_RES_P(handle), VTIFUL_RESOURCE_NAME, le_xls_writer)) == NULL) {
3442
zend_throw_exception(vtiful_exception_ce, "format resources resolution fail", 210);

kernel/write.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,12 @@ void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_
212212
*/
213213
void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle)
214214
{
215-
lxw_format *value_format = NULL;
215+
lxw_format *value_format = workbook_add_format(res->workbook);
216216

217217
if (format_handle != NULL) {
218218
format_copy(value_format, format_handle);
219219
}
220220

221-
value_format = workbook_add_format(res->workbook);
222221
format_set_num_format(value_format, ZSTR_VAL(format));
223222
worksheet_write_datetime(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, datetime, value_format);
224223
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$config = [
8+
'path' => './tests'
9+
];
10+
11+
$fileObject = new \Vtiful\Kernel\Excel($config);
12+
$fileObject = $fileObject->fileName('tutorial.xlsx');
13+
14+
$fileHandle = $fileObject->getHandle();
15+
16+
$format = new \Vtiful\Kernel\Format($fileHandle);
17+
$colorStyle = $format->fontColor(\Vtiful\Kernel\Format::COLOR_ORANGE)->toResource();
18+
19+
$filePath = $fileObject->header(['date'])
20+
->insertDate(1, 0, time(), 'mmm d yyyy hh:mm AM/PM', $colorStyle)
21+
->output();
22+
23+
var_dump($filePath);
24+
?>
25+
--CLEAN--
26+
<?php
27+
@unlink(__DIR__ . '/tutorial.xlsx');
28+
?>
29+
--EXPECT--
30+
string(21) "./tests/tutorial.xlsx"

0 commit comments

Comments
 (0)