On Mon, Mar 21, 2016 at 9:37 AM, Miguel A. Vico <[email protected]> wrote:
> Using strstr(3) for checking for extensions is an error-prone mechanism > as extension names can be prefixes of other extension names (see > https://www.opengl.org/registry/doc/rules.html#using). > > This change implements the check_extension() function to properly check > for an extension and replaces all usages of strstr(3). > > Signed-off-by: Miguel A Vico Moya <[email protected]> > Reviewed-by: Andy Ritger <[email protected]> > --- > src/gl-renderer.c | 56 > +++++++++++++++++++++++++++++++++++++++++-------------- > 1 file changed, 42 insertions(+), 14 deletions(-) > > diff --git a/src/gl-renderer.c b/src/gl-renderer.c > index 1d6d98c..3ca1aed 100644 > --- a/src/gl-renderer.c > +++ b/src/gl-renderer.c > @@ -2701,6 +2701,34 @@ gl_renderer_destroy(struct weston_compositor *ec) > free(gr); > } > > +static > +bool check_extension(const char *extensions, const char *extension) > +{ > + size_t extlen = strlen(extension); > + const char *end = extensions + strlen(extensions); > + > + while (extensions < end) { > + size_t n = 0; > + > + /* Skip whitespaces, if any */ > + if (*extensions == ' ') { > + extensions++; > + continue; > + } > + > + n = strcspn(extensions, " "); > + > + /* Compare strings */ > + if (n == extlen && strncmp(extension, extensions, n) == 0) > + return true; /* Found */ > + > + extensions += n; > + } > + > + /* Not found */ > + return false; > +} > + > Slightly faster version that avoids some temporaries and strlen calls: static bool check_extension(const char *extensions, const char *extension) { while (*extensions) { size_t n = strcspn(extensions, " "); /* Skip whitespaces, if any */ if (n < 1) { extensions++; continue; } /* Compare strings */ if (strncmp(extension, extensions, n) == 0 && !extension[n]) return true; /* Found */ extensions += n; } /* Not found */ return false; }
_______________________________________________ wayland-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/wayland-devel
