]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/alloc_ttype.c
ncurses 6.5 - patch 20240525
[ncurses.git] / ncurses / tinfo / alloc_ttype.c
index eef8170cf21c6ebc4ddb7431b7aebfd39cb493e8..304c1b62f4e691e39675f021bb8d167256c101ea 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright 2018-2021,2022 Thomas E. Dickey                                *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
  * Copyright 1999-2016,2017 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
@@ -43,7 +43,7 @@
 
 #include <tic.h>
 
-MODULE_ID("$Id: alloc_ttype.c,v 1.37 2022/04/30 17:00:05 tom Exp $")
+MODULE_ID("$Id: alloc_ttype.c,v 1.51 2023/09/09 23:15:53 tom Exp $")
 
 #if NCURSES_XNAMES
 /*
@@ -378,8 +378,8 @@ adjust_cancels(TERMTYPE2 *to, TERMTYPE2 *from)
     int j, k;
 
     DEBUG(3, (T_CALLED("adjust_cancels(%s), from(%s)"),
-             to ? NonNull(to->term_names) : "?",
-             from ? NonNull(from->term_names) : "?"));
+             NonNull(to->term_names),
+             NonNull(from->term_names)));
     for (j = first; j < last;) {
        char *name = to->ext_Names[j];
        int j_str = to->num_Strings - first - to->ext_Strings;
@@ -429,7 +429,7 @@ _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
     na = to ? ((int) NUM_EXT_NAMES(to)) : 0;
     nb = from ? ((int) NUM_EXT_NAMES(from)) : 0;
 
-    DEBUG(2, (T_CALLED("align_termtype to(%d:%s), from(%d:%s)"),
+    DEBUG(2, (T_CALLED("_nc_align_termtype to(%d:%s), from(%d:%s)"),
              na, to ? NonNull(to->term_names) : "?",
              nb, from ? NonNull(from->term_names) : "?"));
 
@@ -450,8 +450,10 @@ _nc_align_termtype(TERMTYPE2 *to, TERMTYPE2 *from)
                    break;
                }
            }
-           if (same)
+           if (same) {
+               DEBUG(2, (T_RETURN("")));
                return;
+           }
        }
        /*
         * This is where we pay for having a simple extension representation.
@@ -528,12 +530,14 @@ copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
     unsigned i;
     int pass;
     char *new_table;
+    size_t new_table_size;
 #if NCURSES_EXT_NUMBERS
     short *oldptr = 0;
     int *newptr = 0;
 #endif
 
-    DEBUG(2, (T_CALLED("copy_termtype(dst=%p, src=%p, mode=%d)"), dst, src, mode));
+    DEBUG(2, (T_CALLED("copy_termtype(dst=%p, src=%p, mode=%d)"), (void *)
+             dst, (const void *) src, mode));
     *dst = *src;               /* ...to copy the sizes and string-tables */
 
     TYPE_MALLOC(NCURSES_SBOOL, NUM_BOOLEANS(dst), dst->Booleans);
@@ -547,12 +551,24 @@ copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
           NUM_STRINGS(dst) * sizeof(dst->Strings[0]));
 
     new_table = NULL;
