Hello

I just encoutered a patch(1) limitation when using it on minified json 
files from Wordpress. The lines can span more than the maximum of what
patch(1) can cope, which is INT16_MAX. Here is a test for taht:

jot -b A $(( 37268 / 2 )) |tr '\n' 'A' > g.txt
mv g.txt g.txt.orig
sed 's/A/B/' g.txt.orig > g.txt
diff -U4 g.txt.orig  g.txt > g.patch
mv g.txt.orig g.txt
patch < g.patch

Currently it dumps a core. 

patch(1) tracks line length using a short, hence the INT16_MAX limit.
Changing short for long seems reasonable. Anyone sees something wrong
with this change?

--- patch.c.orig        2024-07-12 03:06:47.101773258 +0200
+++ patch.c     2024-07-12 03:07:58.394948697 +0200
@@ -1015,5 +1015,5 @@
        const char      *ilineptr;
        const char      *plineptr;
-       short           plinelen;
+       long            plinelen;
 
        for (iline = base + offset + fuzz; pline <= pat_lines; pline++, 
iline++) {
--- pch.c.orig  2024-07-12 03:06:26.498848676 +0200
+++ pch.c       2024-07-12 03:07:54.115023483 +0200
@@ -62,5 +62,5 @@
 static LINENUM p_input_line = 0;       /* current line # from patch file */
 static char    **p_line = NULL;/* the text of the hunk */
-static short   *p_len = NULL;  /* length of each line */
+static long    *p_len = NULL;  /* length of each line */
 static char    *p_char = NULL; /* +, -, and ! */
 static int     hunkmax = INITHUNKMAX;  /* size of above arrays to begin with */
@@ -149,5 +149,5 @@
        int             new_hunkmax;
        char            **new_p_line;
-       short           *new_p_len;
+       long            *new_p_len;
        char            *new_p_char;
 
@@ -1208,5 +1208,5 @@
 {
        char    **tp_line;      /* the text of the hunk */
-       short   *tp_len;        /* length of each line */
+       long    *tp_len;        /* length of each line */
        char    *tp_char;       /* +, -, and ! */
        LINENUM i;
@@ -1365,5 +1365,5 @@
  * Return the length of a particular patch line.
  */
-short
+long
 pch_line_len(LINENUM line)
 {
--- pch.h.orig  2024-07-12 03:06:34.298099780 +0200
+++ pch.h       2024-07-12 03:07:19.155488620 +0200
@@ -48,5 +48,5 @@
 bool           pch_swap(void);
 char           *pfetch(LINENUM);
-short          pch_line_len(LINENUM);
+long           pch_line_len(LINENUM);
 LINENUM                pch_first(void);
 LINENUM                pch_ptrn_lines(void);





-- 
Emmanuel Dreyfus
m...@netbsd.org

Reply via email to