bug-texinfo
[Top][All Lists]
Advanced

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

Re: segfault with 2 similar index entries and 2 similar printindex


From: Karl Berry
Subject: Re: segfault with 2 similar index entries and 2 similar printindex
Date: Sat, 10 May 2003 12:10:17 -0400

    Subject: segfault with 2 similar index entries and 2 similar printindex

Thanks for the report.  Here's a patch, such as it is.  I'm not entirely
happy with it, but it seems to get the job done for now.

k


*** index.c     16 Feb 2003 17:30:39 -0000      1.6
--- index.c     10 May 2003 15:54:39 -0000
***************
*** 556,571 ****
    free (copy);
  }
  
! /* Sort the index passed in INDEX, returning an array of
!    pointers to elements.  The array is terminated with a NULL
!    pointer.  We call qsort because it's supposed to be fast.
!    I think this looks bad. */
  INDEX_ELT **
  sort_index (index)
       INDEX_ELT *index;
  {
    INDEX_ELT **array;
!   INDEX_ELT *temp = index;
    int count = 0;
    int save_line_number = line_number;
    char *save_input_filename = input_filename;
--- 556,571 ----
    free (copy);
  }
  
! 
! /* Sort the index passed in INDEX, returning an array of pointers to
!    elements.  The array is terminated with a NULL pointer.  */
! 
  INDEX_ELT **
  sort_index (index)
       INDEX_ELT *index;
  {
    INDEX_ELT **array;
!   INDEX_ELT *temp;
    int count = 0;
    int save_line_number = line_number;
    char *save_input_filename = input_filename;
***************
*** 578,612 ****
       characters @AE{} etc., to sort incorrectly.  */
    html = 0;
  
!   while (temp)
!     {
!       count++;
!       temp = temp->next;
!     }
! 
!   /* We have the length.  Make an array. */
! 
    array = xmalloc ((count + 1) * sizeof (INDEX_ELT *));
-   count = 0;
-   temp = index;
  
!   while (temp)
      {
!       array[count++] = temp;
  
        /* Set line number and input filename to the source line for this
           index entry, as this expansion finds any errors.  */
!       line_number = array[count - 1]->defining_line;
!       input_filename = array[count - 1]->defining_file;
  
        /* If this particular entry should be printed as a "code" index,
!          then expand it as @code{entry}, i.e. as in fixed-width font.  */
!       array[count-1]->entry = expansion (temp->entry_text,
!                                          array[count-1]->code);
! 
!       temp = temp->next;
      }
    array[count] = NULL;    /* terminate the array. */
    line_number = save_line_number;
    input_filename = save_input_filename;
    html = save_html;
--- 578,613 ----
       characters @AE{} etc., to sort incorrectly.  */
    html = 0;
  
!   for (temp = index, count = 0; temp; temp = temp->next, count++)
!     ;
!   /* We have the length, now we can allocate an array. */
    array = xmalloc ((count + 1) * sizeof (INDEX_ELT *));
  
!   for (temp = index, count = 0; temp; temp = temp->next, count++)
      {
!       /* Allocate new memory for the return array, since parts of the
!          original INDEX get freed.  Otherwise, if the document calls
!          @printindex twice on the same index, with duplicate entries,
!          we'll have garbage the second time.  There are cleaner ways to
!          deal, but this will suffice for now.  */
!       array[count] = xmalloc (sizeof (INDEX_ELT));
!       *(array[count]) = *(temp);  /* struct assignment, hope it's ok */
! 
!       /* Adjust next pointers to use the new memory.  */
!       if (count > 0)
!         array[count-1]->next = array[count];
  
        /* Set line number and input filename to the source line for this
           index entry, as this expansion finds any errors.  */
!       line_number = array[count]->defining_line;
!       input_filename = array[count]->defining_file;
  
        /* If this particular entry should be printed as a "code" index,
!          then expand it as @code{entry}, i.e., as in fixed-width font.  */
!       array[count]->entry = expansion (temp->entry_text, array[count]->code);
      }
    array[count] = NULL;    /* terminate the array. */
+ 
    line_number = save_line_number;
    input_filename = save_input_filename;
    html = save_html;
***************
*** 624,634 ****
        if (lang_env && !STREQ (lang_env, "C") && !STREQ (lang_env, "POSIX"))
          index_compare_fn = strcoll;
      }
! #endif /* HAVE_STRCOLL */    
  
    /* Sort the array. */
    qsort (array, count, sizeof (INDEX_ELT *), index_element_compare);
    make_index_entries_unique (array, count);
    return array;
  }
  
--- 625,642 ----
        if (lang_env && !STREQ (lang_env, "C") && !STREQ (lang_env, "POSIX"))
          index_compare_fn = strcoll;
      }
! #endif /* HAVE_STRCOLL */
  
    /* Sort the array. */
    qsort (array, count, sizeof (INDEX_ELT *), index_element_compare);
+ 
+   /* Remove duplicate entries.  */
    make_index_entries_unique (array, count);
+ 
+   /* Replace the original index with the sorted one, in case the
+      document wants to print it again.  */
+   *index = **array;
+ 
    return array;
  }
  
***************
*** 851,867 ****
  
        free (line);
        free (index_name);
!       
        me_inhibit_expansion--;
-       
        printing_index = 0;
!       free (array);
        close_single_paragraph ();
        filling_enabled = saved_filling_enabled;
        inhibit_paragraph_indentation = saved_inhibit_paragraph_indentation;
        input_filename = saved_input_filename;
        line_number = saved_line_number;
!       
        if (html)
          add_word ("</ul>");
        else if (xml && docbook)
--- 859,874 ----
  
        free (line);
        free (index_name);
! 
        me_inhibit_expansion--;
        printing_index = 0;
! 
        close_single_paragraph ();
        filling_enabled = saved_filling_enabled;
        inhibit_paragraph_indentation = saved_inhibit_paragraph_indentation;
        input_filename = saved_input_filename;
        line_number = saved_line_number;
! 
        if (html)
          add_word ("</ul>");
        else if (xml && docbook)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]