Browse Source

re-factor crypt_list_all_constants()

Steffen Jaeckel 7 years ago
parent
commit
8bc889cd64
1 changed files with 8 additions and 17 deletions
  1. 8 17
      src/misc/crypt/crypt_constants.c

+ 8 - 17
src/misc/crypt/crypt_constants.c

@@ -258,20 +258,16 @@ int crypt_get_constant(const char* namein, int *valueout) {
 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
     int i;
     int i;
     unsigned int total_len = 0;
     unsigned int total_len = 0;
-    char number[32], *ptr;
+    char *ptr;
     int number_len;
     int number_len;
     int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
     int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
 
 
     /* calculate amount of memory required for the list */
     /* calculate amount of memory required for the list */
     for (i=0; i<count; i++) {
     for (i=0; i<count; i++) {
-        total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1;
-        /* the above +1 is for the commas */
-        number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
-        if ((number_len < 0) ||
-            ((unsigned int)number_len >= sizeof(number)))
+        number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
+        if (number_len < 0)
           return -1;
           return -1;
-        total_len += number_len + 1;
-        /* this last +1 is for newlines (and ending NULL) */
+        total_len += number_len;
     }
     }
 
 
     if (names_list == NULL) {
     if (names_list == NULL) {
@@ -283,16 +279,11 @@ int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
         /* build the names list */
         /* build the names list */
         ptr = names_list;
         ptr = names_list;
         for (i=0; i<count; i++) {
         for (i=0; i<count; i++) {
-            strcpy(ptr, _crypt_constants[i].name);
-            ptr += strlen(_crypt_constants[i].name);
-            strcpy(ptr, ",");
-            ptr += 1;
-
-            number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
-            strcpy(ptr, number);
+            number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
+            if (number_len < 0) return -1;
+            if ((unsigned int)number_len > total_len) return -1;
+            total_len -= number_len;
             ptr += number_len;
             ptr += number_len;
-            strcpy(ptr, "\n");
-            ptr += 1;
         }
         }
         /* to remove the trailing new-line */
         /* to remove the trailing new-line */
         ptr -= 1;
         ptr -= 1;