Skip to content

Commit 8e54167

Browse files
committed
First attempt a limiting the error msg size.
1 parent 8b48ff8 commit 8e54167

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

include/pybind11/pybind11.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,9 +1122,13 @@ class cpp_function : public function {
11221122
msg += '\n';
11231123
}
11241124
msg += "\nInvoked with: ";
1125+
constexpr auto max_args_repr_size = 5000u;
1126+
const auto orig_msg_size = msg.size();
11251127
auto args_ = reinterpret_borrow<tuple>(args_in);
11261128
bool some_args = false;
1127-
for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
1129+
for (size_t ti = overloads->is_constructor ? 1 : 0;
1130+
ti < args_.size() && msg.size() - orig_msg_size <= max_args_repr_size;
1131+
++ti) {
11281132
if (!some_args) {
11291133
some_args = true;
11301134
} else {
@@ -1136,7 +1140,7 @@ class cpp_function : public function {
11361140
msg += "<repr raised Error>";
11371141
}
11381142
}
1139-
if (kwargs_in) {
1143+
if (kwargs_in && msg.size() - orig_msg_size <= max_args_repr_size) {
11401144
auto kwargs = reinterpret_borrow<dict>(kwargs_in);
11411145
if (!kwargs.empty()) {
11421146
if (some_args) {
@@ -1145,6 +1149,10 @@ class cpp_function : public function {
11451149
msg += "kwargs: ";
11461150
bool first = true;
11471151
for (const auto &kwarg : kwargs) {
1152+
if (msg.size() - orig_msg_size > max_args_repr_size) {
1153+
break;
1154+
}
1155+
11481156
if (first) {
11491157
first = false;
11501158
} else {
@@ -1160,6 +1168,10 @@ class cpp_function : public function {
11601168
}
11611169
}
11621170

1171+
if (msg.size() - orig_msg_size > max_args_repr_size) {
1172+
msg += "...";
1173+
}
1174+
11631175
append_note_if_missing_header_is_suspected(msg);
11641176
// Attach additional error info to the exception if supported
11651177
if (PyErr_Occurred()) {

0 commit comments

Comments
 (0)