reporter: Eliminate undefined behavior in SaveErrorReport

The optionals are unconditionally dereferenced when setting the custom
error text, and in a few cases this function is called using the default
value of the optionals.

This means we'd be dereferencing uninitialized storage.

Since they're used unconditionally, we can use value_or to set a default
when storage is uninitialized.
This commit is contained in:
Lioncash 2022-12-05 21:26:00 -05:00
parent 3b19f741bd
commit e7f9f58fa4
2 changed files with 6 additions and 6 deletions

View file

@ -339,8 +339,8 @@ void Reporter::SavePlayReport(PlayReportType type, u64 title_id, std::vector<std
} }
void Reporter::SaveErrorReport(u64 title_id, Result result, void Reporter::SaveErrorReport(u64 title_id, Result result,
std::optional<std::string> custom_text_main, const std::optional<std::string>& custom_text_main,
std::optional<std::string> custom_text_detail) const { const std::optional<std::string>& custom_text_detail) const {
if (!IsReportingEnabled()) { if (!IsReportingEnabled()) {
return; return;
} }
@ -354,8 +354,8 @@ void Reporter::SaveErrorReport(u64 title_id, Result result,
out["backtrace"] = GetBacktraceData(system); out["backtrace"] = GetBacktraceData(system);
out["error_custom_text"] = { out["error_custom_text"] = {
{"main", *custom_text_main}, {"main", custom_text_main.value_or("")},
{"detail", *custom_text_detail}, {"detail", custom_text_detail.value_or("")},
}; };
SaveToFile(std::move(out), GetPath("error_report", title_id, timestamp)); SaveToFile(std::move(out), GetPath("error_report", title_id, timestamp));

View file

@ -61,8 +61,8 @@ public:
// Used by error applet // Used by error applet
void SaveErrorReport(u64 title_id, Result result, void SaveErrorReport(u64 title_id, Result result,
std::optional<std::string> custom_text_main = {}, const std::optional<std::string>& custom_text_main = {},
std::optional<std::string> custom_text_detail = {}) const; const std::optional<std::string>& custom_text_detail = {}) const;
void SaveFSAccessLog(std::string_view log_message) const; void SaveFSAccessLog(std::string_view log_message) const;