Fix potential init crash by catching exceptions in scope_exit reset() (#40691)
* Fix potential init crash by catching exceptions in scope_exit reset() * Fix ordering
Blue committed
Jun 3, 2026 at 10:49 UTC
6cdc2b822d47853ea1151bd244bc2aa058b6f431
1 file changed
+57
-48
src/linux/inc/lxwil.h
+57
-48
@@ -90,6 +90,54 @@ private:
90
};
91
92
namespace details {
93
+ inline void ThrowErrorIf(bool condition, int error, FailureInfo info)
94
+ {
95
+ if (condition)
96
+ {
97
+ throw ::wil::ResultException(error, info);
98
+ }
99
+ }
100
+
101
+ inline void LogFailure(const char* message, const char* exceptionDescription) noexcept
102
+ {
103
+ auto callback = g_LogExceptionCallback;
104
+ if (callback != nullptr)
105
+ {
106
+ callback(message, exceptionDescription);
107
+ }
108
+ else
109
+ {
110
+ if (message != nullptr)
111
+ {
112
+ fputs(message, stderr);
113
+ fputs("\n", stderr);
114
+ }
115
+
116
+ if (exceptionDescription != nullptr)
117
+ {
118
+ fputs("Exception: ", stderr);
119
+ fputs(exceptionDescription, stderr);
120
+ fputs("\n", stderr);
121
+ }
122
+ }
123
+ }
124
+
125
+ inline void LogCaughtException(const char* message)
126
+ {
127
+ try
128
+ {
129
+ throw;
130
+ }
131
+ catch (const std::exception& ex)
132
+ {
133
+ LogFailure(message, ex.what());
134
+ }
135
+ catch (...)
136
+ {
137
+ LogFailure(message, nullptr);
138
+ }
139
+ }
140
+
141
template <typename TLambda>
142
class lambda_call
143
{
@@ -128,7 +176,15 @@ namespace details {
176
if (m_call)
177
{
178
m_call = false;
131
- m_lambda();
179
+
180
+ try
181
+ {
182
+ m_lambda();
183
+ }
184
+ catch (...)
185
+ {
186
+ LogCaughtException("Exception thrown from a scope_exit lambda");
187
+ }
188
}
189
}
190
@@ -143,53 +199,6 @@ namespace details {
199
bool m_call = true;
200
};
201
146
- inline void ThrowErrorIf(bool condition, int error, FailureInfo info)
147
- {
148
- if (condition)
149
- {
150
- throw ::wil::ResultException(error, info);
151
- }
152
- }
153
-
154
- inline void LogFailure(const char* message, const char* exceptionDescription) noexcept
155
- {
156
- auto callback = g_LogExceptionCallback;
157
- if (callback != nullptr)
158
- {
159
- callback(message, exceptionDescription);
160
- }
161
- else
162
- {
163
- if (message != nullptr)
164
- {
165
- fputs(message, stderr);
166
- fputs("\n", stderr);
167
- }
168
-
169
- if (exceptionDescription != nullptr)
170
- {
171
- fputs("Exception: ", stderr);
172
- fputs(exceptionDescription, stderr);
173
- fputs("\n", stderr);
174
- }
175
- }
176
- }
177
-
178
- inline void LogCaughtException(const char* message)
179
- {
180
- try
181
- {
182
- throw;
183
- }
184
- catch (const std::exception& ex)
185
- {
186
- LogFailure(message, ex.what());
187
- }
188
- catch (...)
189
- {
190
- LogFailure(message, nullptr);
191
- }
192
- }
202
} // namespace details
203
204
inline int ResultFromCaughtException()