List for cgit developers and users
 help / color / mirror / Atom feed
From: jamie.couture at gmail.com (Jamie Couture)
Subject: [PATCH 1/1] move LCS table away from the stack
Date: Fri,  9 Sep 2011 00:43:10 -0400	[thread overview]
Message-ID: <1315543390-12451-2-git-send-email-jamie.couture@gmail.com> (raw)
In-Reply-To: <1315543390-12451-1-git-send-email-jamie.couture@gmail.com>

From: Jamie Couture <jamie.couture at gmail.com>

Minified files such as javascript, or something similar, where very long
single lines to compare when printing deferred line changes would have
caused segfaults.

Signed-off-by: Jamie Couture <jamie.couture at gmail.com>
---
 cgit.h      |   12 ++++++++++++
 ui-ssdiff.c |   32 ++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/cgit.h b/cgit.h
index bad66f0..1ff5cb3 100644
--- a/cgit.h
+++ b/cgit.h
@@ -47,6 +47,18 @@
  */
 #define PAGE_ENCODING "UTF-8"
 
+/*
+ * ssdiff line limits
+ */
+#ifndef MAX_SSDIFF_M
+#define MAX_SSDIFF_M 1024
+#endif
+
+#ifndef MAX_SSDIFF_N
+#define MAX_SSDIFF_N 1024
+#endif
+#define MAX_SSDIFF_SIZE ((MAX_SSDIFF_M) * (MAX_SSDIFF_N))
+
 typedef void (*configfn)(const char *name, const char *value);
 typedef void (*filepair_fn)(struct diff_filepair *pair);
 typedef void (*linediff_fn)(char *line, int len);
diff --git a/ui-ssdiff.c b/ui-ssdiff.c
index 2481585..4df01ed 100644
--- a/ui-ssdiff.c
+++ b/ui-ssdiff.c
@@ -16,16 +16,41 @@ struct deferred_lines {
 static struct deferred_lines *deferred_old, *deferred_old_last;
 static struct deferred_lines *deferred_new, *deferred_new_last;
 
+static int **create_lcs_table(size_t m, size_t n)
+{
+	int **L;
+	int i;
+
+	// xcalloc will die if we ran out of memory;
+	// not very helpful for debugging
+	L = (int**)xcalloc(m, sizeof(int *));
+	*L = (int*)xcalloc(m * n, sizeof(int));
+
+	for (i = 1; i < m; i++) {
+		L[i] = *L + i * n;
+	}
+
+	return L;
+}
+
 static char *longest_common_subsequence(char *A, char *B)
 {
 	int i, j, ri;
 	int m = strlen(A);
 	int n = strlen(B);
-	int L[m + 1][n + 1];
-	int tmp1, tmp2;
+	int **L;
+	int tmp1, tmp2, length;
 	int lcs_length;
 	char *result;
 
+	length = (m + 1) * (n + 1);
+
+	// We bail if the lines are too long
+	if (length > MAX_SSDIFF_SIZE)
+		return NULL;
+
+	L = create_lcs_table(m + 1, n + 1);
+
 	for (i = m; i >= 0; i--) {
 		for (j = n; j >= 0; j--) {
 			if (A[i] == '\0' || B[j] == '\0') {
@@ -59,6 +84,9 @@ static char *longest_common_subsequence(char *A, char *B)
 			j += 1;
 		}
 	}
+
+	free(*L);
+	free(L);
 	return result;
 }
 
-- 
1.7.6





  reply	other threads:[~2011-09-09  4:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-09  4:43 [PATCH 0/1] segfault in cgit jamie.couture
2011-09-09  4:43 ` jamie.couture [this message]
2011-09-11  0:44   ` [PATCH 1/1] move LCS table away from the stack jamie.couture
2011-09-11  0:44     ` [PATCH] " jamie.couture
2011-09-14  6:38       ` hjemli
2011-09-17 22:25         ` [PATCH 1/1] " jamie.couture
2012-01-03 15:12           ` hjemli
2012-01-04  8:59             ` normalperson
2012-01-04 16:57               ` jamie.couture
2012-01-12  3:38               ` jamie.couture
2012-01-12  3:38                 ` [PATCH 1/2] correct length check for LCS table jamie.couture
2012-01-12  3:38                 ` [PATCH 2/2] use correct type for sizeof jamie.couture
2012-03-18  9:22                   ` hjemli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1315543390-12451-2-git-send-email-jamie.couture@gmail.com \
    --to=cgit@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).