gl-renderer: Implement & use check_extension

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 <mvicomoya@nvidia.com>
Reviewed-by: Andy Ritger <aritger@nvidia.com>
Reviewed-by: Daniel Stone <daniels@collabora.com>
[Pekka: move 'bool' to the same line with 'static']
Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
diff --git a/src/gl-renderer.c b/src/gl-renderer.c
index cb08344..23c0cd7 100644
--- a/src/gl-renderer.c
+++ b/src/gl-renderer.c
@@ -2668,6 +2668,34 @@
 	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;
+}
+
 static void
 renderer_setup_egl_client_extensions(struct gl_renderer *gr)
 {
@@ -2679,7 +2707,7 @@
 		return;
 	}
 
-	if (strstr(extensions, "EGL_EXT_platform_base"))
+	if (check_extension(extensions, "EGL_EXT_platform_base"))
 		gr->create_platform_window =
 			(void *) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
 	else
@@ -2709,7 +2737,7 @@
 		return -1;
 	}
 
-	if (strstr(extensions, "EGL_WL_bind_wayland_display"))
+	if (check_extension(extensions, "EGL_WL_bind_wayland_display"))
 		gr->has_bind_display = 1;
 	if (gr->has_bind_display) {
 		ret = gr->bind_display(gr->egl_display, ec->wl_display);
@@ -2717,14 +2745,14 @@
 			gr->has_bind_display = 0;
 	}
 
-	if (strstr(extensions, "EGL_EXT_buffer_age"))
+	if (check_extension(extensions, "EGL_EXT_buffer_age"))
 		gr->has_egl_buffer_age = 1;
 	else
 		weston_log("warning: EGL_EXT_buffer_age not supported. "
 			   "Performance could be affected.\n");
 
 #ifdef EGL_EXT_swap_buffers_with_damage
-	if (strstr(extensions, "EGL_EXT_swap_buffers_with_damage"))
+	if (check_extension(extensions, "EGL_EXT_swap_buffers_with_damage"))
 		gr->swap_buffers_with_damage =
 			(void *) eglGetProcAddress("eglSwapBuffersWithDamageEXT");
 	else
@@ -2733,12 +2761,12 @@
 #endif
 
 #ifdef EGL_MESA_configless_context
-	if (strstr(extensions, "EGL_MESA_configless_context"))
+	if (check_extension(extensions, "EGL_MESA_configless_context"))
 		gr->has_configless_context = 1;
 #endif
 
 #ifdef EGL_EXT_image_dma_buf_import
-	if (strstr(extensions, "EGL_EXT_image_dma_buf_import"))
+	if (check_extension(extensions, "EGL_EXT_image_dma_buf_import"))
 		gr->has_dmabuf_import = 1;
 #endif
 
@@ -2807,19 +2835,19 @@
 			       extensions);
 	}
 
-	if (!strstr(extensions, "EGL_EXT_platform_base"))
+	if (!check_extension(extensions, "EGL_EXT_platform_base"))
 		return 0;
 
 	snprintf(s, sizeof s, "EGL_KHR_platform_%s", extension_suffix);
-	if (strstr(extensions, s))
+	if (check_extension(extensions, s))
 		return 1;
 
 	snprintf(s, sizeof s, "EGL_EXT_platform_%s", extension_suffix);
-	if (strstr(extensions, s))
+	if (check_extension(extensions, s))
 		return 1;
 
 	snprintf(s, sizeof s, "EGL_MESA_platform_%s", extension_suffix);
-	if (strstr(extensions, s))
+	if (check_extension(extensions, s))
 		return 1;
 
 	/* at this point we definitely have some platform extensions but
@@ -3071,22 +3099,22 @@
 		return -1;
 	}
 
-	if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
+	if (!check_extension(extensions, "GL_EXT_texture_format_BGRA8888")) {
 		weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
 		return -1;
 	}
 
-	if (strstr(extensions, "GL_EXT_read_format_bgra"))
+	if (check_extension(extensions, "GL_EXT_read_format_bgra"))
 		ec->read_format = PIXMAN_a8r8g8b8;
 	else
 		ec->read_format = PIXMAN_a8b8g8r8;
 
 #ifdef GL_EXT_unpack_subimage
-	if (strstr(extensions, "GL_EXT_unpack_subimage"))
+	if (check_extension(extensions, "GL_EXT_unpack_subimage"))
 		gr->has_unpack_subimage = 1;
 #endif
 
-	if (strstr(extensions, "GL_OES_EGL_image_external"))
+	if (check_extension(extensions, "GL_OES_EGL_image_external"))
 		gr->has_egl_image_external = 1;
 
 	glActiveTexture(GL_TEXTURE0);