+    new_table_size = 0;
     for (pass = 0; pass < 2; ++pass) {
        size_t str_size = 0;
-       for (i = 0; i < NUM_STRINGS(dst); ++i) {
+       if (src->term_names != NULL) {
+           if (pass) {
+               dst->term_names = new_table + str_size;
+               _nc_STRCPY(dst->term_names + str_size,
+                          src->term_names,
+                          new_table_size - str_size);
+           }
+           str_size += strlen(src->term_names) + 1;
+       }
+       for_each_string(i, src) {
            if (VALID_STRING(src->Strings[i])) {
                if (pass) {
-                   strcpy(new_table + str_size, src->Strings[i]);
+                   _nc_STRCPY(new_table + str_size,
+                              src->Strings[i],
+                              new_table_size - str_size);
                    dst->Strings[i] = new_table + str_size;
                }
                str_size += strlen(src->Strings[i]) + 1;
@@ -562,7 +578,9 @@ copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
            dst->str_table = new_table;
        } else {
            ++str_size;
-           new_table = malloc(str_size);
+           if ((new_table = malloc(str_size)) == NULL)
+               _nc_err_abort(MSG_NO_MEMORY);
+           new_table_size = str_size;
        }
     }
 
@@ -615,12 +633,30 @@ copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
        memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
 
        new_table = NULL;
+       new_table_size = 0;
        for (pass = 0; pass < 2; ++pass) {
            size_t str_size = 0;
+           char *raw_data = src->ext_str_table;
+           if (raw_data != NULL) {
+               for (i = 0; i < src->ext_Strings; ++i) {
+                   size_t skip = strlen(raw_data) + 1;
+                   if (skip != 1) {
+                       if (pass) {
+                           _nc_STRCPY(new_table + str_size,
+                                      raw_data,
+                                      new_table_size - str_size);
+                       }
+                       str_size += skip;
+                       raw_data += skip;
+                   }
+               }
+           }
            for (i = 0; i < NUM_EXT_NAMES(dst); ++i) {
                if (VALID_STRING(src->ext_Names[i])) {
                    if (pass) {
-                       strcpy(new_table + str_size, src->ext_Names[i]);
+                       _nc_STRCPY(new_table + str_size,
+                                  src->ext_Names[i],
+                                  new_table_size - str_size);
                        dst->ext_Names[i] = new_table + str_size;
                    }
                    str_size += strlen(src->ext_Names[i]) + 1;
@@ -630,23 +666,24 @@ copy_termtype(TERMTYPE2 *dst, const TERMTYPE2 *src, int mode)
                dst->ext_str_table = new_table;
            } else {
                ++str_size;
-               new_table = malloc(str_size);
+               if ((new_table = calloc(str_size, 1)) == NULL)
+                   _nc_err_abort(MSG_NO_MEMORY);
+               new_table_size = str_size;
            }
        }
     } else {
        dst->ext_Names = 0;
     }
 #endif
+    (void) new_table_size;
     DEBUG(2, (T_RETURN("")));
 }
 
-/*
- * This entrypoint is used by tack 1.07
- */
 NCURSES_EXPORT(void)
 _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
 {
-    DEBUG(2, (T_CALLED("_nc_copy_termtype(dst=%p, src=%p)"), dst, src));
+    DEBUG(2, (T_CALLED("_nc_copy_termtype(dst=%p, src=%p)"), (void *) dst,
+             (const void *) src));
     copy_termtype((TERMTYPE2 *) dst, (const TERMTYPE2 *) src, 0);
     DEBUG(2, (T_RETURN("")));
 }
@@ -655,7 +692,8 @@ _nc_copy_termtype(TERMTYPE *dst, const TERMTYPE *src)
 NCURSES_EXPORT(void)
 _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
 {
-    DEBUG(2, (T_CALLED("_nc_copy_termtype2(dst=%p, src=%p)"), dst, src));
+    DEBUG(2, (T_CALLED("_nc_copy_termtype2(dst=%p, src=%p)"), (void *) dst,
+             (const void *) src));
     copy_termtype(dst, src, srcINT | dstINT);
     DEBUG(2, (T_RETURN("")));
 }
@@ -667,7 +705,8 @@ _nc_copy_termtype2(TERMTYPE2 *dst, const TERMTYPE2 *src)
 NCURSES_EXPORT(void)
 _nc_export_termtype2(TERMTYPE *dst, const TERMTYPE2 *src)
 {
-    DEBUG(2, (T_CALLED("_nc_export_termtype2(dst=%p, src=%p)"), dst, src));
+    DEBUG(2, (T_CALLED("_nc_export_termtype2(dst=%p, src=%p)"), (void *)
+             dst, (const void *) src));
     copy_termtype((TERMTYPE2 *) dst, src, srcINT);
     DEBUG(2, (T_RETURN("")));
 }