Bug Summary

File:plugins/clipboard/xutils.c
Warning:line 87, column 25
Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name xutils.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/rootdir/plugins/clipboard -resource-dir /usr/lib/llvm-16/lib/clang/16 -D HAVE_CONFIG_H -I . -I ../.. -I ../../cafe-settings-daemon -D CAFE_SETTINGS_LOCALEDIR="/usr/share/locale" -I /usr/include/ctk-3.0 -I /usr/include/pango-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/x86_64-linux-gnu -I /usr/include/webp -I /usr/include/gio-unix-2.0 -I /usr/include/atk-1.0 -I /usr/include/at-spi2-atk/2.0 -I /usr/include/at-spi-2.0 -I /usr/include/dbus-1.0 -I /usr/lib/x86_64-linux-gnu/dbus-1.0/include -D PIC -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/rootdir/plugins/clipboard -ferror-limit 19 -fgnuc-version=4.2.1 -analyzer-checker deadcode.DeadStores -analyzer-checker alpha.deadcode.UnreachableCode -analyzer-checker alpha.core.CastSize -analyzer-checker alpha.core.CastToStruct -analyzer-checker alpha.core.IdenticalExpr -analyzer-checker alpha.core.SizeofPtr -analyzer-checker alpha.security.ArrayBoundV2 -analyzer-checker alpha.security.MallocOverflow -analyzer-checker alpha.security.ReturnPtrRange -analyzer-checker alpha.unix.SimpleStream -analyzer-checker alpha.unix.cstring.BufferOverlap -analyzer-checker alpha.unix.cstring.NotNullTerminated -analyzer-checker alpha.unix.cstring.OutOfBounds -analyzer-checker alpha.core.FixedAddr -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /rootdir/html-report/2024-01-21-030324-65499-1 -x c xutils.c
1/*
2 * Copyright © 2004 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Red Hat not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. Red Hat makes no representations about the
11 * suitability of this software for any purpose. It is provided "as is"
12 * without express or implied warranty.
13 *
14 * RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL RED HAT
16 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 * Author: Matthias Clasen, Red Hat, Inc.
22 */
23
24#include <stdlib.h>
25
26#include "xutils.h"
27
28Atom XA_ATOM_PAIR;
29Atom XA_CLIPBOARD_MANAGER;
30Atom XA_CLIPBOARD;
31Atom XA_DELETE;
32Atom XA_INCR;
33Atom XA_INSERT_PROPERTY;
34Atom XA_INSERT_SELECTION;
35Atom XA_MANAGER;
36Atom XA_MULTIPLE;
37Atom XA_NULL;
38Atom XA_SAVE_TARGETS;
39Atom XA_TARGETS;
40Atom XA_TIMESTAMP;
41
42unsigned long SELECTION_MAX_SIZE = 0;
43
44
45void
46init_atoms (Display *display)
47{
48 unsigned long max_request_size;
49
50 if (SELECTION_MAX_SIZE > 0)
51 return;
52
53 XA_ATOM_PAIR = XInternAtom (display, "ATOM_PAIR", False0);
54 XA_CLIPBOARD_MANAGER = XInternAtom (display, "CLIPBOARD_MANAGER", False0);
55 XA_CLIPBOARD = XInternAtom (display, "CLIPBOARD", False0);
56 XA_DELETE = XInternAtom (display, "DELETE", False0);
57 XA_INCR = XInternAtom (display, "INCR", False0);
58 XA_INSERT_PROPERTY = XInternAtom (display, "INSERT_PROPERTY", False0);
59 XA_INSERT_SELECTION = XInternAtom (display, "INSERT_SELECTION", False0);
60 XA_MANAGER = XInternAtom (display, "MANAGER", False0);
61 XA_MULTIPLE = XInternAtom (display, "MULTIPLE", False0);
62 XA_NULL = XInternAtom (display, "NULL", False0);
63 XA_SAVE_TARGETS = XInternAtom (display, "SAVE_TARGETS", False0);
64 XA_TARGETS = XInternAtom (display, "TARGETS", False0);
65 XA_TIMESTAMP = XInternAtom (display, "TIMESTAMP", False0);
66
67 max_request_size = XExtendedMaxRequestSize (display);
68 if (max_request_size == 0)
69 max_request_size = XMaxRequestSize (display);
70
71 SELECTION_MAX_SIZE = max_request_size - 100;
72 if (SELECTION_MAX_SIZE > 262144)
73 SELECTION_MAX_SIZE = 262144;
74}
75
76typedef struct
77{
78 Window window;
79 Atom timestamp_prop_atom;
80} TimeStampInfo;
81
82static Boolint
83timestamp_predicate (Display *display,
84 XEvent *xevent,
85 XPointer arg)
86{
87 TimeStampInfo *info = (TimeStampInfo *)arg;
Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption
88
89 if (xevent->type == PropertyNotify28 &&
90 xevent->xproperty.window == info->window &&
91 xevent->xproperty.atom == info->timestamp_prop_atom)
92 return True1;
93
94 return False0;
95}
96
97Time
98get_server_time (Display *display,
99 Window window)
100{
101 unsigned char c = 'a';
102 XEvent xevent;
103 TimeStampInfo info;
104
105 info.timestamp_prop_atom = XInternAtom (display, "_TIMESTAMP_PROP", False0);
106 info.window = window;
107
108 XChangeProperty (display, window,
109 info.timestamp_prop_atom, info.timestamp_prop_atom,
110 8, PropModeReplace0, &c, 1);
111
112 XIfEvent (display, &xevent,
113 timestamp_predicate, (XPointer)&info);
114
115 return xevent.xproperty.time;
116}
117