Bug Summary

File:test/xkb.c
Location:line 163, column 5
Description:Potential leak of memory pointed to by 'rmlvo_backup.variant'

Annotated Source Code

1/**
2 * Copyright © 2009 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#ifdef HAVE_DIX_CONFIG_H1
25#include <dix-config.h>
26#endif
27
28#include <xkb-config.h>
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <ctype.h>
33#include <unistd.h>
34#include <math.h>
35#include <X11/X.h>
36#include <X11/Xproto.h>
37#include <X11/keysym.h>
38#include <X11/Xatom.h>
39#include "misc.h"
40#include "inputstr.h"
41#include "opaque.h"
42#include "property.h"
43#define XKBSRV_NEED_FILE_FUNCS
44#include <xkbsrv.h>
45#include "../xkb/xkbgeom.h"
46#include <X11/extensions/XKMformat.h>
47#include "xkbfile.h"
48#include "../xkb/xkb.h"
49#include <assert.h>
50
51/**
52 * Initialize an empty XkbRMLVOSet.
53 * Call XkbGetRulesDflts to obtain the default ruleset.
54 * Compare obtained ruleset with the built-in defaults.
55 *
56 * Result: RMLVO defaults are the same as obtained.
57 */
58static void
59xkb_get_rules_test(void)
60{
61 XkbRMLVOSet rmlvo = { NULL((void*)0) };
62 XkbGetRulesDflts(&rmlvo);
63
64 assert(rmlvo.rules)(__builtin_expect(!(rmlvo.rules), 0) ? __assert_rtn(__func__,
"xkb.c", 64, "rmlvo.rules") : (void)0)
;
65 assert(rmlvo.model)(__builtin_expect(!(rmlvo.model), 0) ? __assert_rtn(__func__,
"xkb.c", 65, "rmlvo.model") : (void)0)
;
66 assert(rmlvo.layout)(__builtin_expect(!(rmlvo.layout), 0) ? __assert_rtn(__func__
, "xkb.c", 66, "rmlvo.layout") : (void)0)
;
67 assert(rmlvo.variant)(__builtin_expect(!(rmlvo.variant), 0) ? __assert_rtn(__func__
, "xkb.c", 67, "rmlvo.variant") : (void)0)
;
68 assert(rmlvo.options)(__builtin_expect(!(rmlvo.options), 0) ? __assert_rtn(__func__
, "xkb.c", 68, "rmlvo.options") : (void)0)
;
69 assert(strcmp(rmlvo.rules, XKB_DFLT_RULES) == 0)(__builtin_expect(!(strcmp(rmlvo.rules, "base") == 0), 0) ? __assert_rtn
(__func__, "xkb.c", 69, "strcmp(rmlvo.rules, XKB_DFLT_RULES) == 0"
) : (void)0)
;
70 assert(strcmp(rmlvo.model, XKB_DFLT_MODEL) == 0)(__builtin_expect(!(strcmp(rmlvo.model, "pc105") == 0), 0) ? __assert_rtn
(__func__, "xkb.c", 70, "strcmp(rmlvo.model, XKB_DFLT_MODEL) == 0"
) : (void)0)
;
71 assert(strcmp(rmlvo.layout, XKB_DFLT_LAYOUT) == 0)(__builtin_expect(!(strcmp(rmlvo.layout, "us") == 0), 0) ? __assert_rtn
(__func__, "xkb.c", 71, "strcmp(rmlvo.layout, XKB_DFLT_LAYOUT) == 0"
) : (void)0)
;
72 assert(strcmp(rmlvo.variant, XKB_DFLT_VARIANT) == 0)(__builtin_expect(!(strcmp(rmlvo.variant, "") == 0), 0) ? __assert_rtn
(__func__, "xkb.c", 72, "strcmp(rmlvo.variant, XKB_DFLT_VARIANT) == 0"
) : (void)0)
;
73 assert(strcmp(rmlvo.options, XKB_DFLT_OPTIONS) == 0)(__builtin_expect(!(strcmp(rmlvo.options, "") == 0), 0) ? __assert_rtn
(__func__, "xkb.c", 73, "strcmp(rmlvo.options, XKB_DFLT_OPTIONS) == 0"
) : (void)0)
;
74}
75
76/**
77 * Initialize an random XkbRMLVOSet.
78 * Call XkbGetRulesDflts to obtain the default ruleset.
79 * Compare obtained ruleset with the built-in defaults.
80 * Result: RMLVO defaults are the same as obtained.
81 */
82static void
83xkb_set_rules_test(void)
84{
85 XkbRMLVOSet rmlvo;
86 XkbRMLVOSet rmlvo_new = { NULL((void*)0) };
87
88 XkbInitRules(&rmlvo, "test-rules", "test-model", "test-layout",
89 "test-variant", "test-options");
90 assert(rmlvo.rules)(__builtin_expect(!(rmlvo.rules), 0) ? __assert_rtn(__func__,
"xkb.c", 90, "rmlvo.rules") : (void)0)
;
91 assert(rmlvo.model)(__builtin_expect(!(rmlvo.model), 0) ? __assert_rtn(__func__,
"xkb.c", 91, "rmlvo.model") : (void)0)
;
92 assert(rmlvo.layout)(__builtin_expect(!(rmlvo.layout), 0) ? __assert_rtn(__func__
, "xkb.c", 92, "rmlvo.layout") : (void)0)
;
93 assert(rmlvo.variant)(__builtin_expect(!(rmlvo.variant), 0) ? __assert_rtn(__func__
, "xkb.c", 93, "rmlvo.variant") : (void)0)
;
94 assert(rmlvo.options)(__builtin_expect(!(rmlvo.options), 0) ? __assert_rtn(__func__
, "xkb.c", 94, "rmlvo.options") : (void)0)
;
95
96 XkbSetRulesDflts(&rmlvo);
97 XkbGetRulesDflts(&rmlvo_new);
98
99 /* XkbGetRulesDflts strdups the values */
100 assert(rmlvo.rules != rmlvo_new.rules)(__builtin_expect(!(rmlvo.rules != rmlvo_new.rules), 0) ? __assert_rtn
(__func__, "xkb.c", 100, "rmlvo.rules != rmlvo_new.rules") : (
void)0)
;
101 assert(rmlvo.model != rmlvo_new.model)(__builtin_expect(!(rmlvo.model != rmlvo_new.model), 0) ? __assert_rtn
(__func__, "xkb.c", 101, "rmlvo.model != rmlvo_new.model") : (
void)0)
;
102 assert(rmlvo.layout != rmlvo_new.layout)(__builtin_expect(!(rmlvo.layout != rmlvo_new.layout), 0) ? __assert_rtn
(__func__, "xkb.c", 102, "rmlvo.layout != rmlvo_new.layout") :
(void)0)
;
103 assert(rmlvo.variant != rmlvo_new.variant)(__builtin_expect(!(rmlvo.variant != rmlvo_new.variant), 0) ?
__assert_rtn(__func__, "xkb.c", 103, "rmlvo.variant != rmlvo_new.variant"
) : (void)0)
;
104 assert(rmlvo.options != rmlvo_new.options)(__builtin_expect(!(rmlvo.options != rmlvo_new.options), 0) ?
__assert_rtn(__func__, "xkb.c", 104, "rmlvo.options != rmlvo_new.options"
) : (void)0)
;
105
106 assert(strcmp(rmlvo.rules, rmlvo_new.rules) == 0)(__builtin_expect(!(strcmp(rmlvo.rules, rmlvo_new.rules) == 0
), 0) ? __assert_rtn(__func__, "xkb.c", 106, "strcmp(rmlvo.rules, rmlvo_new.rules) == 0"
) : (void)0)
;
107 assert(strcmp(rmlvo.model, rmlvo_new.model) == 0)(__builtin_expect(!(strcmp(rmlvo.model, rmlvo_new.model) == 0
), 0) ? __assert_rtn(__func__, "xkb.c", 107, "strcmp(rmlvo.model, rmlvo_new.model) == 0"
) : (void)0)
;
108 assert(strcmp(rmlvo.layout, rmlvo_new.layout) == 0)(__builtin_expect(!(strcmp(rmlvo.layout, rmlvo_new.layout) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 108, "strcmp(rmlvo.layout, rmlvo_new.layout) == 0"
) : (void)0)
;
109 assert(strcmp(rmlvo.variant, rmlvo_new.variant) == 0)(__builtin_expect(!(strcmp(rmlvo.variant, rmlvo_new.variant) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 109, "strcmp(rmlvo.variant, rmlvo_new.variant) == 0"
) : (void)0)
;
110 assert(strcmp(rmlvo.options, rmlvo_new.options) == 0)(__builtin_expect(!(strcmp(rmlvo.options, rmlvo_new.options) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 110, "strcmp(rmlvo.options, rmlvo_new.options) == 0"
) : (void)0)
;
111
112 XkbFreeRMLVOSet(&rmlvo, FALSE0);
113}
114
115/**
116 * Get the default RMLVO set.
117 * Set the default RMLVO set.
118 * Get the default RMLVO set.
119 * Repeat the last two steps.
120 *
121 * Result: RMLVO set obtained is the same as previously set.
122 */
123static void
124xkb_set_get_rules_test(void)
125{
126/* This test failed before XkbGetRulesDftlts changed to strdup.
127 We test this twice because the first time using XkbGetRulesDflts we obtain
128 the built-in defaults. The unexpected free isn't triggered until the second
129 XkbSetRulesDefaults.
130 */
131 XkbRMLVOSet rmlvo = { NULL((void*)0) };
132 XkbRMLVOSet rmlvo_backup;
133
134 XkbGetRulesDflts(&rmlvo);
135
136 /* pass 1 */
137 XkbSetRulesDflts(&rmlvo);
138 XkbGetRulesDflts(&rmlvo);
139
140 /* Make a backup copy */
141 rmlvo_backup.rules = strdup(rmlvo.rules);
142 rmlvo_backup.layout = strdup(rmlvo.layout);
143 rmlvo_backup.model = strdup(rmlvo.model);
144 rmlvo_backup.variant = strdup(rmlvo.variant);
2
Memory is allocated
145 rmlvo_backup.options = strdup(rmlvo.options);
146
147 /* pass 2 */
148 XkbSetRulesDflts(&rmlvo);
149
150 /* This test is iffy, because strictly we may be comparing against already
151 * freed memory */
152 assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0)(__builtin_expect(!(strcmp(rmlvo.rules, rmlvo_backup.rules) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 152, "strcmp(rmlvo.rules, rmlvo_backup.rules) == 0"
) : (void)0)
;
153 assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0)(__builtin_expect(!(strcmp(rmlvo.model, rmlvo_backup.model) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 153, "strcmp(rmlvo.model, rmlvo_backup.model) == 0"
) : (void)0)
;
154 assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0)(__builtin_expect(!(strcmp(rmlvo.layout, rmlvo_backup.layout)
== 0), 0) ? __assert_rtn(__func__, "xkb.c", 154, "strcmp(rmlvo.layout, rmlvo_backup.layout) == 0"
) : (void)0)
;
155 assert(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0)(__builtin_expect(!(strcmp(rmlvo.variant, rmlvo_backup.variant
) == 0), 0) ? __assert_rtn(__func__, "xkb.c", 155, "strcmp(rmlvo.variant, rmlvo_backup.variant) == 0"
) : (void)0)
;
156 assert(strcmp(rmlvo.options, rmlvo_backup.options) == 0)(__builtin_expect(!(strcmp(rmlvo.options, rmlvo_backup.options
) == 0), 0) ? __assert_rtn(__func__, "xkb.c", 156, "strcmp(rmlvo.options, rmlvo_backup.options) == 0"
) : (void)0)
;
157
158 XkbGetRulesDflts(&rmlvo);
159 assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0)(__builtin_expect(!(strcmp(rmlvo.rules, rmlvo_backup.rules) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 159, "strcmp(rmlvo.rules, rmlvo_backup.rules) == 0"
) : (void)0)
;
160 assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0)(__builtin_expect(!(strcmp(rmlvo.model, rmlvo_backup.model) ==
0), 0) ? __assert_rtn(__func__, "xkb.c", 160, "strcmp(rmlvo.model, rmlvo_backup.model) == 0"
) : (void)0)
;
161 assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0)(__builtin_expect(!(strcmp(rmlvo.layout, rmlvo_backup.layout)
== 0), 0) ? __assert_rtn(__func__, "xkb.c", 161, "strcmp(rmlvo.layout, rmlvo_backup.layout) == 0"
) : (void)0)
;
162 assert(strcmp(rmlvo.variant, rmlvo_backup.variant) == 0)(__builtin_expect(!(strcmp(rmlvo.variant, rmlvo_backup.variant
) == 0), 0) ? __assert_rtn(__func__, "xkb.c", 162, "strcmp(rmlvo.variant, rmlvo_backup.variant) == 0"
) : (void)0)
;
163 assert(strcmp(rmlvo.options, rmlvo_backup.options) == 0)(__builtin_expect(!(strcmp(rmlvo.options, rmlvo_backup.options
) == 0), 0) ? __assert_rtn(__func__, "xkb.c", 163, "strcmp(rmlvo.options, rmlvo_backup.options) == 0"
) : (void)0)
;
3
Within the expansion of the macro 'assert':
a
Potential leak of memory pointed to by 'rmlvo_backup.variant'
164}
165
166int
167main(int argc, char **argv)
168{
169 xkb_set_get_rules_test();
1
Calling 'xkb_set_get_rules_test'
170 xkb_get_rules_test();
171 xkb_set_rules_test();
172
173 return 0;
174}