[latex3-commits] [l3svn] r6796 - Forbid \newtoks in comparison code when sorting

noreply at latex-project.org noreply at latex-project.org
Thu Dec 15 07:17:05 CET 2016


Author: bruno
Date: 2016-12-15 07:17:05 +0100 (Thu, 15 Dec 2016)
New Revision: 6796

Modified:
   trunk/l3experimental/l3sort/l3sort.dtx
   trunk/l3experimental/l3sort/testfiles/m3sort001.luatex.tlg
   trunk/l3experimental/l3sort/testfiles/m3sort001.lvt
   trunk/l3experimental/l3sort/testfiles/m3sort001.tlg
Log:
Forbid \newtoks in comparison code when sorting


Modified: trunk/l3experimental/l3sort/l3sort.dtx
===================================================================
--- trunk/l3experimental/l3sort/l3sort.dtx	2016-12-15 05:45:53 UTC (rev 6795)
+++ trunk/l3experimental/l3sort/l3sort.dtx	2016-12-15 06:17:05 UTC (rev 6796)
@@ -371,7 +371,7 @@
       }
   }
 %</package>
-%<*format>
+%<*initex>
 \int_const:Nn \c_@@_max_length_int
   { ( \c_max_register_int + 1 ) * 3 / 4 }
 \cs_new_protected:Npn \@@_compute_range:
@@ -380,7 +380,7 @@
     \int_set:Nn \l_@@_true_max_int { \c_max_register_int + \c_one }
     \int_set_eq:NN \l_@@_max_int \c_@@_max_length_int
   }
-%</format>
+%</initex>
 %    \end{macrocode}
 % \end{variable}
 % \end{macro}
@@ -402,6 +402,7 @@
 \cs_new_protected:Npn \@@_main:NNNnNn #1#2#3#4#5#6
   {
     \group_begin:
+%<package>      \@@_disable_toksdef:
       \@@_compute_range:
       \int_set_eq:NN \l_@@_top_int \l_@@_min_int
       #2 #5
@@ -593,7 +594,7 @@
       \tex_advance:D \l_@@_A_int - \c_one
       \tex_advance:D \l_@@_B_int - \c_one
       \tex_advance:D \l_@@_C_int - \c_one
-      \@@_merge_blocks_aux:
+      \exp_after:wN \@@_merge_blocks_aux:
       \exp_after:wN \@@_merge_blocks:
     \fi:
   }
@@ -1031,7 +1032,40 @@
 %
 % \subsection{Messages}
 %
-% \begin{macro}[aux]{\@@_too_long_error:NNw, \@@_too_long_error_aux:w}
+% \begin{macro}[aux]{\@@_disable_toksdef:, \@@_disabled_toksdef:n}
+%   While sorting, \tn{toksdef} is locally disabled to prevent users
+%   from using \tn{newtoks} or similar commands in their comparison
+%   code: the \tn{toks} registers that would be assigned are in use by
+%   \pkg{l3sort}.  Since it may be unsafe to bail out of the user's code
+%   with a delimited argument, we redefine many \pkg{l3sort} commands to
+%   be trivial, with \cs{@@_level:} getting rid of the final assignment.
+%   This error recovery won't work in a group.  In format mode, none of
+%   this is needed since there is no \tn{toks} allocator.
+%    \begin{macrocode}
+%<*package>
+\cs_new_protected:Npn \@@_disable_toksdef:
+  { \cs_set_eq:NN \toksdef \@@_disabled_toksdef:n }
+\cs_new_protected:Npn \@@_disabled_toksdef:n #1
+  {
+    \__msg_kernel_error:nnx { sort } { toksdef }
+      { \token_to_str:N #1 }
+    \cs_set_eq:NN \@@_merge_blocks_aux: \prg_do_nothing:
+    \cs_set_eq:NN \@@_merge_blocks: \prg_do_nothing:
+    \cs_set_protected:Npn \@@_level: { \@@_error:w }
+    \tex_toksdef:D #1
+  }
+\__msg_kernel_new:nnnn { sort } { toksdef }
+  { Allocation~of~\iow_char:N\\toks~registers~impossible~while~sorting. }
+  {
+    The~comparison~code~used~for~sorting~a~list~has~attempted~to~
+    define~#1~as~a~new~\iow_char:N\\toks~register~using~\iow_char:N\\newtoks~
+    or~a~similar~command.~The~list~will~not~be~sorted.
+  }
+%</package>
+%    \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}[aux]{\@@_too_long_error:NNw, \@@_error:w}
 %   When there are too many items in a sequence, this is an error, and
 %   we clean up properly the mapping over items in the list: break using
 %   the type-specific breaking function |#1|.
