瀏覽代碼

libdpkg: Change m_calloc() signature to take also a nmemb argument

We are using these on arrays, so we end up having to compute the total
size from the number of members and the member size. Just delegate the
computation to the underlaying call which should ideally also handle
overflows and similar. And we can always pass nmemb=1 from the call
sites if needed.
Guillem Jover 11 年之前
父節點
當前提交
d8e6c56b20
共有 3 個文件被更改,包括 5 次插入5 次删除
  1. 1 1
      lib/dpkg/dpkg.h
  2. 2 2
      lib/dpkg/mlib.c
  3. 2 2
      src/querycmd.c

+ 1 - 1
lib/dpkg/dpkg.h

@@ -140,7 +140,7 @@ void cu_filename(int argc, void **argv);
 
 void setcloexec(int fd, const char *fn);
 void *m_malloc(size_t);
-void *m_calloc(size_t);
+void *m_calloc(size_t nmemb, size_t size);
 void *m_realloc(void *, size_t);
 char *m_strdup(const char *str);
 char *m_strndup(const char *str, size_t n);

+ 2 - 2
lib/dpkg/mlib.c

@@ -62,9 +62,9 @@ void *m_malloc(size_t amount) {
 }
 
 void *
-m_calloc(size_t size)
+m_calloc(size_t nmemb, size_t size)
 {
-  return must_alloc(calloc(1, size));
+  return must_alloc(calloc(nmemb, size));
 }
 
 void *m_realloc(void *r, size_t amount) {

+ 2 - 2
src/querycmd.c

@@ -290,7 +290,7 @@ listpackages(const char *const *argv)
     struct pkg_spec *ps;
 
     for (argc = 0; argv[argc]; argc++);
-    found = m_calloc(sizeof(int) * argc);
+    found = m_calloc(argc, sizeof(int));
 
     ps = m_malloc(sizeof(*ps) * argc);
     for (ip = 0; ip < argc; ip++) {
@@ -571,7 +571,7 @@ showpackages(const char *const *argv)
     struct pkg_spec *ps;
 
     for (argc = 0; argv[argc]; argc++);
-    found = m_calloc(sizeof(int) * argc);
+    found = m_calloc(argc, sizeof(int));
 
     ps = m_malloc(sizeof(*ps) * argc);
     for (ip = 0; ip < argc; ip++) {