[geary/wip/737642-uint: 1/2] Move UID/UIDVALIDITY from signed 32 -> unsigned 32



commit 35a0157e0fb545fc59faa1081fefc510b4c6b604
Author: Jim Nelson <jim yorba org>
Date:   Wed Nov 12 11:49:36 2014 -0800

    Move UID/UIDVALIDITY from signed 32 -> unsigned 32
    
    Prior code inadvertantly limited UID and UIDVALIDITY to signed, not
    unsigned, 32-bit integers.
    
    This also enforces bit width of integers from IMAP string parameters
    to avoid a similar problem in the future.

 src/engine/imap-db/imap-db-folder.vala             |    4 +-
 .../imap-engine/imap-engine-minimal-folder.vala    |   14 ++++++------
 .../imap-engine-abstract-list-email.vala           |    8 +++---
 src/engine/imap/api/imap-folder.vala               |    6 ++--
 src/engine/imap/command/imap-message-set.vala      |    2 +-
 src/engine/imap/message/imap-sequence-number.vala  |   23 ++++++++++++-------
 src/engine/imap/message/imap-uid-validity.vala     |    4 +-
 src/engine/imap/message/imap-uid.vala              |    4 +-
 .../imap/parameter/imap-string-parameter.vala      |   14 +----------
 .../imap/response/imap-fetch-data-decoder.vala     |    4 +-
 src/engine/imap/response/imap-fetched-data.vala    |    2 +-
 src/engine/imap/response/imap-response-code.vala   |    6 ++--
 src/engine/imap/response/imap-server-data.vala     |   16 +++++++-------
 src/engine/imap/response/imap-status-data.vala     |   10 ++++----
 src/engine/imap/transport/imap-client-session.vala |    2 +-
 15 files changed, 57 insertions(+), 62 deletions(-)
---
diff --git a/src/engine/imap-db/imap-db-folder.vala b/src/engine/imap-db/imap-db-folder.vala
index 07f2aaf..1629e3a 100644
--- a/src/engine/imap-db/imap-db-folder.vala
+++ b/src/engine/imap-db/imap-db-folder.vala
@@ -601,7 +601,7 @@ private class Geary.ImapDB.Folder : BaseObject, Geary.ReferenceSemantics {
     }
     
     // pos is 1-based.  This method does not respect messages marked for removal.
