Просмотр исходного кода

libdpkg: Rewrite get_prefix_name() to be more efficient

Avoid temporary allocations from the heap and use sprintf to write the
string with delimited parts.
Guillem Jover лет назад: 15
Родитель
Сommit
b070e0ec0d
1 измененных файлов с 5 добавлено и 14 удалено
  1. 5 14
      lib/dpkg/tarfn.c

+ 5 - 14
lib/dpkg/tarfn.c

@@ -95,26 +95,17 @@ StoC(const char *s, int size)
 	return str;
 }
 
-/* FIXME: Rewrite using varbuf, once it supports the needed functionality. */
 static char *
 get_prefix_name(struct tar_header *h)
 {
-	char *prefix, *name, *s;
+	char *path;
 
-	/* The size is not going to be bigger than that. */
-	s = m_malloc(257);
+	path = m_malloc(sizeof(h->prefix) + 1 + sizeof(h->name) + 1);
 
-	prefix = StoC(h->prefix, sizeof(h->prefix));
-	name = StoC(h->name, sizeof(h->name));
+	sprintf(path, "%.*s/%.*s", (int)sizeof(h->prefix), h->prefix,
+	        (int)sizeof(h->name), h->name);
 
-	strcpy(s, prefix);
-	strcat(s, "/");
-	strcat(s, name);
-
-	free(prefix);
-	free(name);
-
-	return s;
+	return path;
 }
 
 static mode_t