Skip to content

Commit 95f5775

Browse files
committed
Merge branch 'ttys3-master'
return null in case of std exceptions
2 parents 95a2e73 + 468351e commit 95f5775

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

bencode.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ PHP_METHOD(bitem, parse)
1919
RETURN_NULL();
2020
}
2121
std::string ben_str(ben, ben_len);
22-
RETURN_ZVAL(bitem::parse(ben_str), 1, 1);
22+
CALL_AND_HANDLE(RETURN_ZVAL(bitem::parse(ben_str), 1, 1));
2323
}
2424
PHP_METHOD(bitem, load)
2525
{
@@ -29,7 +29,7 @@ PHP_METHOD(bitem, load)
2929
RETURN_NULL();
3030
}
3131
std::string file_path_str(file_path, file_path_len);
32-
RETURN_ZVAL(bitem::load(file_path_str), 1, 1);
32+
CALL_AND_HANDLE(RETURN_ZVAL(bitem::load(file_path_str), 1, 1));
3333
}
3434
PHP_METHOD(bitem, save)
3535
{
@@ -210,7 +210,7 @@ PHP_METHOD(bdict, parse)
210210
if (!ben_len) RETURN_NULL();
211211
std::string tmp(ben, ben_len);
212212
size_t pt = 0;
213-
RETURN_ZVAL(bdict::parse(tmp, pt), 1, 1);
213+
CALL_AND_HANDLE(RETURN_ZVAL(bdict::parse(tmp, pt), 1, 1));
214214
}
215215
PHP_METHOD(bdict, encode)
216216
{
@@ -228,7 +228,7 @@ PHP_METHOD(bdict, search)
228228
}
229229
std::string tmp(needle, needle_len);
230230
bdict_object *intern = Z_BDICT_OBJ_P(getThis());
231-
RETURN_ZVAL(intern->bnode_data->search(tmp, mode, ""), 1, 1);
231+
CALL_AND_HANDLE(RETURN_ZVAL(intern->bnode_data->search(tmp, mode, ""), 1, 1));
232232
}
233233
PHP_METHOD(bdict, to_array)
234234
{
@@ -425,7 +425,7 @@ PHP_METHOD(blist, parse)
425425
if (!ben_len) RETURN_NULL();
426426
std::string tmp(ben, ben_len);
427427
size_t pt = 0;
428-
RETURN_ZVAL(blist::parse(tmp, pt), 1, 1);
428+
CALL_AND_HANDLE(RETURN_ZVAL(blist::parse(tmp, pt), 1, 1));
429429
}
430430
PHP_METHOD(blist, encode)
431431
{
@@ -443,7 +443,7 @@ PHP_METHOD(blist, search)
443443
}
444444
std::string tmp(needle, needle_len);
445445
blist_object *intern = Z_BLIST_OBJ_P(getThis());
446-
RETURN_ZVAL(intern->bnode_data->search(tmp, mode, ""), 1, 1);
446+
CALL_AND_HANDLE(RETURN_ZVAL(intern->bnode_data->search(tmp, mode, ""), 1, 1));
447447
}
448448
PHP_METHOD(blist, to_array)
449449
{
@@ -544,7 +544,7 @@ PHP_METHOD(bstr, parse)
544544
if (!ben_len) RETURN_NULL();
545545
std::string tmp(ben);
546546
size_t pt = 0;
547-
RETURN_ZVAL(bstr::parse(tmp, pt), 1, 1);
547+
CALL_AND_HANDLE(RETURN_ZVAL(bstr::parse(tmp, pt), 1, 1));
548548
}
549549
PHP_METHOD(bstr, encode)
550550
{
@@ -641,7 +641,7 @@ PHP_METHOD(bint, parse)
641641
if (!ben_len) RETURN_NULL();
642642
std::string tmp(ben, ben_len);
643643
size_t pt = 0;
644-
RETURN_ZVAL(bint::parse(tmp, pt), 1, 1);
644+
CALL_AND_HANDLE(RETURN_ZVAL(bint::parse(tmp, pt), 1, 1));
645645
}
646646
PHP_METHOD(bint, encode)
647647
{

php_bencode.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,22 @@ PHP_MSHUTDOWN_FUNCTION(bencode);
1818
extern zend_module_entry bencode_module_entry;
1919
#define phpext_bencode_ptr &bencode_module_entry
2020

21+
#define CALL_AND_HANDLE(expr) \
22+
try { \
23+
return (expr); \
24+
} catch (const std::invalid_argument& ia) { \
25+
zend_throw_exception(NULL, std::string("Invalid argument: " + std::string(ia.what())).c_str(), 2); \
26+
RETURN_NULL(); \
27+
} catch (const std::out_of_range& oor) { \
28+
zend_throw_exception(NULL, std::string("Out of Range error: " + std::string(oor.what())).c_str(), 3); \
29+
RETURN_NULL(); \
30+
} catch (const std::exception& e) { \
31+
zend_throw_exception(NULL, std::string("Undefined error: " + std::string(e.what())).c_str(), 4); \
32+
RETURN_NULL(); \
33+
} catch (...) { \
34+
zend_throw_exception(NULL, "Unkown error", 5); \
35+
RETURN_NULL(); \
36+
} \
37+
38+
2139
#endif

0 commit comments

Comments
 (0)