shithub: vcardfs

Download patch

ref: 732192cde475a07a558f581eecdd21f05cd1607a
parent: fa5d37089f461a9765301ef9c96094ffaa6cbc02
author: sirjofri <sirjofri@sirjofri.de>
date: Fri Nov 8 09:42:56 EST 2024

fixes memory leaks, adds line folding

--- a/john.vcf
+++ b/john.vcf
@@ -9,4 +9,5 @@
 VERSION:4.0
 N:D;Jane
 FN:Jane D
+TEL:4378574385974598437589574859475843975843957483957483957439857438754385974385974398
 END:VCARD
\ No newline at end of file
--- a/libvcard/vcard.c
+++ b/libvcard/vcard.c
@@ -119,7 +119,7 @@
 	char *upper;
 	
 	if (!param)
-		return strdup("");
+		return nil;
 	
 	s = nil;
 	for (p = param; p; p = p->next) {
@@ -151,8 +151,6 @@
 
 	ls = nil;
 	ps = serializeparams(l->params);
-	if (!ps)
-		goto Err;
 	upper = estrdup(l->name);
 	iltoupper(upper);
 	ls = smprint(
@@ -160,8 +158,12 @@
 		"%s%s:%s\r\n",    /* name, param, value */
 		(l->group && *l->group ? l->group : ""),  /* group string */
 		(l->group && *l->group ? "." : ""),       /* group dot */
-		upper, ps, l->value);
+		upper,
+		ps ? ps : "",
+		l->value);
 	free(upper);
+	if (ps)
+		free(ps);
 	if (!ls)
 		goto Err;
 	return ls;
@@ -173,6 +175,45 @@
 }
 
 static char*
+foldline(char *line)
+{
+	Rune r;
+	char *nline, *s, *lstart, *lend;
+	char *cut;
+	int l, n;
+	int rlen;
+	
+	l = strlen(line);
+	if (l <= 75)
+		return line;
+
+	n = l/75*3 + l;
+	nline = realloc(line, n+1);
+	if (!nline)
+		return line;
+	memset(nline + l, 0, l/75*3 + 1);
+	lend = strchr(nline, 0);
+	lstart = nline;
+
+	s = nline;
+	while (*s) {
+		rlen = chartorune(&r, s);
+		s += rlen;
+		if (s - lstart < 73)
+			continue;
+		memmove(s+3, s, lend-s);
+		s[0] = '\r';
+		s[1] = '\n';
+		s[2] = '\t';
+		s += 3;
+		lend += 3;
+		lstart = s;
+	}
+	
+	return nline;
+}
+
+static char*
 serializelines(Vline *line)
 {
 	Vline *l;
@@ -197,6 +238,7 @@
 			if (s) free(s);
 			return nil;
 		}
+		ls = foldline(ls);
 		if (!s) {
 			s = ls;
 		} else {
--- a/libvcard/vcard.y
+++ b/libvcard/vcard.y
@@ -58,9 +58,10 @@
 	vl = mallocz(sizeof(Vline), 1);
 	vl->name = name;
 	iltolower(vl->name);
-	if (strcmp(vl->name, "version") == 0)
+	if (strcmp(vl->name, "version") == 0) {
 		vl->value = strdup("4.0");
-	else
+		free(value);
+	} else
 		vl->value = value;
 	vl->params = params;
 	vl->group = group;