Date: Thu, 14 Jul 2016 15:46:41 +0000 (UTC) From: Garrett Cooper <ngie@FreeBSD.org> To: ports-committers@freebsd.org, svn-ports-all@freebsd.org, svn-ports-head@freebsd.org Subject: svn commit: r418537 - in head/ports-mgmt/portfind: . files Message-ID: <201607141546.u6EFkffZ069876@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: ngie (src committer) Date: Thu Jul 14 15:46:41 2016 New Revision: 418537 URL: https://svnweb.freebsd.org/changeset/ports/418537 Log: Fix ports-mgmt/portfind string management/searching issues Makefile: Bump `PORT_REVISION` for the change. portfind.c: get_release(..): The function was incorrectly modifying a pointer that wasn't the original calloc'ed pointer, tripping asserts when MALLOC_PRODUCTION wasn't enabled in jemalloc [*]. - Use one temporary buffer (`release`) instead of two (`release` and `version`). - Improve temporary memory idiom for managing memory used with sysctlbyname(3) by first checking the length, mallocing the buffer, then filling it with a second call to sysctlbyname(3). - Use strchr(3) instead of handrolling it in a while-loop and to avoid the improper free(3) of the memory allocated for `release`. main(..): - Use asprintf instead of calloc + sprintf. - Use constant `pasting` with `INDEX_FILE` instead of passing it in to asprintf(3). - Fix error message when unable to open `INDEX_FILE`. Approved by: brd Differential Revision: https://reviews.freebsd.org/D7198 PR: 211032 [*] Reported by: Michael Zhilin <mizhka@gmail.com> Reviewed by: Michael Zhilin <mizhka@gmail.com> Sponsored by: EMC / Isilon Storage Division Added: head/ports-mgmt/portfind/files/ head/ports-mgmt/portfind/files/patch-portfind.c (contents, props changed) Modified: head/ports-mgmt/portfind/Makefile Modified: head/ports-mgmt/portfind/Makefile ============================================================================== --- head/ports-mgmt/portfind/Makefile Thu Jul 14 15:43:08 2016 (r418536) +++ head/ports-mgmt/portfind/Makefile Thu Jul 14 15:46:41 2016 (r418537) @@ -2,6 +2,7 @@ PORTNAME= portfind PORTVERSION= 1.6.1 +PORTREVISION= 1 CATEGORIES= ports-mgmt perl5 MASTER_SITES= http://dynsoft.com/files/ Added: head/ports-mgmt/portfind/files/patch-portfind.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/ports-mgmt/portfind/files/patch-portfind.c Thu Jul 14 15:46:41 2016 (r418537) @@ -0,0 +1,80 @@ +--- portfind.c.orig 2014-08-24 15:54:28 UTC ++++ portfind.c +@@ -103,20 +103,25 @@ int main(int argc, char **argv) { + return 0; + } + ++ char *filename; + char *release = get_release(); ++ + if(!release) { + fprintf(stderr, "Could not determine release\n"); + return 1; + } + +- char *filename = calloc(strlen(release) + strlen(INDEX_FILE) + 1, sizeof(char)); +- sprintf(filename, "%s%s", INDEX_FILE, release); ++ asprintf(&filename, INDEX_FILE "%s", release); ++ if (filename == NULL) { ++ fprintf(stderr, "Could not allocate memory for `filename`\n"); ++ return 1; ++ } + free(release); + + FILE *file = fopen(filename, "r"); + free(filename); + if(!file) { +- fprintf(stderr, "Could not open %s\n", INDEX_FILE); ++ fprintf(stderr, "Could not open %s\n", filename); + return 1; + } + +@@ -435,25 +440,32 @@ char *get_installed_version(const char * + return version; + } + +-char *get_release() { +- size_t length = 0; +- sysctlbyname("kern.osrelease", NULL, &length, NULL, 0); +- if(length == 0) +- return NULL; +- +- char *release = calloc(length, sizeof(char)); +- char *version = calloc(length, sizeof(char)); +- char *tmp = version; +- sysctlbyname("kern.osrelease", release, &length, NULL, 0); +- char c = *release; +- while(c != '.' && c != '\0') { +- *tmp++ = c; +- c = *(++release); +- } ++char *get_release(void) { ++ char *first_dot, *release; ++ size_t length; + ++ release = NULL; ++ ++ if (sysctlbyname("kern.osrelease", NULL, &length, NULL, 0) == -1) ++ goto fail; ++ ++ if ((release = malloc(sizeof(char) * length)) == NULL) ++ goto fail; ++ ++ if (sysctlbyname("kern.osrelease", release, &length, NULL, 0) == -1) ++ goto fail; ++ ++ if ((first_dot = strchr(release, '.')) == NULL) ++ goto fail; ++ ++ *first_dot = '\0'; ++ ++ return release; ++ ++fail: + free(release); + +- return version; ++ return NULL; + } + + void help(const char *program) {
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201607141546.u6EFkffZ069876>