diff mercurial/thirdparty/xdiff/xprepare.c @ 36822:882657a9f768

xdiff: replace {unsigned ,}long with {u,}int64_t MSVC treats "long" as 4-byte. That could cause overflows since the xdiff code uses "long" in places where "size_t" or "ssize_t" should be used. Let's use explicit 8 byte integers to avoid FWIW git avoids that overflow by limiting diff size to 1GB [1]. After examining the code, I think the remaining risk (the use of "int") is low since "int" is only used for return values and hash table size. Although a wrong hash table size would not affect the correctness of the code, but that could make the code extremely slow. The next patch will change hash table size to 8-byte integer so the 1GB limit is unlikely needed. This patch was done by using `sed`. [1]: https://github.com/git/git/commit/dcd1742e56ebb944c4ff62346da4548e1e3be67 Differential Revision: https://phab.mercurial-scm.org/D2762
author Jun Wu <quark@fb.com>
date Fri, 09 Mar 2018 14:24:27 -0800
parents f33a87cf60cc
children 49fe6249937a
line wrap: on
line diff
--- a/mercurial/thirdparty/xdiff/xprepare.c	Sun Mar 04 11:30:16 2018 -0800
+++ b/mercurial/thirdparty/xdiff/xprepare.c	Fri Mar 09 14:24:27 2018 -0800
@@ -31,35 +31,35 @@
 
 typedef struct s_xdlclass {
 	struct s_xdlclass *next;
-	unsigned long ha;
+	uint64_t ha;
 	char const *line;
-	long size;
-	long idx;
-	long len1, len2;
+	int64_t size;
+	int64_t idx;
+	int64_t len1, len2;
 } xdlclass_t;
 
 typedef struct s_xdlclassifier {
 	unsigned int hbits;
-	long hsize;
+	int64_t hsize;
 	xdlclass_t **rchash;
 	chastore_t ncha;
 	xdlclass_t **rcrecs;
-	long alloc;
-	long count;
-	long flags;
+	int64_t alloc;
+	int64_t count;
+	int64_t flags;
 } xdlclassifier_t;
 
 
 
 
-static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags);
+static int xdl_init_classifier(xdlclassifier_t *cf, int64_t size, int64_t flags);
 static void xdl_free_classifier(xdlclassifier_t *cf);
 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
 			       unsigned int hbits, xrecord_t *rec);
-static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
+static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, int64_t narec, xpparam_t const *xpp,
 			   xdlclassifier_t *cf, xdfile_t *xdf);
 static void xdl_free_ctx(xdfile_t *xdf);
-static int xdl_clean_mmatch(char const *dis, long i, long s, long e);
+static int xdl_clean_mmatch(char const *dis, int64_t i, int64_t s, int64_t e);
 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2);
 static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2);
@@ -67,7 +67,7 @@
 
 
 
-static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) {
+static int xdl_init_classifier(xdlclassifier_t *cf, int64_t size, int64_t flags) {
 	cf->flags = flags;
 
 	cf->hbits = xdl_hashbits((unsigned int) size);
@@ -108,7 +108,7 @@
 
 static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash,
 			       unsigned int hbits, xrecord_t *rec) {
-	long hi;
+	int64_t hi;
 	char const *line;
 	xdlclass_t *rcrec;
 	xdlclass_t **rcrecs;
@@ -163,11 +163,11 @@
  * outweighs the shift change. A diff result with suboptimal shifting is still
  * valid.
  */
-static void xdl_trim_files(mmfile_t *mf1, mmfile_t *mf2, long reserved,
+static void xdl_trim_files(mmfile_t *mf1, mmfile_t *mf2, int64_t reserved,
 		xdfenv_t *xe, mmfile_t *out_mf1, mmfile_t *out_mf2) {
 	mmfile_t msmall, mlarge;
 	/* prefix lines, prefix bytes, suffix lines, suffix bytes */
-	long plines = 0, pbytes = 0, slines = 0, sbytes = 0, i;
+	int64_t plines = 0, pbytes = 0, slines = 0, sbytes = 0, i;
 	/* prefix char pointer for msmall and mlarge */
 	const char *pp1, *pp2;
 	/* suffix char pointer for msmall and mlarge */
@@ -237,18 +237,18 @@
 }
 
 
-static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp,
+static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, int64_t narec, xpparam_t const *xpp,
 			   xdlclassifier_t *cf, xdfile_t *xdf) {
 	unsigned int hbits;
-	long nrec, hsize, bsize;
-	unsigned long hav;
+	int64_t nrec, hsize, bsize;
+	uint64_t hav;
 	char const *blk, *cur, *top, *prev;
 	xrecord_t *crec;
 	xrecord_t **recs, **rrecs;
 	xrecord_t **rhash;
-	unsigned long *ha;
+	uint64_t *ha;
 	char *rchg;
-	long *rindex;
+	int64_t *rindex;
 
 	ha = NULL;
 	rindex = NULL;
@@ -296,9 +296,9 @@
 		goto abort;
 	memset(rchg, 0, (nrec + 2) * sizeof(char));
 
-	if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long))))
+	if (!(rindex = (int64_t *) xdl_malloc((nrec + 1) * sizeof(long))))
 		goto abort;
-	if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long))))
+	if (!(ha = (uint64_t *) xdl_malloc((nrec + 1) * sizeof(unsigned long))))
 		goto abort;
 
 	xdf->nrec = nrec;
@@ -340,7 +340,7 @@
 
 int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 		    xdfenv_t *xe) {
-	long enl1, enl2, sample;
+	int64_t enl1, enl2, sample;
 	mmfile_t tmf1, tmf2;
 	xdlclassifier_t cf;
 
@@ -388,8 +388,8 @@
 }
 
 
-static int xdl_clean_mmatch(char const *dis, long i, long s, long e) {
-	long r, rdis0, rpdis0, rdis1, rpdis1;
+static int xdl_clean_mmatch(char const *dis, int64_t i, int64_t s, int64_t e) {
+	int64_t r, rdis0, rpdis0, rdis1, rpdis1;
 
 	/*
 	 * Limits the window the is examined during the similar-lines
@@ -452,7 +452,7 @@
  * might be potentially discarded if they happear in a run of discardable.
  */
 static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) {
-	long i, nm, nreff, mlim;
+	int64_t i, nm, nreff, mlim;
 	xrecord_t **recs;
 	xdlclass_t *rcrec;
 	char *dis, *dis1, *dis2;
@@ -515,7 +515,7 @@
  * Early trim initial and terminal matching records.
  */
 static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) {
-	long i, lim;
+	int64_t i, lim;
 	xrecord_t **recs1, **recs2;
 
 	recs1 = xdf1->recs;