summaryrefslogtreecommitdiffstats
path: root/toolkit/xre/nsGDKErrorHandler.cpp
blob: 421abdf12f90685424d6baab30914cb9c8ee7a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*- 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>
#include <gdk/gdkx.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "nsDebug.h"
#include "nsString.h"
#include "nsX11ErrorHandler.h"

#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) {
  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, /* aIgnoreCase = */ false,
                        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 {
    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);
  if (PR_GetEnv("MOZ_X_SYNC")) {
    XSynchronize(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), X11True);
  }
}