v2: zero the buffer before reading into it to protect against malformed input file [aaronp]
The big change is to defer strdup until we actually know the line matches the extension we're initializing. This cuts the number of calls to strdup on this path from 24090 to 554, and 'Xvfb -pogo' drops from about 45M to 38M instructions retired according to valgrind. Signed-off-by: Adam Jackson <[email protected]> --- dix/registry.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/dix/registry.c b/dix/registry.c index 82a3340..3880824 100644 --- a/dix/registry.c +++ b/dix/registry.c @@ -124,13 +124,17 @@ RegisterExtensionNames(ExtensionEntry * extEntry) { char buf[256], *lineobj, *ptr; unsigned offset; + int namelen; if (fh == NULL) return; + namelen = strlen(extEntry->name); + rewind(fh); - while (fgets(buf, sizeof(buf), fh)) { + /* clear the buffer before fgets'ing into it */ + while (memset(buf, 0, sizeof (buf)), fgets(buf, sizeof(buf), fh)) { lineobj = NULL; ptr = strchr(buf, '\n'); if (ptr) @@ -149,26 +153,22 @@ RegisterExtensionNames(ExtensionEntry * extEntry) goto invalid; } - /* Check for space character in the fifth position */ - ptr = strchr(buf, ' '); - if (!ptr || ptr != buf + 4) + /* check for proper formatting */ + if (buf[4] != ' ') goto invalid; + /* check for extension name match */ + if (strncmp(buf + 5, extEntry->name, namelen)) + goto skip; + + if (buf[5 + namelen] != ':') + goto skip; + /* Duplicate the string after the space */ - lineobj = strdup(ptr + 1); + lineobj = strdup(buf + 5); if (!lineobj) continue; - /* Check for a colon somewhere on the line */ - ptr = strchr(buf, ':'); - if (!ptr) - goto invalid; - - /* Compare the part before colon with the target extension name */ - *ptr = 0; - if (strcmp(buf + 5, extEntry->name)) - goto skip; - /* Get the opcode for the request, event, or error */ offset = strtol(buf + 1, &ptr, 10); if (offset == 0 && ptr == buf + 1) -- 1.8.4.2 _______________________________________________ [email protected]: X.Org development Archives: http://lists.x.org/archives/xorg-devel Info: http://lists.x.org/mailman/listinfo/xorg-devel
