value: Make sure to cast int range values to guints before storing them
authorSebastian Dröge <sebastian@centricular.com>
Thu, 19 Jun 2014 07:23:56 +0000 (09:23 +0200)
committerSebastian Dröge <sebastian@centricular.com>
Thu, 19 Jun 2014 07:33:54 +0000 (09:33 +0200)
Otherwise negative values will sets all of the 64 bits due to two's
complement's definition of negative values.

Also add a test for negative int ranges.

gst/gstvalue.c
tests/check/gst/gstvalue.c

index 85d3a11c91fa9d8549268d4fade987d9826f2c62..ca6ce7d997dfbff4078146ac2178c093951465f4 100644 (file)
@@ -980,8 +980,8 @@ gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
   g_return_if_fail (start % step == 0);
   g_return_if_fail (end % step == 0);
 
-  sstart = (guint64) (start / step);
-  sstop = (guint64) (end / step);
+  sstart = (guint) (start / step);
+  sstop = (guint) (end / step);
   value->data[0].v_uint64 = (sstart << 32) | sstop;
   value->data[1].v_int = step;
 }
@@ -3331,8 +3331,9 @@ gst_value_union_int_int_range (GValue * dest, const GValue * src1,
   /* check if it extends the range */
   if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
     if (dest) {
-      guint64 new_min = (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2);
-      guint64 new_max = INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2);
+      guint64 new_min =
+          (guint) ((INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2));
+      guint64 new_max = (guint) (INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
 
       gst_value_init_and_copy (dest, src2);
       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
@@ -3341,8 +3342,9 @@ gst_value_union_int_int_range (GValue * dest, const GValue * src1,
   }
   if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
     if (dest) {
-      guint64 new_min = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
-      guint64 new_max = (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2);
+      guint64 new_min = (guint) (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
+      guint64 new_max =
+          (guint) ((INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2));
 
       gst_value_init_and_copy (dest, src2);
       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
@@ -3411,10 +3413,11 @@ gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
       if (scalar ==
           (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
         if (dest) {
-          guint64 new_min =
-              (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value);
-          guint64 new_max =
-              INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value);
+          guint64 new_min = (guint)
+              ((INT_RANGE_MIN (range_value) -
+                  1) * INT_RANGE_STEP (range_value));
+          guint64 new_max = (guint)
+              (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
 
           gst_value_init_and_copy (dest, range_value);
           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
@@ -3423,10 +3426,11 @@ gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
       } else if (scalar ==
           (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
         if (dest) {
-          guint64 new_min =
-              INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value);
-          guint64 new_max =
-              (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value);
+          guint64 new_min = (guint)
+              (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
+          guint64 new_max = (guint)
+              ((INT_RANGE_MAX (range_value) +
+                  1) * INT_RANGE_STEP (range_value));
           gst_value_init_and_copy (dest, range_value);
           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
         }
index b4cd2d2b1890b505bab87bf3ae26660a255f1f8e..9a0df42709bb3c61b04d3c0ded94664633780994 100644 (file)
@@ -2505,6 +2505,13 @@ GST_START_TEST (test_int_range)
   g_value_unset (&dest);
   fail_unless (gst_value_intersect (&dest, &range, &range2) == FALSE);
 
+  gst_value_set_int_range (&range, -7, -6);
+  fail_unless_equals_int (gst_value_get_int_range_min (&range), -7);
+  fail_unless_equals_int (gst_value_get_int_range_max (&range), -6);
+  gst_value_set_int_range (&range, -7, 7);
+  fail_unless_equals_int (gst_value_get_int_range_min (&range), -7);
+  fail_unless_equals_int (gst_value_get_int_range_max (&range), 7);
+
   g_value_unset (&start);
   g_value_unset (&end);
   g_value_unset (&range);