Merge lp:~bregma/compiz/lp-1101608 into lp:compiz/0.9.12

Proposed by Stephen M. Webb
Status: Merged
Approved by: Christopher Townsend
Approved revision: 3910
Merged at revision: 3912
Proposed branch: lp:~bregma/compiz/lp-1101608
Merge into: lp:compiz/0.9.12
Diff against target: 126 lines (+52/-30)
1 file modified
compizconfig/libcompizconfig/src/compiz.cpp (+52/-30)
To merge this branch: bzr merge lp:~bregma/compiz/lp-1101608
Reviewer Review Type Date Requested Status
Christopher Townsend Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+243377@code.launchpad.net

Commit message

libcompizconfig: reorder stat() and open() calls to eliminate a race condition

Description of the change

Reorder stat() and open() calls to eliminate a potential race condition when loading COmpiz configuration.

To post a comment you must log in.
lp:~bregma/compiz/lp-1101608 updated
3910. By Stephen M. Webb

fixed code style

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Christopher Townsend (townsend) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'compizconfig/libcompizconfig/src/compiz.cpp'
2--- compizconfig/libcompizconfig/src/compiz.cpp 2014-08-04 09:01:36 +0000
3+++ compizconfig/libcompizconfig/src/compiz.cpp 2014-12-02 11:35:42 +0000
4@@ -35,7 +35,9 @@
5 #include <dirent.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8+#include <fcntl.h>
9 #include <glib.h>
10+#include <unistd.h>
11
12 #include <libxslt/transform.h>
13 #include <libxslt/xsltutils.h>
14@@ -1973,7 +1975,7 @@
15 Bool success = ccsCreateDirFor (metadataCacheFileDummy.c_str ());
16 if (!success)
17 ccsError ("Error creating directory \"%s\"",
18- metadataCacheDir.c_str ());
19+ metadataCacheDir.c_str ());
20 free (cacheBaseDir);
21
22 if (success)
23@@ -2899,16 +2901,24 @@
24 #endif
25
26 // Load from .xml
27- FILE *fp = fopen (xmlFilePath, "r");
28+ int fd = open (xmlFilePath, O_RDONLY);
29 #ifdef USE_PROTOBUF
30 Bool xmlLoaded = FALSE;
31 #endif
32
33- if (fp)
34- {
35- fclose (fp);
36- xmlDoc *doc = xmlReadFile (xmlFilePath, NULL, 0);
37- if (doc)
38+ if (-1 == fd)
39+ {
40+ ccsError ("error %d opening %s: %s",
41+ errno, xmlFilePath, strerror(errno));
42+ }
43+ else
44+ {
45+ xmlDoc *doc = xmlReadFd (fd, xmlFilePath, NULL, 0);
46+ if (!doc)
47+ {
48+ ccsError ("error parsing %s", xmlFilePath);
49+ }
50+ else
51 {
52 #ifdef USE_PROTOBUF
53 xmlLoaded =
54@@ -2917,6 +2927,7 @@
55 pluginInfoPBv);
56 xmlFreeDoc (doc);
57 }
58+ close (fd);
59 }
60 free (xmlFilePath);
61
62@@ -3163,30 +3174,41 @@
63 {
64 CCSPluginPrivate *pPrivate = GET_PRIVATE (CCSPluginPrivate, plugin);
65
66- xmlDoc *doc = NULL;
67- xmlNode **nodes;
68- int num;
69-
70- if (stat (pPrivate->xmlFile, xmlStat))
71- return;
72-
73- FILE *fp = fopen (pPrivate->xmlFile, "r");
74- if (!fp)
75- return;
76-
77- fclose (fp);
78- doc = xmlReadFile (pPrivate->xmlFile, NULL, 0);
79-
80- nodes = getNodesFromXPath (doc, NULL, pPrivate->xmlPath, &num);
81- if (num)
82- {
83- initOptionsFromRootNode (plugin, nodes[0], pluginPBv);
84- if (!basicMetadata)
85- initStringExtensionsFromRootNode (plugin, nodes[0], pluginPBv);
86- free (nodes);
87- }
88- if (doc)
89+ int fd = open (pPrivate->xmlFile, O_RDONLY);
90+ if (-1 == fd)
91+ {
92+ ccsError ("error %d opening %s: %s",
93+ errno, pPrivate->xmlFile, strerror(errno));
94+ return;
95+ }
96+
97+ if (-1 == fstat (fd, xmlStat))
98+ {
99+ ccsError ("error %d statting %s: %s",
100+ errno, pPrivate->xmlFile, strerror(errno));
101+ close (fd);
102+ return;
103+ }
104+
105+ xmlDoc *doc = xmlReadFd (fd, pPrivate->xmlFile, NULL, 0);
106+ if (!doc)
107+ {
108+ ccsError ("error parsing %s", pPrivate->xmlFile);
109+ }
110+ else
111+ {
112+ int num = 0;
113+ xmlNode **nodes = getNodesFromXPath (doc, NULL, pPrivate->xmlPath, &num);
114+ if (num)
115+ {
116+ initOptionsFromRootNode (plugin, nodes[0], pluginPBv);
117+ if (!basicMetadata)
118+ initStringExtensionsFromRootNode (plugin, nodes[0], pluginPBv);
119+ free (nodes);
120+ }
121 xmlFreeDoc (doc);
122+ }
123+ close (fd);
124 }
125
126 void

Subscribers

People subscribed via source and target branches