@@ -1043,9 +1077,9 @@
       { \token_to_str:N #2 }
       { \int_eval:n { \l_@@_true_max_int - \l_@@_min_int } }
       { \int_eval:n { \l_@@_top_int - \l_@@_min_int } }
-    #1 \@@_too_long_error_aux:w
+    #1 \@@_error:w
   }
-\cs_new_protected:Npn \@@_too_long_error_aux:w #1 \use:x #2
+\cs_new_protected:Npn \@@_error:w #1 \use:x #2
   { \group_end: }
 \__msg_kernel_new:nnnn { sort } { too-large }
   { The~list~#1~is~too~long~to~be~sorted~by~TeX. }

Modified: trunk/l3experimental/l3sort/testfiles/m3sort001.luatex.tlg
===================================================================
--- trunk/l3experimental/l3sort/testfiles/m3sort001.luatex.tlg	2016-12-15 05:45:53 UTC (rev 6795)
+++ trunk/l3experimental/l3sort/testfiles/m3sort001.luatex.tlg	2016-12-15 06:17:05 UTC (rev 6796)
@@ -76,3 +76,27 @@
 | up to 48849 items. All extra items will be deleted.
 |...............................................
 ============================================================
+============================================================
+TEST 5: sort newtoks error
+============================================================
+dc
+ba
+ca
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+!
+! LaTeX error: "sort/toksdef"
+! 
+! Allocation of \toks registers impossible while sorting.
+! 
+! See the LaTeX3 documentation for further information.
+! 
+! For immediate help type H <return>.
+!...............................................  
+l. ...  }
+|'''''''''''''''''''''''''''''''''''''''''''''''
+| The comparison code used for sorting a list has attempted to define
+| \l_test_toks as a new \toks register using \newtoks or a similar command.
+| The list will not be sorted.
+|...............................................
+\l_test_toks=\toks303
+============================================================

Modified: trunk/l3experimental/l3sort/testfiles/m3sort001.lvt
===================================================================
--- trunk/l3experimental/l3sort/testfiles/m3sort001.lvt	2016-12-15 05:45:53 UTC (rev 6795)
+++ trunk/l3experimental/l3sort/testfiles/m3sort001.lvt	2016-12-15 06:17:05 UTC (rev 6796)
@@ -122,4 +122,19 @@
       { \ERROR }
   }
 
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\TEST { sort~newtoks~error }
+  {
+    \tl_set:Nn \l_tmpa_tl { dcba }
+    \tl_sort:Nn \l_tmpa_tl
+      {
+        \TYPE { #1 #2 }
+        \str_if_eq:nnT { #1 #2 } { c a }
+          { \use:c { newtoks } \l_test_toks }
+        \sort_return_unchanged:
+      }
+  }
+
+
 \END

Modified: trunk/l3experimental/l3sort/testfiles/m3sort001.tlg
===================================================================
--- trunk/l3experimental/l3sort/testfiles/m3sort001.tlg	2016-12-15 05:45:53 UTC (rev 6795)
+++ trunk/l3experimental/l3sort/testfiles/m3sort001.tlg	2016-12-15 06:17:05 UTC (rev 6796)
@@ -76,3 +76,27 @@
 | up to 24273 items. All extra items will be deleted.
 |...............................................
 ============================================================
+============================================================
+TEST 5: sort newtoks error
+============================================================
+dc
+ba
+ca
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+!
+! LaTeX error: "sort/toksdef"
+! 
+! Allocation of \toks registers impossible while sorting.
+! 
+! See the LaTeX3 documentation for further information.
+! 
+! For immediate help type H <return>.
+!...............................................  
+l. ...  }
+|'''''''''''''''''''''''''''''''''''''''''''''''
+| The comparison code used for sorting a list has attempted to define
+| \l_test_toks as a new \toks register using \newtoks or a similar command.
+| The list will not be sorted.
+|...............................................
+\l_test_toks=\toks303
+============================================================



More information about the latex3-commits mailing list