fix for-loop in _mesa_GetDouble to avoid out of bounds memory read
[mesa.git] / src / mesa / main / occlude.c
index 73a29cc93921629429ffc5f269123ce1b2f9a46e..bc61a475d663e2b679fcf7f75fe6622d0b1748cc 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.0.2
  *
- * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
 #include "occlude.h"
 #include "mtypes.h"
 
-#ifndef GL_SAMPLES_PASSED_ARB
-#define GL_SAMPLES_PASSED_ARB         0x8914
-#define GL_QUERY_COUNTER_BITS_ARB     0x8864
-#define GL_CURRENT_QUERY_ARB          0x8865
-#define GL_QUERY_RESULT_ARB           0x8866
-#define GL_QUERY_RESULT_AVAILABLE_ARB 0x8867
-#endif
-
 
 struct occlusion_query
 {
@@ -53,20 +45,6 @@ struct occlusion_query
 };
 
 
-
-void
-_mesa_init_occlude(GLcontext *ctx)
-{
-#if FEATURE_ARB_occlusion_query
-   ctx->Occlusion.QueryObjects = _mesa_NewHashTable();
-#endif
-
-   ctx->OcclusionResult = GL_FALSE;
-   ctx->OcclusionResultSaved = GL_FALSE;
-}
-
-
-
 /**
  * Allocate a new occlusion query object.
  * \param target - must be GL_SAMPLES_PASSED_ARB at this time
@@ -188,6 +166,11 @@ _mesa_BeginQueryARB(GLenum target, GLuint id)
       return;
    }
 
+   if (id == 0) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(id==0)");
+      return;
+   }
+
    if (ctx->Occlusion.CurrentQueryObject) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glBeginQueryARB(target)");
       return;
@@ -220,7 +203,7 @@ void GLAPIENTRY
 _mesa_EndQueryARB(GLenum target)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct occlusion_query *q;
+   struct occlusion_query *q = NULL;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    FLUSH_VERTICES(ctx, _NEW_DEPTH);
@@ -230,11 +213,12 @@ _mesa_EndQueryARB(GLenum target)
       return;
    }
 
-   q = (struct occlusion_query *)
-      _mesa_HashLookup(ctx->Occlusion.QueryObjects,
-                       ctx->Occlusion.CurrentQueryObject);
+   if (ctx->Occlusion.CurrentQueryObject)
+      q = (struct occlusion_query *)
+         _mesa_HashLookup(ctx->Occlusion.QueryObjects,
+                          ctx->Occlusion.CurrentQueryObject);
    if (!q || !q->Active) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glEndQuery with glBeginQuery");
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glEndQuery with no glBeginQuery");
       return;
    }
 
@@ -274,13 +258,15 @@ void GLAPIENTRY
 _mesa_GetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct occlusion_query *q;
+   struct occlusion_query *q = NULL;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   q = (struct occlusion_query *)
-      _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
+   if (id)
+      q = (struct occlusion_query *)
+         _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
+
    if (!q || q->Active) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectivARB");
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectivARB(id=%d)", id);
       return;
    }
 
@@ -303,13 +289,14 @@ void GLAPIENTRY
 _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct occlusion_query *q;
+   struct occlusion_query *q = NULL;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   q = (struct occlusion_query *)
-      _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
+   if (id)
+      q = (struct occlusion_query *)
+         _mesa_HashLookup(ctx->Occlusion.QueryObjects, id);
    if (!q || q->Active) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectuivARB");
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetQueryObjectuivARB(id=%d", id);
       return;
    }
 
@@ -326,3 +313,41 @@ _mesa_GetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
          return;
    }
 }
+
+
+
+/**
+ * Allocate/init the context state related to occlusion query objects.
+ */
+void
+_mesa_init_occlude(GLcontext *ctx)
+{
+#if FEATURE_ARB_occlusion_query
+   ctx->Occlusion.QueryObjects = _mesa_NewHashTable();
+#endif
+   ctx->OcclusionResult = GL_FALSE;
+   ctx->OcclusionResultSaved = GL_FALSE;
+}
+
+
+/**
+ * Free the context state related to occlusion query objects.
+ */
+void
+_mesa_free_occlude_data(GLcontext *ctx)
+{
+   while (1) {
+      GLuint query = _mesa_HashFirstEntry(ctx->Occlusion.QueryObjects);
+      if (query) {
+         struct occlusion_query *q = (struct occlusion_query *)
+            _mesa_HashLookup(ctx->Occlusion.QueryObjects, query);
+         ASSERT(q);
+         delete_query_object(q);
+         _mesa_HashRemove(ctx->Occlusion.QueryObjects, query);
+      }
+      else {
+         break;
+      }
+   }
+   _mesa_DeleteHashTable(ctx->Occlusion.QueryObjects);
+}