OSDN Git Service

Avoid overflow in 'last' variable of FindGLXFunction(...)
[android-x86/external-mesa.git] / src / glx / glxglvnd.c
1 #include <string.h>
2 #include <X11/Xlib.h>
3
4 #include "glvnd/libglxabi.h"
5
6 #include "glxglvnd.h"
7
8
9 static Bool __glXGLVNDIsScreenSupported(Display *dpy, int screen)
10 {
11     /* TODO: Think of a better heuristic... */
12     return True;
13 }
14
15 static void *__glXGLVNDGetProcAddress(const GLubyte *procName)
16 {
17     return glXGetProcAddressARB(procName);
18 }
19
20 static unsigned FindGLXFunction(const GLubyte *name)
21 {
22     int first = 0;
23     int last = DI_FUNCTION_COUNT - 1;
24
25     while (first <= last) {
26         int middle = (first + last) / 2;
27         int comp = strcmp((const char *) name,
28                           __glXDispatchTableStrings[middle]);
29
30         if (comp < 0)
31             first = middle + 1;
32         else if (comp > 0)
33             last = middle - 1;
34         else
35             return middle;
36     }
37
38     /* Just point to the dummy entry at the end of the respective table */
39     return DI_FUNCTION_COUNT;
40 }
41
42 static void *__glXGLVNDGetDispatchAddress(const GLubyte *procName)
43 {
44     unsigned internalIndex = FindGLXFunction(procName);
45
46     return __glXDispatchFunctions[internalIndex];
47 }
48
49 static void __glXGLVNDSetDispatchIndex(const GLubyte *procName, int index)
50 {
51     unsigned internalIndex = FindGLXFunction(procName);
52
53     __glXDispatchTableIndices[internalIndex] = index;
54 }
55
56 _X_EXPORT Bool __glx_Main(uint32_t version, const __GLXapiExports *exports,
57                           __GLXvendorInfo *vendor, __GLXapiImports *imports)
58 {
59     static Bool initDone = False;
60
61     if (GLX_VENDOR_ABI_GET_MAJOR_VERSION(version) !=
62         GLX_VENDOR_ABI_MAJOR_VERSION ||
63         GLX_VENDOR_ABI_GET_MINOR_VERSION(version) <
64         GLX_VENDOR_ABI_MINOR_VERSION)
65         return False;
66
67     if (!initDone) {
68         initDone = True;
69         __glXGLVNDAPIExports = exports;
70
71         imports->isScreenSupported = __glXGLVNDIsScreenSupported;
72         imports->getProcAddress = __glXGLVNDGetProcAddress;
73         imports->getDispatchAddress = __glXGLVNDGetDispatchAddress;
74         imports->setDispatchIndex = __glXGLVNDSetDispatchIndex;
75         imports->notifyError = NULL;
76         imports->isPatchSupported = NULL;
77         imports->initiatePatch = NULL;
78     }
79
80     return True;
81 }