Date: Thu, 4 Feb 2016 08:32:05 +0000 (UTC) From: Garrett Cooper <ngie@FreeBSD.org> To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r295240 - user/ngie/bsnmp_cleanup/usr.sbin/bsnmpd/tools/libbsnmptools Message-ID: <201602040832.u148W51G096824@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: ngie Date: Thu Feb 4 08:32:05 2016 New Revision: 295240 URL: https://svnweb.freebsd.org/changeset/base/295240 Log: - Use calloc instead of malloc + memset - Fix really busted logic proposed in r295191. Use asprintf instead of malloc, then strlcpy to circumvent the warning correctly by automatically creating the right sized buffer. The proposed change I committed was broken from the get-go because it was running strlen on an improperly initialized buffer, so it would have always segfaulted [*] Reported by: bde [*] Modified: user/ngie/bsnmp_cleanup/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmpmap.c Modified: user/ngie/bsnmp_cleanup/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmpmap.c ============================================================================== --- user/ngie/bsnmp_cleanup/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmpmap.c Thu Feb 4 08:27:37 2016 (r295239) +++ user/ngie/bsnmp_cleanup/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmpmap.c Thu Feb 4 08:32:05 2016 (r295240) @@ -55,12 +55,11 @@ snmp_mapping_init(void) { struct snmp_mappings *m; - if ((m = malloc(sizeof(struct snmp_mappings))) == NULL) { + if ((m = calloc(1, sizeof(struct snmp_mappings))) == NULL) { syslog(LOG_ERR, "malloc() failed: %s", strerror(errno)); return (NULL); } - memset(m, 0, sizeof(struct snmp_mappings)); return (m); } @@ -268,21 +267,18 @@ enum_pair_insert(struct enum_pairs *head { struct enum_pair *e_new; - if ((e_new = malloc(sizeof(struct enum_pair))) == NULL) { + if ((e_new = calloc(1, sizeof(struct enum_pair))) == NULL) { syslog(LOG_ERR, "malloc() failed: %s", strerror(errno)); return (-1); } - memset(e_new, 0, sizeof(struct enum_pair)); - - if ((e_new->enum_str = malloc(strlen(enum_str) + 1)) == NULL) { + if (asprintf(&e_new->enum_str, "%s", enum_str) == -1) { syslog(LOG_ERR, "malloc() failed: %s", strerror(errno)); free(e_new); return (-1); } e_new->enum_val = enum_val; - strlcpy(e_new->enum_str, enum_str, strlen(e_new->enum_str)); STAILQ_INSERT_TAIL(headp, e_new, link); return (1); @@ -481,13 +477,11 @@ snmp_syntax_insert(struct snmp_idxlist * { struct index *idx; - if ((idx = malloc(sizeof(struct index))) == NULL) { + if ((idx = calloc(1, sizeof(struct index))) == NULL) { syslog(LOG_ERR, "malloc() failed: %s", strerror(errno)); return (-1); } - memset(idx, 0, sizeof(struct index)); - if (snmp_index_insert(headp, idx) < 0) { free(idx); return (-1); @@ -557,18 +551,16 @@ snmp_enumtc_init(char *name) { struct enum_type *enum_tc; - if ((enum_tc = malloc(sizeof(struct enum_type))) == NULL) { + if ((enum_tc = calloc(1, sizeof(struct enum_type))) == NULL) { syslog(LOG_ERR, "malloc() failed: %s", strerror(errno)); return (NULL); } - memset(enum_tc, 0, sizeof(struct enum_type)); - if ((enum_tc->name = malloc(strlen(name) + 1)) == NULL) { + if (asprintf(&enum_tc->name, "%s", name) == -1) { syslog(LOG_ERR, "malloc() failed: %s", strerror(errno)); free(enum_tc); return (NULL); } - strlcpy(enum_tc->name, name, sizeof(enum_tc->name)); return (enum_tc); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201602040832.u148W51G096824>