Fix KHR_Debug segfault errors

Calls to functions ObjectLabel, ObjectPtrLabel, GetObjectLabel,
GetObjectPtrLabel were resulting into segfaults due to improper
validation and parameter handling. It could be that the
implementations of those functions were based on an earlier version
of the KHR_Debug extension.

The patch fixes the segfault error and almost all failing dEQP 3.1
tests related to KHR_Debug. The failing tests were also relevant to
older ES versions. There is still one failing test, but that one
fails since ES3.1 is not fully supported yet.

List of reasons for the segfault error and failing tests:
- the segfault error was caused by strlen called on a null pointer
- another segfault was caused by writing out the length to a null
pointer
- even if the buffer size for getObject(Ptr)Label is 0, still the
length of the label can be returned. That was not handled.

BUG=angleproject:1446
TEST=angle_deqp_gtest_gles31_tests
--gtest_filter=*functional_debug_object*

Change-Id: I4743be8e862f3620091061cd7abb206a426655ed
Reviewed-on: https://chromium-review.googlesource.com/361300
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/libANGLE/validationES2.cpp b/src/libANGLE/validationES2.cpp
index 8acf7da..9d7129b 100644
--- a/src/libANGLE/validationES2.cpp
+++ b/src/libANGLE/validationES2.cpp
@@ -1601,6 +1601,32 @@
     }
 }
 
+static bool ValidateLabelLength(Context *context, GLsizei length, const GLchar *label)
+{
+    size_t labelLength = 0;
+
+    if (length < 0)
+    {
+        if (label != nullptr)
+        {
+            labelLength = strlen(label);
+        }
+    }
+    else
+    {
+        labelLength = static_cast<size_t>(length);
+    }
+
+    if (labelLength > context->getExtensions().maxLabelLength)
+    {
+        context->handleError(
+            Error(GL_INVALID_VALUE, "Label length is larger than GL_MAX_LABEL_LENGTH."));
+        return false;
+    }
+
+    return true;
+}
+
 bool ValidateObjectLabelKHR(Context *context,
                             GLenum identifier,
                             GLuint name,
@@ -1618,11 +1644,8 @@
         return false;
     }
 
-    size_t labelLength = (length < 0) ? strlen(label) : length;
-    if (labelLength > context->getExtensions().maxLabelLength)
+    if (!ValidateLabelLength(context, length, label))
     {
-        context->handleError(
-            Error(GL_INVALID_VALUE, "Label length is larger than GL_MAX_LABEL_LENGTH."));
         return false;
     }
 
@@ -1653,8 +1676,7 @@
         return false;
     }
 
-    // Can no-op if bufSize is zero.
-    return bufSize > 0;
+    return true;
 }
 
 static bool ValidateObjectPtrName(Context *context, const void *ptr)
@@ -1684,11 +1706,8 @@
         return false;
     }
 
-    size_t labelLength = (length < 0) ? strlen(label) : length;
-    if (labelLength > context->getExtensions().maxLabelLength)
+    if (!ValidateLabelLength(context, length, label))
     {
-        context->handleError(
-            Error(GL_INVALID_VALUE, "Label length is larger than GL_MAX_LABEL_LENGTH."));
         return false;
     }
 
@@ -1718,8 +1737,7 @@
         return false;
     }
 
-    // Can no-op if bufSize is zero.
-    return bufSize > 0;
+    return true;
 }
 
 bool ValidateGetPointervKHR(Context *context, GLenum pname, void **params)