gallium/u_vbuf: Protect against overflow with large instance divisors.
[mesa.git] / src / gallium / auxiliary / util / u_debug.c
index db6635713e5183000cfb6e51a07d8e531fde5cb8..edfb27fc6f6ffc8a314b2d9c3bde75487bbc0512 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "pipe/p_compiler.h"
 #include "util/u_debug.h"
+#include "util/u_dump.h"
 #include "pipe/p_format.h"
 #include "pipe/p_state.h"
 #include "util/u_inlines.h"
@@ -203,25 +204,16 @@ debug_get_num_option(const char *name, long dfault)
    const char *str;
 
    str = os_get_option(name);
-   if (!str)
+   if (!str) {
       result = dfault;
-   else {
-      long sign;
-      char c;
-      c = *str++;
-      if (c == '-') {
-        sign = -1;
-        c = *str++;
-      }
-      else {
-        sign = 1;
-      }
-      result = 0;
-      while ('0' <= c && c <= '9') {
-        result = result*10 + (c - '0');
-        c = *str++;
+   } else {
+      char *endptr;
+
+      result = strtol(str, &endptr, 0);
+      if (str == endptr) {
+         /* Restore the default value when no digits were found. */
+         result = dfault;
       }
-      result *= sign;
    }
 
    if (debug_get_option_should_print())
@@ -423,7 +415,7 @@ debug_print_format(const char *msg, unsigned fmt )
 
 /** Return string name of given primitive type */
 const char *
-u_prim_name(unsigned prim)
+u_prim_name(enum pipe_prim_type prim)
 {
    static const struct debug_named_value names[] = {
       DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
@@ -452,7 +444,8 @@ int fl_indent = 0;
 const char* fl_function[1024];
 
 int
-debug_funclog_enter(const char* f, const int line, const char* file)
+debug_funclog_enter(const char* f, UNUSED const int line,
+                    UNUSED const char* file)
 {
    int i;
 
@@ -467,7 +460,8 @@ debug_funclog_enter(const char* f, const int line, const char* file)
 }
 
 void
-debug_funclog_exit(const char* f, const int line, const char* file)
+debug_funclog_exit(const char* f, UNUSED const int line,
+                   UNUSED const char* file)
 {
    --fl_indent;
    assert(fl_indent >= 0);
@@ -475,7 +469,8 @@ debug_funclog_exit(const char* f, const int line, const char* file)
 }
 
 void
-debug_funclog_enter_exit(const char* f, const int line, const char* file)
+debug_funclog_enter_exit(const char* f, UNUSED const int line,
+                         UNUSED const char* file)
 {
    int i;
    for (i = 0; i < fl_indent; i++)
@@ -493,21 +488,9 @@ debug_funclog_enter_exit(const char* f, const int line, const char* file)
 void
 debug_print_transfer_flags(const char *msg, unsigned usage)
 {
-   static const struct debug_named_value names[] = {
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_READ),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_WRITE),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_MAP_DIRECTLY),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_RANGE),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_DONTBLOCK),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_UNSYNCHRONIZED),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_FLUSH_EXPLICIT),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_PERSISTENT),
-      DEBUG_NAMED_VALUE(PIPE_TRANSFER_COHERENT),
-      DEBUG_NAMED_VALUE_END
-   };
-
-   debug_printf("%s: %s\n", msg, debug_dump_flags(names, usage));
+   debug_printf("%s: ", msg);
+   util_dump_transfer_usage(stdout, usage);
+   printf("\n");
 }
 
 
@@ -526,8 +509,6 @@ debug_print_bind_flags(const char *msg, unsigned usage)
       DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER),
       DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER),
       DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET),
-      DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_WRITE),
-      DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_READ),
       DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT),
       DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR),
       DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM),
@@ -550,7 +531,7 @@ debug_print_bind_flags(const char *msg, unsigned usage)
  * Print PIPE_USAGE_x enum values with a message.
  */
 void
-debug_print_usage_enum(const char *msg, unsigned usage)
+debug_print_usage_enum(const char *msg, enum pipe_resource_usage usage)
 {
    static const struct debug_named_value names[] = {
       DEBUG_NAMED_VALUE(PIPE_USAGE_DEFAULT),