]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/array.c
utf8ify_rfc822_string() is in libcitadel now
[citadel.git] / libcitadel / lib / array.c
index a8adb292d84ba950fbfabd59a3146f97637622ee..daed5a3aa1b9c897982816debe5cfa0025c97c0e 100644 (file)
@@ -5,19 +5,8 @@
  *
  * Copyright (c) 2021 by Art Cancro
  *
- * This program is open source software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is open source software.  Use, duplication, or disclosure
+ * is subject to the terms of the GNU General Public License, version 3.
  */
 
 
@@ -61,12 +50,18 @@ void array_append(Array *arr, void *new_element) {
                arr->num_alloc = 1;
                arr->num_elements = 0;
                arr->the_elements = malloc(arr->element_size * arr->num_alloc);
+               if (arr->the_elements == NULL) {
+                       abort();
+               }
        }
 
        ++arr->num_elements;
        if (arr->num_elements > arr->num_alloc) {
                arr->num_alloc = arr->num_alloc * 2;            // whenever we exceed the buffer size, we double it.
                arr->the_elements = realloc(arr->the_elements, (arr->element_size * arr->num_alloc));
+               if (arr->the_elements == NULL) {
+                       abort();
+               }
        }
 
        memcpy((arr->the_elements + ( (arr->num_elements-1) * arr->element_size )), new_element, arr->element_size);
@@ -107,6 +102,10 @@ void array_delete_element_at(Array *arr, int index) {
                return;
        }
 
+       if (index < 0) {                                // If the supplied index is invalid, return quietly.
+               return;
+       }
+
        --arr->num_elements;
 
        if (index == arr->num_elements) {               // If we deleted the element at the end, there's no more to be done.