[Xcb-commit] 5 commits - src
Julien Danjou
jdanjou at kemper.freedesktop.org
Fri Apr 10 01:30:33 PDT 2009
src/xcb_auth.c | 31 +++++++++++++++-------
src/xcb_util.c | 77 ++++++++++++++++++++++++---------------------------------
src/xcbint.h | 4 ++
3 files changed, 58 insertions(+), 54 deletions(-)
New commits:
commit ca978a9dae621126075712f9e2c29591208570bc
Author: Julien Danjou <julien at danjou.info>
Date: Tue Apr 7 14:22:57 2009 +0200
util: remove useless strlen calls from decnet opening
Signed-off-by: Julien Danjou <julien at danjou.info>
diff --git a/src/xcb_util.c b/src/xcb_util.c
index 54361f4..adac909 100644
--- a/src/xcb_util.c
+++ b/src/xcb_util.c
@@ -193,8 +193,9 @@ static int _xcb_open_decnet(const char *host, const char *protocol, const unsign
addr.sdn_add.a_len = nodeaddr->n_length;
memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
- sprintf((char *)addr.sdn_objname, "X$X%d", port);
- addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
+ addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
+ if(addr.sdn_objnamel < 0)
+ return -1;
addr.sdn_objnum = 0;
fd = socket(PF_DECnet, SOCK_STREAM, 0);
@@ -202,8 +203,9 @@ static int _xcb_open_decnet(const char *host, const char *protocol, const unsign
return -1;
memset(&accessdata, 0, sizeof(accessdata));
- sprintf((char*)accessdata.acc_acc, "%d", getuid());
- accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
+ accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
+ if(accessdata.acc_accl < 0)
+ return -1;
setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
commit cc191431412d8764c645a51b0f106c0dfe652213
Author: Julien Danjou <julien at danjou.info>
Date: Tue Apr 7 14:18:40 2009 +0200
util: merge common code for xcb_connect
Many code was duplicated between xcb_connect_to_display_with_auth_info
and xcb_connect(). We merge both, since the difference is just about the
xcb_auth_info_t pointer being supplied, or not.
Signed-off-by: Julien Danjou <julien at danjou.info>
diff --git a/src/xcb_util.c b/src/xcb_util.c
index 008a38b..54361f4 100644
--- a/src/xcb_util.c
+++ b/src/xcb_util.c
@@ -317,39 +317,7 @@ static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
{
- int fd, display = 0;
- char *host;
- char *protocol;
- xcb_connection_t *c;
- xcb_auth_info_t auth;
-
- int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
-
-#ifdef HAVE_LAUNCHD
- if(!displayname)
- displayname = getenv("DISPLAY");
- if(displayname && strlen(displayname)>11 && !strncmp(displayname, "/tmp/launch", 11))
- fd = _xcb_open_unix(NULL, displayname);
- else
-#endif
- if(!parsed)
- return (xcb_connection_t *) &error_connection;
- else
- fd = _xcb_open(host, protocol, display);
- free(host);
-
- if(fd == -1)
- return (xcb_connection_t *) &error_connection;
-
- if(_xcb_get_auth_info(fd, &auth, display))
- {
- c = xcb_connect_to_fd(fd, &auth);
- free(auth.name);
- free(auth.data);
- }
- else
- c = xcb_connect_to_fd(fd, 0);
- return c;
+ return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
}
xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
@@ -357,6 +325,8 @@ xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname,
int fd, display = 0;
char *host;
char *protocol;
+ xcb_auth_info_t ourauth;
+ xcb_connection_t *c;
int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
@@ -376,5 +346,17 @@ xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname,
if(fd == -1)
return (xcb_connection_t *) &error_connection;
- return xcb_connect_to_fd(fd, auth);
+ if(auth)
+ return xcb_connect_to_fd(fd, auth);
+
+ if(_xcb_get_auth_info(fd, &ourauth, display))
+ {
+ c = xcb_connect_to_fd(fd, &ourauth);
+ free(ourauth.name);
+ free(ourauth.data);
+ }
+ else
+ c = xcb_connect_to_fd(fd, 0);
+
+ return c;
}
commit 8797e053b2b2ee989f47490c7687b9a2fbdb0021
Author: Julien Danjou <julien at danjou.info>
Date: Tue Apr 7 13:37:40 2009 +0200
util: open_abstract gets filelen as parameters
That saves us from a couple of strlen() calls.
Signed-off-by: Julien Danjou <julien at danjou.info>
diff --git a/src/xcb_util.c b/src/xcb_util.c
index 4ae9097..008a38b 100644
--- a/src/xcb_util.c
+++ b/src/xcb_util.c
@@ -125,7 +125,7 @@ static int _xcb_open_unix(char *protocol, const char *file);
static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
#endif
#ifdef HAVE_ABSTRACT_SOCKETS
-static int _xcb_open_abstract(char *protocol, const char *file);
+static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
#endif
static int _xcb_open(char *host, char *protocol, const int display)
@@ -135,6 +135,7 @@ static int _xcb_open(char *host, char *protocol, const int display)
#endif
static const char base[] = "/tmp/.X11-unix/X";
char file[sizeof(base) + 20];
+ int filelen;
if(*host)
{
@@ -161,9 +162,13 @@ static int _xcb_open(char *host, char *protocol, const int display)
}
/* display specifies Unix socket */
- snprintf(file, sizeof(file), "%s%d", base, display);
+ filelen = snprintf(file, sizeof(file), "%s%d", base, display);
+ if(filelen < 0)
+ return -1;
+ /* snprintf may truncate the file */
+ filelen = MIN(filelen, sizeof(file) - 1);
#ifdef HAVE_ABSTRACT_SOCKETS
- fd = _xcb_open_abstract(protocol, file);
+ fd = _xcb_open_abstract(protocol, file, filelen);
if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
return fd;
@@ -284,7 +289,7 @@ static int _xcb_open_unix(char *protocol, const char *file)
}
#ifdef HAVE_ABSTRACT_SOCKETS
-static int _xcb_open_abstract(char *protocol, const char *file)
+static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
{
int fd;
struct sockaddr_un addr = {0};
@@ -295,9 +300,9 @@ static int _xcb_open_abstract(char *protocol, const char *file)
strcpy(addr.sun_path + 1, file);
addr.sun_family = AF_UNIX;
- namelen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(file);
+ namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
#ifdef HAVE_SOCKADDR_SUN_LEN
- addr.sun_len = 1 + strlen(file);
+ addr.sun_len = 1 + filelen;
#endif
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
commit f0b29819749b769e5a8d313bf1bab80d6513208b
Author: Julien Danjou <julien at danjou.info>
Date: Tue Apr 7 11:55:30 2009 +0200
auth: use snprintf() return value
That save us from a strlen().
Signed-off-by: Julien Danjou <julien at danjou.info>
diff --git a/src/xcb_auth.c b/src/xcb_auth.c
index a648b16..6e0ff46 100644
--- a/src/xcb_auth.c
+++ b/src/xcb_auth.c
@@ -97,6 +97,7 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
unsigned short family;
char hostnamebuf[256]; /* big enough for max hostname */
char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
+ int dispbuflen;
family = FamilyLocal; /* 256 */
switch(sockname->sa_family)
@@ -127,7 +128,11 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
return 0; /* cannot authenticate this family */
}
- snprintf(dispbuf, sizeof(dispbuf), "%d", display);
+ dispbuflen = snprintf(dispbuf, sizeof(dispbuf), "%d", display);
+ if(dispbuflen < 0)
+ return 0;
+ /* snprintf may have truncate our text */
+ dispbuflen = MIN(dispbuflen, sizeof(dispbuf) - 1);
if (family == FamilyLocal) {
if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1)
@@ -138,7 +143,7 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
return XauGetBestAuthByAddr (family,
(unsigned short) addrlen, addr,
- (unsigned short) strlen(dispbuf), dispbuf,
+ (unsigned short) dispbuflen, dispbuf,
N_AUTH_PROTOS, authnames, authnameslen);
}
diff --git a/src/xcbint.h b/src/xcbint.h
index dac0a61..154cca0 100644
--- a/src/xcbint.h
+++ b/src/xcbint.h
@@ -60,6 +60,10 @@ enum lazy_reply_tag
#define offsetof(type,member) ((size_t) &((type *)0)->member)
#endif
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+#endif
+
#define container_of(pointer,type,member) ((type *)(((char *)(pointer)) - offsetof(type, member)))
/* xcb_list.c */
commit 9f24c91f91dd68a52e46191b686283b0df38d2f5
Author: Julien Danjou <julien at danjou.info>
Date: Tue Apr 7 11:49:13 2009 +0200
auth: precompute authnameslen
Signed-off-by: Julien Danjou <julien at danjou.info>
diff --git a/src/xcb_auth.c b/src/xcb_auth.c
index 93a6f68..a648b16 100644
--- a/src/xcb_auth.c
+++ b/src/xcb_auth.c
@@ -49,11 +49,21 @@ enum auth_protos {
N_AUTH_PROTOS
};
+#define AUTH_PROTO_XDM_AUTHORIZATION "XDM-AUTHORIZATION-1"
+#define AUTH_PROTO_MIT_MAGIC_COOKIE "MIT-MAGIC-COOKIE-1"
+
static char *authnames[N_AUTH_PROTOS] = {
#ifdef HASXDMAUTH
- "XDM-AUTHORIZATION-1",
+ AUTH_PROTO_XDM_AUTHORIZATION,
+#endif
+ AUTH_PROTO_MIT_MAGIC_COOKIE,
+};
+
+static int authnameslen[N_AUTH_PROTOS] = {
+#ifdef HASXDMAUTH
+ sizeof(AUTH_PROTO_XDM_AUTHORIZATION) - 1,
#endif
- "MIT-MAGIC-COOKIE-1",
+ sizeof(AUTH_PROTO_MIT_MAGIC_COOKIE) - 1,
};
static size_t memdup(char **dst, void *src, size_t len)
@@ -70,7 +80,7 @@ static size_t memdup(char **dst, void *src, size_t len)
static int authname_match(enum auth_protos kind, char *name, size_t namelen)
{
- if(strlen(authnames[kind]) != namelen)
+ if(authnameslen[kind] != namelen)
return 0;
if(memcmp(authnames[kind], name, namelen))
return 0;
@@ -87,8 +97,6 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
unsigned short family;
char hostnamebuf[256]; /* big enough for max hostname */
char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */
- int authnamelens[N_AUTH_PROTOS];
- int i;
family = FamilyLocal; /* 256 */
switch(sockname->sa_family)
@@ -128,12 +136,10 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen,
addrlen = strlen(addr);
}
- for (i = 0; i < N_AUTH_PROTOS; i++)
- authnamelens[i] = strlen(authnames[i]);
return XauGetBestAuthByAddr (family,
(unsigned short) addrlen, addr,
(unsigned short) strlen(dispbuf), dispbuf,
- N_AUTH_PROTOS, authnames, authnamelens);
+ N_AUTH_PROTOS, authnames, authnameslen);
}
#ifdef HASXDMAUTH
More information about the xcb-commit
mailing list