gecko-dev/toolkit/xre/nsGDKErrorHandler.cpp
Nika Layzell c15823d075 Bug 1772006 - Part 5: Simplify and move the string searching APIs from ns[T]StringObsolete, r=xpcom-reviewers,necko-reviewers,eeejay,dragana,barret
The biggest set of APIs from ns[T]StringObsolete which are still heavily used
are the string searching APIs. It appears the intention was for these to be
replaced by the `FindInReadable` APIs, however that doesn't appear to have
happened.

In addition, the APIs have some quirks around their handling of mixed character
widths. These APIs generally supported both narrow strings and the native
string type, probably because char16_t string literals weren't available until
c++11. Finally they also used easy-to-confuse unlabeled boolean and integer
optional arguments to control behaviour.

These patches do the following major changes to the searching APIs:

1. The ASCII case-insensitive search method was split out as
   LowerCaseFindASCII, rather than using a boolean. This should be less
   error-prone and more explicit, and allows the method to continue to use
   narrow string literals for all string types (as only ASCII is supported).
2. The other [R]Find methods were restricted to only support arguments with
   matching character types. I considered adding a FindASCII method which would
   use narrow string literals for both wide and narrow strings but it would've
   been the same amount of work as changing all of the literals to unicode
   literals.
   This ends up being the bulk of the changes in the patch.
3. All find methods were re-implemented using std::basic_string_view's find
   algorithm or stl algorithms to reduce code complexity, and avoid the need to
   carry around the logic from nsStringObsolete.cpp.
4. The implementations were moved to nsTStringRepr.cpp.
5. An overload of Find was added to try to catch callers which previously
   called `Find(..., false)` or `Find(..., true)` to set case-sensitivity, due
   to booleans normally implicitly coercing to `index_type`. This should
   probably be removed at some point, but may be useful during the transition.

Differential Revision: https://phabricator.services.mozilla.com/D148300
2022-07-30 00:12:48 +00:00

118 lines
3.4 KiB
C++

/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsGDKErrorHandler.h"
#include <gtk/gtk.h>
#ifdef MOZ_X11
# include <gdk/gdkx.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "nsDebug.h"
#include "nsString.h"
#ifdef MOZ_X11
# include "nsX11ErrorHandler.h"
#endif
#include "prenv.h"
/* See https://bugzilla.gnome.org/show_bug.cgi?id=629608#c8
*
* GDK implements X11 error traps to ignore X11 errors.
* Unfortunatelly We don't know which X11 events can be ignored
* so we have to utilize the Gdk error handler to avoid
* false alarms in Gtk3.
*/
static void GdkErrorHandler(const gchar* log_domain, GLogLevelFlags log_level,
const gchar* message, gpointer user_data) {
#ifdef MOZ_X11
if (strstr(message, "X Window System error")) {
XErrorEvent event;
nsDependentCString buffer(message);
char* endptr;
/* Parse Gdk X Window error message which has this format:
* (Details: serial XXXX error_code XXXX request_code XXXX (XXXX) minor_code
* XXXX)
*/
constexpr auto serialString = "(Details: serial "_ns;
int32_t start = buffer.Find(serialString);
if (start == kNotFound) {
MOZ_CRASH_UNSAFE(message);
}
start += serialString.Length();
errno = 0;
event.serial = strtol(buffer.BeginReading() + start, &endptr, 10);
if (errno) {
MOZ_CRASH_UNSAFE(message);
}
constexpr auto errorCodeString = " error_code "_ns;
if (!StringBeginsWith(Substring(endptr, buffer.EndReading()),
errorCodeString)) {
MOZ_CRASH_UNSAFE(message);
}
errno = 0;
event.error_code = strtol(endptr + errorCodeString.Length(), &endptr, 10);
if (errno) {
MOZ_CRASH_UNSAFE(message);
}
constexpr auto requestCodeString = " request_code "_ns;
if (!StringBeginsWith(Substring(endptr, buffer.EndReading()),
requestCodeString)) {
MOZ_CRASH_UNSAFE(message);
}
errno = 0;
event.request_code =
strtol(endptr + requestCodeString.Length(), &endptr, 10);
if (errno) {
MOZ_CRASH_UNSAFE(message);
}
constexpr auto minorCodeString = " minor_code "_ns;
start = buffer.Find(minorCodeString, endptr - buffer.BeginReading());
if (!start) {
MOZ_CRASH_UNSAFE(message);
}
errno = 0;
event.minor_code = strtol(
buffer.BeginReading() + start + minorCodeString.Length(), nullptr, 10);
if (errno) {
MOZ_CRASH_UNSAFE(message);
}
event.display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
// Gdk does not provide resource ID
event.resourceid = 0;
X11Error(event.display, &event);
} else
#endif
{
g_log_default_handler(log_domain, log_level, message, user_data);
MOZ_CRASH_UNSAFE(message);
}
}
void InstallGdkErrorHandler() {
g_log_set_handler("Gdk",
(GLogLevelFlags)(G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL |
G_LOG_FLAG_RECURSION),
GdkErrorHandler, nullptr);
#ifdef MOZ_X11
if (PR_GetEnv("MOZ_X_SYNC")) {
XSynchronize(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), X11True);
}
#endif
}