-    public async ImapDB.EmailIdentifier? get_id_at_async(int pos, Cancellable? cancellable) throws Error {
+    public async ImapDB.EmailIdentifier? get_id_at_async(int64 pos, Cancellable? cancellable) throws Error {
         assert(pos >= 1);
         
         ImapDB.EmailIdentifier? id = null;
@@ -615,7 +615,7 @@ private class Geary.ImapDB.Folder : BaseObject, Geary.ReferenceSemantics {
                 OFFSET ?
             """);
             stmt.bind_rowid(0, folder_id);
-            stmt.bind_int(1, pos - 1);
+            stmt.bind_int64(1, pos - 1);
             
             Db.Result results = stmt.exec(cancellable);
             if (!results.finished)
diff --git a/src/engine/imap-engine/imap-engine-minimal-folder.vala 
b/src/engine/imap-engine/imap-engine-minimal-folder.vala
index af8750f..45e49f0 100644
--- a/src/engine/imap-engine/imap-engine-minimal-folder.vala
+++ b/src/engine/imap-engine/imap-engine-minimal-folder.vala
@@ -1017,8 +1017,8 @@ private class Geary.ImapEngine.MinimalFolder : Geary.AbstractFolder, Geary.Folde
     
     // This MUST only be called from ReplayRemoval.
     internal async void do_replay_removed_message(int reported_remote_count, Imap.SequenceNumber 
remote_position) {
-        debug("%s do_replay_removed_message: current remote_count=%d remote_position=%d 
reported_remote_count=%d",
-            to_string(), remote_count, remote_position.value, reported_remote_count);
+        debug("%s do_replay_removed_message: current remote_count=%d remote_position=%s 
reported_remote_count=%d",
+            to_string(), remote_count, remote_position.value.to_string(), reported_remote_count);
         
         if (!remote_position.is_valid()) {
             debug("%s do_replay_removed_message: ignoring, invalid remote position or count",
@@ -1028,7 +1028,7 @@ private class Geary.ImapEngine.MinimalFolder : Geary.AbstractFolder, Geary.Folde
         }
         
         int local_count = -1;
-        int local_position = -1;
+        int64 local_position = -1;
         
         ImapDB.EmailIdentifier? owned_id = null;
         try {
@@ -1041,13 +1041,13 @@ private class Geary.ImapEngine.MinimalFolder : Geary.AbstractFolder, Geary.Folde
             // zero or negative means the message exists beyond the local vector's range, so
             // nothing to do there
             if (local_position > 0) {
-                debug("%s do_replay_removed_message: local_count=%d local_position=%d", to_string(),
-                    local_count, local_position);
+                debug("%s do_replay_removed_message: local_count=%d local_position=%s", to_string(),
+                    local_count, local_position.to_string());
                 
                 owned_id = yield local_folder.get_id_at_async(local_position, null);
             } else {
-                debug("%s do_replay_removed_message: message not stored locally (local_count=%d 
local_position=%d)",
-                    to_string(), local_count, local_position);
+                debug("%s do_replay_removed_message: message not stored locally (local_count=%d 
local_position=%s)",
+                    to_string(), local_count, local_position.to_string());
             }
         } catch (Error err) {
             debug("%s do_replay_removed_message: unable to determine ID of removed message %s: %s",
diff --git a/src/engine/imap-engine/replay-ops/imap-engine-abstract-list-email.vala 
b/src/engine/imap-engine/replay-ops/imap-engine-abstract-list-email.vala
index 4f1a3e2..f391ff6 100644
--- a/src/engine/imap-engine/replay-ops/imap-engine-abstract-list-email.vala
+++ b/src/engine/imap-engine/replay-ops/imap-engine-abstract-list-email.vala
@@ -252,7 +252,7 @@ private abstract class Geary.ImapEngine.AbstractListEmail : Geary.ImapEngine.Sen
                 // server to determine the position number of a particular UID, this makes sense
                 assert(high_pos != null);
                 low_pos = new Imap.SequenceNumber(
-                    Numeric.int_floor((high_pos.value - count) + 1, 1));
+                    Numeric.int64_floor((high_pos.value - count) + 1, 1));
             }
         }
         
@@ -267,7 +267,7 @@ private abstract class Geary.ImapEngine.AbstractListEmail : Geary.ImapEngine.Sen
         }
         
         Imap.MessageSet msg_set;
-        int actual_count = -1;
+        int64 actual_count = -1;
         if (high_pos != null) {
             msg_set = new Imap.MessageSet.range_by_first_last(low_pos, high_pos);
             actual_count = (high_pos.value - low_pos.value) + 1;
@@ -275,9 +275,9 @@ private abstract class Geary.ImapEngine.AbstractListEmail : Geary.ImapEngine.Sen
             msg_set = new Imap.MessageSet.range_to_highest(low_pos);
         }
         
-        debug("%s: Performing vector expansion using %s for initial_uid=%s count=%d actual_count=%d 
local_count=%d remote_count=%d oldest_to_newest=%s",
+        debug("%s: Performing vector expansion using %s for initial_uid=%s count=%d actual_count=%s 
local_count=%d remote_count=%d oldest_to_newest=%s",
             owner.to_string(), msg_set.to_string(),
-            (initial_uid != null) ? initial_uid.to_string() : "(null)", count, actual_count,
+            (initial_uid != null) ? initial_uid.to_string() : "(null)", count, actual_count.to_string(),
             local_count, remote_count, flags.is_oldest_to_newest().to_string());
         
         Gee.List<Geary.Email>? list = yield owner.remote_folder.list_email_async(msg_set,
diff --git a/src/engine/imap/api/imap-folder.vala b/src/engine/imap/api/imap-folder.vala
index 81aed1d..4441b82 100644
--- a/src/engine/imap/api/imap-folder.vala
+++ b/src/engine/imap/api/imap-folder.vala
@@ -235,10 +235,10 @@ private class Geary.Imap.Folder : BaseObject {
             recent(total);
     }
     
-    private void on_search(Gee.List<int> seq_or_uid) {
+    private void on_search(Gee.List<int64?> seq_or_uid) {
         // All SEARCH from this class are UID SEARCH, so can reliably convert and add to
         // accumulator
-        foreach (int uid in seq_or_uid)
+        foreach (int64 uid in seq_or_uid)
             search_accumulator.add(new UID(uid));
     }
     
@@ -1002,7 +1002,7 @@ private class Geary.Imap.Folder : BaseObject {
         StatusResponse response = responses.get(cmd);
         if (response.status == Status.OK && response.response_code != null &&
             response.response_code.get_response_code_type().is_value("appenduid")) {
-            UID new_id = new UID(response.response_code.get_as_string(2).as_int());
+            UID new_id = new UID(response.response_code.get_as_string(2).as_int64());
             
             return new ImapDB.EmailIdentifier.no_message_id(new_id);
         }
diff --git a/src/engine/imap/command/imap-message-set.vala b/src/engine/imap/command/imap-message-set.vala
index 21497e2..85af92a 100644
--- a/src/engine/imap/command/imap-message-set.vala
+++ b/src/engine/imap/command/imap-message-set.vala
@@ -47,7 +47,7 @@ public class Geary.Imap.MessageSet : BaseObject {
         assert(count > 0);
         
         value = (count > 1)
-            ? "%d:%d".printf(low_seq_num.value, low_seq_num.value + count - 1)
+            ? "%s:%s".printf(low_seq_num.value.to_string(), (low_seq_num.value + count - 1).to_string())
             : low_seq_num.serialize();
     }
     
diff --git a/src/engine/imap/message/imap-sequence-number.vala 
b/src/engine/imap/message/imap-sequence-number.vala
index c71b7a8..82cc392 100644
--- a/src/engine/imap/message/imap-sequence-number.vala
+++ b/src/engine/imap/message/imap-sequence-number.vala
@@ -12,21 +12,26 @@
  * @see UID
  */
 
-public class Geary.Imap.SequenceNumber : Geary.MessageData.IntMessageData, Geary.Imap.MessageData,
+public class Geary.Imap.SequenceNumber : Geary.MessageData.Int64MessageData, Geary.Imap.MessageData,
     Gee.Comparable<SequenceNumber> {
     /**
      * Minimum value of a valid { link SequenceNumber}.
      *
      * See [[http://tools.ietf.org/html/rfc3501#section-2.3.1.2]]
      */
-    public int MIN_VALUE = 1;
+    public static int64 MIN = 1;
+    
+    /**
+     * 32-bit upper limit of a valid { link SequenceNumber}.
+     */
+    public static int64 MAX = uint32.MAX;
     
     /**
      * Create a new { link SequenceNumber}.
      *
      * This does not check if the value is valid, i.e. >= { link MIN_VALUE}.
      */
-    public SequenceNumber(int value) {
+    public SequenceNumber(int64 value) {
         base (value);
     }
     
@@ -42,10 +47,10 @@ public class Geary.Imap.SequenceNumber : Geary.MessageData.IntMessageData, Geary
     }
     
     /**
-     * Defined as { link value} >= { link MIN_VALUE}.
+     * Defined as { link value} >= { link MIN} and <= { link MAX}.
      */
     public bool is_valid() {
-        return value >= MIN_VALUE;
+        return value >= MIN && value <= MAX;
     }
     
     /**
@@ -62,16 +67,16 @@ public class Geary.Imap.SequenceNumber : Geary.MessageData.IntMessageData, Geary
      * Returns null if the decremented value is less than { link MIN_VALUE}.
      */
     public SequenceNumber? dec() {
-        return (value > MIN_VALUE) ? new SequenceNumber(value - 1) : null;
+        return (value > MIN) ? new SequenceNumber(value - 1) : null;
     }
     
     /**
      * Returns a new { link SequenceNumber} that is one lower than this value.
      *
-     * Returns a SequenceNumber of MIN_VALUE if the decremented value is less than it.
+     * Returns a SequenceNumber of MIN if the decremented value is less than it.
      */
     public SequenceNumber dec_clamped() {
-        return (value > MIN_VALUE) ? new SequenceNumber(value - 1) : new SequenceNumber(MIN_VALUE);
+        return (value > MIN) ? new SequenceNumber(value - 1) : new SequenceNumber(MIN);
     }
     
     /**
@@ -100,7 +105,7 @@ public class Geary.Imap.SequenceNumber : Geary.MessageData.IntMessageData, Geary
     }
     
     public virtual int compare_to(SequenceNumber other) {
-        return value - other.value;
+        return (int) (value - other.value).clamp(-1, 1);
     }
     
     public string serialize() {
diff --git a/src/engine/imap/message/imap-uid-validity.vala b/src/engine/imap/message/imap-uid-validity.vala
index fe6875a..f1ed27d 100644
--- a/src/engine/imap/message/imap-uid-validity.vala
+++ b/src/engine/imap/message/imap-uid-validity.vala
@@ -13,9 +13,9 @@
  */
 
 public class Geary.Imap.UIDValidity : Geary.MessageData.Int64MessageData, Geary.Imap.MessageData {
-    // Using statics because int32.MAX is static, not const (??)
+    // Using statics because uint32.MAX is static, not const (??)
     public static int64 MIN = 1;
-    public static int64 MAX = int32.MAX;
+    public static int64 MAX = uint32.MAX;
     public static int64 INVALID = -1;
     
     public UIDValidity(int64 value) {
diff --git a/src/engine/imap/message/imap-uid.vala b/src/engine/imap/message/imap-uid.vala
index 1cd57bf..0fc5199 100644
--- a/src/engine/imap/message/imap-uid.vala
+++ b/src/engine/imap/message/imap-uid.vala
@@ -14,9 +14,9 @@
 
 public class Geary.Imap.UID : Geary.MessageData.Int64MessageData, Geary.Imap.MessageData,
     Gee.Comparable<Geary.Imap.UID> {
-    // Using statics because int32.MAX is static, not const (??)
+    // Using statics because uint32.MAX is static, not const (??)
     public static int64 MIN = 1;
-    public static int64 MAX = int32.MAX;
+    public static int64 MAX = uint32.MAX;
     public static int64 INVALID = -1;
     
     public UID(int64 value) {
diff --git a/src/engine/imap/parameter/imap-string-parameter.vala 
b/src/engine/imap/parameter/imap-string-parameter.vala
index 0a164f5..b89d8e9 100644
--- a/src/engine/imap/parameter/imap-string-parameter.vala
+++ b/src/engine/imap/parameter/imap-string-parameter.vala
@@ -169,18 +169,8 @@ public abstract class Geary.Imap.StringParameter : Geary.Imap.Parameter {
      * TODO: This does not check that the value is a properly-formed integer.  This should be
      *. added later.
      */
-    public int as_int(int clamp_min = int.MIN, int clamp_max = int.MAX) throws ImapError {
-        return int.parse(ascii).clamp(clamp_min, clamp_max);
-    }
-    
-    /**
-     * Converts the { link value} to a long integer, clamped between clamp_min and clamp_max.
-     *
-     * TODO: This does not check that the value is a properly-formed long integer.  This should be
-     *. added later.
-     */
-    public long as_long(int clamp_min = int.MIN, int clamp_max = int.MAX) throws ImapError {
-        return long.parse(ascii).clamp(clamp_min, clamp_max);
+    public int32 as_int32(int32 clamp_min = int32.MIN, int32 clamp_max = int32.MAX) throws ImapError {
+        return (int32) int64.parse(ascii).clamp(clamp_min, clamp_max);
     }
     
     /**
diff --git a/src/engine/imap/response/imap-fetch-data-decoder.vala 
b/src/engine/imap/response/imap-fetch-data-decoder.vala
index 142a2a3..99f3676 100644
--- a/src/engine/imap/response/imap-fetch-data-decoder.vala
+++ b/src/engine/imap/response/imap-fetch-data-decoder.vala
@@ -85,7 +85,7 @@ public class Geary.Imap.UIDDecoder : Geary.Imap.FetchDataDecoder {
     }
     
     protected override MessageData decode_string(StringParameter stringp) throws ImapError {
-        return new UID(stringp.as_int());
+        return new UID(stringp.as_int64());
     }
 }
 
@@ -119,7 +119,7 @@ public class Geary.Imap.RFC822SizeDecoder : Geary.Imap.FetchDataDecoder {
     }
     
     protected override MessageData decode_string(StringParameter stringp) throws ImapError {
-        return new RFC822Size(stringp.as_long());
+        return new RFC822Size(stringp.as_int32());
     }
 }
 
diff --git a/src/engine/imap/response/imap-fetched-data.vala b/src/engine/imap/response/imap-fetched-data.vala
index a759b85..f07455f 100644
--- a/src/engine/imap/response/imap-fetched-data.vala
+++ b/src/engine/imap/response/imap-fetched-data.vala
@@ -49,7 +49,7 @@ public class Geary.Imap.FetchedData : Object {
         if (!server_data.get_as_string(2).equals_ci(FetchCommand.NAME))
             throw new ImapError.PARSE_ERROR("Not FETCH data: %s", server_data.to_string());
         
-        FetchedData fetched_data = new FetchedData(new 
SequenceNumber(server_data.get_as_string(1).as_int()));
+        FetchedData fetched_data = new FetchedData(new 
SequenceNumber(server_data.get_as_string(1).as_int64()));
         
         // walk the list for each returned fetch data item, which is paired by its data item name
         // and the structured data itself
diff --git a/src/engine/imap/response/imap-response-code.vala 
b/src/engine/imap/response/imap-response-code.vala
index f76224e..a6bb7b6 100644
--- a/src/engine/imap/response/imap-response-code.vala
+++ b/src/engine/imap/response/imap-response-code.vala
@@ -27,7 +27,7 @@ public class Geary.Imap.ResponseCode : Geary.Imap.ListParameter {
         if (!get_response_code_type().is_value(ResponseCodeType.UIDNEXT))
             throw new ImapError.INVALID("Not UIDNEXT: %s", to_string());
         
-        return new UID(get_as_string(1).as_int());
+        return new UID(get_as_string(1).as_int64());
     }
     
     /**
@@ -39,7 +39,7 @@ public class Geary.Imap.ResponseCode : Geary.Imap.ListParameter {
         if (!get_response_code_type().is_value(ResponseCodeType.UIDVALIDITY))
             throw new ImapError.INVALID("Not UIDVALIDITY: %s", to_string());
         
-        return new UIDValidity(get_as_string(1).as_int());
+        return new UIDValidity(get_as_string(1).as_int64());
     }
     
     /**
@@ -51,7 +51,7 @@ public class Geary.Imap.ResponseCode : Geary.Imap.ListParameter {
         if (!get_response_code_type().is_value(ResponseCodeType.UNSEEN))
             throw new ImapError.INVALID("Not UNSEEN: %s", to_string());
         
-        return get_as_string(1).as_int(0, int.MAX);
+        return get_as_string(1).as_int32(0, int.MAX);
     }
     
     /**
diff --git a/src/engine/imap/response/imap-server-data.vala b/src/engine/imap/response/imap-server-data.vala
index 81c3ef0..076c42f 100644
--- a/src/engine/imap/response/imap-server-data.vala
+++ b/src/engine/imap/response/imap-server-data.vala
@@ -79,7 +79,7 @@ public class Geary.Imap.ServerData : ServerResponse {
         if (server_data_type != ServerDataType.EXISTS)
             throw new ImapError.INVALID("Not EXISTS data: %s", to_string());
         
-        return get_as_string(1).as_int(0);
+        return get_as_string(1).as_int32(0);
     }
     
     /**
@@ -91,7 +91,7 @@ public class Geary.Imap.ServerData : ServerResponse {
         if (server_data_type != ServerDataType.EXPUNGE)
             throw new ImapError.INVALID("Not EXPUNGE data: %s", to_string());
         
-        return new SequenceNumber(get_as_string(1).as_int());
+        return new SequenceNumber(get_as_string(1).as_int64());
     }
     
     /**
@@ -139,24 +139,24 @@ public class Geary.Imap.ServerData : ServerResponse {
         if (server_data_type != ServerDataType.RECENT)
             throw new ImapError.INVALID("Not RECENT data: %s", to_string());
         
-        return get_as_string(1).as_int(0);
+        return get_as_string(1).as_int32(0);
     }
     
     /**
-     * Parses the { link ServerData} into a { link ServerDataType.RECENT} value, if possible.
+     * Parses the { link ServerData} into a { link ServerDataType.SEARCH} value, if possible.
      *
-     * @throws ImapError.INVALID if not a { link ServerDataType.RECENT} value.
+     * @throws ImapError.INVALID if not a { link ServerDataType.SEARCH} value.
      */
-    public Gee.List<int> get_search() throws ImapError {
+    public Gee.List<int64?> get_search() throws ImapError {
         if (server_data_type != ServerDataType.SEARCH)
             throw new ImapError.INVALID("Not SEARCH data: %s", to_string());
         
-        Gee.List<int> results = new Gee.ArrayList<int>();
+        Gee.List<int64?> results = new Gee.ArrayList<int64?>();
         for (int ctr = 2; ctr < size; ctr++) {
             // can't directly return the result from as_int() into results as a Vala bug causes a
             // build policy violation for uncast int -> pointer on 64-bit architectures:
             // https://bugzilla.gnome.org/show_bug.cgi?id=720437
-            int result = get_as_string(ctr).as_int(0);
+            int64 result = get_as_string(ctr).as_int64(0);
             results.add(result);
         }
         
diff --git a/src/engine/imap/response/imap-status-data.vala b/src/engine/imap/response/imap-status-data.vala
index 56724c2..ea0c40a 100644
--- a/src/engine/imap/response/imap-status-data.vala
+++ b/src/engine/imap/response/imap-status-data.vala
@@ -93,25 +93,25 @@ public class Geary.Imap.StatusData : Object {
                 switch (StatusDataType.from_parameter(typep)) {
                     case StatusDataType.MESSAGES:
                         // see note at UNSET
-                        messages = valuep.as_int(-1, int.MAX);
+                        messages = valuep.as_int32(-1, int.MAX);
                     break;
                     
                     case StatusDataType.RECENT:
                         // see note at UNSET
-                        recent = valuep.as_int(-1, int.MAX);
+                        recent = valuep.as_int32(-1, int.MAX);
                     break;
                     
                     case StatusDataType.UIDNEXT:
-                        uid_next = new UID(valuep.as_int());
+                        uid_next = new UID(valuep.as_int64());
                     break;
                     
                     case StatusDataType.UIDVALIDITY:
-                        uid_validity = new UIDValidity(valuep.as_int());
+                        uid_validity = new UIDValidity(valuep.as_int64());
                     break;
                     
                     case StatusDataType.UNSEEN:
                         // see note at UNSET
-                        unseen = valuep.as_int(-1, int.MAX);
+                        unseen = valuep.as_int32(-1, int.MAX);
                     break;
                     
                     default:
diff --git a/src/engine/imap/transport/imap-client-session.vala 
b/src/engine/imap/transport/imap-client-session.vala
index c157588..e1549c4 100644
--- a/src/engine/imap/transport/imap-client-session.vala
+++ b/src/engine/imap/transport/imap-client-session.vala
@@ -221,7 +221,7 @@ public class Geary.Imap.ClientSession : BaseObject {
     
     public signal void recent(int count);
     
-    public signal void search(Gee.List<int> seq_or_uid);
+    public signal void search(Gee.List<int64?> seq_or_uid);
     
     public signal void status(StatusData status_data);
     


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]