Commit 94400f32 authored by Daniel Gustafsson's avatar Daniel Gustafsson Committed by Jay Satiro
Browse files

all: Refactor malloc+memset to use calloc

When a zeroed out allocation is required, use calloc() rather than
malloc() followed by an explicit memset(). The result will be the
same, but using calloc() everywhere increases consistency in the
codebase and avoids the risk of subtle bugs when code is injected
between malloc and memset by accident.

Closes https://github.com/curl/curl/pull/2497
parent 2b126cd7
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -336,7 +336,6 @@ static void new_conn(char *url, GlobalInfo *g)
  CURLMcode rc;

  conn = calloc(1, sizeof(ConnInfo));
  memset(conn, 0, sizeof(ConnInfo));
  conn->error[0]='\0';

  conn->easy = curl_easy_init();
+1 −3
Original line number Diff line number Diff line
@@ -237,12 +237,10 @@ URL_FILE *url_fopen(const char *url, const char *operation)
  URL_FILE *file;
  (void)operation;

  file = malloc(sizeof(URL_FILE));
  file = calloc(1, sizeof(URL_FILE));
  if(!file)
    return NULL;

  memset(file, 0, sizeof(URL_FILE));

  file->handle.file = fopen(url, operation);
  if(file->handle.file)
    file->type = CFTYPE_FILE; /* marked as URL */
+0 −1
Original line number Diff line number Diff line
@@ -339,7 +339,6 @@ static void new_conn(char *url, GlobalInfo *g)
  CURLMcode rc;

  conn = calloc(1, sizeof(ConnInfo));
  memset(conn, 0, sizeof(ConnInfo));
  conn->error[0]='\0';

  conn->easy = curl_easy_init();
+1 −2
Original line number Diff line number Diff line
@@ -873,10 +873,9 @@ static contenc_writer *new_unencoding_writer(struct connectdata *conn,
                                             contenc_writer *downstream)
{
  size_t sz = offsetof(contenc_writer, params) + handler->paramsize;
  contenc_writer *writer = (contenc_writer *) malloc(sz);
  contenc_writer *writer = (contenc_writer *) calloc(1, sz);

  if(writer) {
    memset(writer, 0, sz);
    writer->handler = handler;
    writer->downstream = downstream;
    if(handler->init_writer(conn, writer)) {
+1 −3
Original line number Diff line number Diff line
@@ -745,12 +745,10 @@ CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
  len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;

  /* Allocate the response */
  ptr = malloc(len);
  ptr = calloc(1, len);
  if(!ptr)
    return CURLE_OUT_OF_MEMORY;

  memset(ptr, 0, len);

  /* Create the BLOB structure */
  snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
           "%c%c%c%c"   /* NTLMv2_BLOB_SIGNATURE */
Loading