Placate -Wconversion for sort ordinals.

Add some casts, but generally hold off on redundant checks.  (Aside from
one doc_id < 0 check.)  The primary safety mechanism is that
implementations of SortCache_Value, which take an ord, perform safe
InStream reads and will throw errors properly upon being handed
out-of-range ords.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/304d5cfd
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/304d5cfd
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/304d5cfd

Branch: refs/heads/master
Commit: 304d5cfd36983eeb7d1d4ccb54154fd1039b5043
Parents: f391174
Author: Marvin Humphrey <mar...@rectangular.com>
Authored: Fri May 6 17:56:04 2016 -0700
Committer: Marvin Humphrey <mar...@rectangular.com>
Committed: Fri May 6 18:28:21 2016 -0700

----------------------------------------------------------------------
 core/Lucy/Index/SortCache.c               | 12 ++++++------
 core/Lucy/Index/SortCache/TextSortCache.c |  2 +-
 core/Lucy/Index/SortFieldWriter.c         | 10 +++++-----
 3 files changed, 12 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/304d5cfd/core/Lucy/Index/SortCache.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Index/SortCache.c b/core/Lucy/Index/SortCache.c
index cbfe8ea..c4e4c00 100644
--- a/core/Lucy/Index/SortCache.c
+++ b/core/Lucy/Index/SortCache.c
@@ -67,8 +67,8 @@ SortCache_Set_Native_Ords_IMP(SortCache *self, bool 
native_ords) {
 int32_t
 SortCache_Ordinal_IMP(SortCache *self, int32_t doc_id) {
     SortCacheIVARS *const ivars = SortCache_IVARS(self);
-    if ((uint32_t)doc_id > (uint32_t)ivars->doc_max) {
-        THROW(ERR, "Out of range: %i32 > %i32", doc_id, ivars->doc_max);
+    if (doc_id > ivars->doc_max || doc_id < 0) {
+        THROW(ERR, "Out of range: %i32 max: %i32", doc_id, ivars->doc_max);
     }
     switch (ivars->ord_width) {
         case 1: return NumUtil_u1get(ivars->ords, (uint32_t)doc_id);
@@ -85,18 +85,18 @@ SortCache_Ordinal_IMP(SortCache *self, int32_t doc_id) {
             }
             else {
                 uint8_t *bytes = (uint8_t*)ivars->ords;
-                bytes += doc_id * sizeof(uint16_t);
+                bytes += (size_t)doc_id * sizeof(uint16_t);
                 return NumUtil_decode_bigend_u16(bytes);
             }
         case 32:
             if (ivars->native_ords) {
-                uint32_t *ints = (uint32_t*)ivars->ords;
+                int32_t *ints = (int32_t*)ivars->ords;
                 return ints[doc_id];
             }
             else {
                 uint8_t *bytes = (uint8_t*)ivars->ords;
-                bytes += doc_id * sizeof(uint32_t);
-                return NumUtil_decode_bigend_u32(bytes);
+                bytes += (size_t)doc_id * sizeof(int32_t);
+                return (int32_t)NumUtil_decode_bigend_u32(bytes);
             }
         default: {
                 THROW(ERR, "Invalid ord width: %i32", ivars->ord_width);

http://git-wip-us.apache.org/repos/asf/lucy/blob/304d5cfd/core/Lucy/Index/SortCache/TextSortCache.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Index/SortCache/TextSortCache.c 
b/core/Lucy/Index/SortCache/TextSortCache.c
index b58f0f1..6c5dfb9 100644
--- a/core/Lucy/Index/SortCache/TextSortCache.c
+++ b/core/Lucy/Index/SortCache/TextSortCache.c
@@ -100,7 +100,7 @@ TextSortCache_Value_IMP(TextSortCache *self, int32_t ord) {
         return NULL;
     }
     else {
-        uint32_t next_ord = ord + 1;
+        int32_t next_ord = ord + 1;
         int64_t next_offset;
         while (1) {
             InStream_Seek(ivars->ix_in, next_ord * (int64_t)sizeof(int64_t));

http://git-wip-us.apache.org/repos/asf/lucy/blob/304d5cfd/core/Lucy/Index/SortFieldWriter.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Index/SortFieldWriter.c 
b/core/Lucy/Index/SortFieldWriter.c
index a300f1d..af6d093 100644
--- a/core/Lucy/Index/SortFieldWriter.c
+++ b/core/Lucy/Index/SortFieldWriter.c
@@ -234,19 +234,19 @@ S_write_ord(void *ords, int32_t width, int32_t doc_id, 
int32_t ord) {
             break;
         case 8: {
                 uint8_t *ints = (uint8_t*)ords;
-                ints[doc_id] = ord;
+                ints[doc_id] = (uint8_t)ord;
             }
             break;
         case 16: {
                 uint8_t *bytes = (uint8_t*)ords;
-                bytes += doc_id * sizeof(uint16_t);
-                NumUtil_encode_bigend_u16(ord, &bytes);
+                bytes += (size_t)doc_id * sizeof(uint16_t);
+                NumUtil_encode_bigend_u16((uint16_t)ord, &bytes);
             }
             break;
         case 32: {
                 uint8_t *bytes = (uint8_t*)ords;
-                bytes += doc_id * sizeof(uint32_t);
-                NumUtil_encode_bigend_u32(ord, &bytes);
+                bytes += (size_t)doc_id * sizeof(uint32_t);
+                NumUtil_encode_bigend_u32((uint32_t)ord, &bytes);
             }
             break;
         default:

Reply via email to