Merge "[DO NOT MERGE] Fix signedness mismatch and integer underflow" into oc-dev
am: 92e63e3f9b

Change-Id: Ia169a9fc152f100a9567f2c47ef5a2e46358aa52
diff --git a/cryptfs.cpp b/cryptfs.cpp
index 6319362..b7a5d29 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -2495,24 +2495,25 @@
 static unsigned int persist_get_max_entries(int encrypted) {
     struct crypt_mnt_ftr crypt_ftr;
     unsigned int dsize;
-    unsigned int max_persistent_entries;
 
     /* If encrypted, use the values from the crypt_ftr, otherwise
      * use the values for the current spec.
      */
     if (encrypted) {
         if (get_crypt_ftr_and_key(&crypt_ftr)) {
-            return -1;
+            /* Something is wrong, assume no space for entries */
+            return 0;
         }
         dsize = crypt_ftr.persist_data_size;
     } else {
         dsize = CRYPT_PERSIST_DATA_SIZE;
     }
 
-    max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
-        sizeof(struct crypt_persist_entry);
-
-    return max_persistent_entries;
+    if (dsize > sizeof(struct crypt_persist_data)) {
+        return (dsize - sizeof(struct crypt_persist_data)) / sizeof(struct crypt_persist_entry);
+    } else {
+        return 0;
+    }
 }
 
 static int persist_get_key(const char *fieldname, char *value)