Date: Mon, 12 Nov 2012 08:10:48 GMT From: Robert Watson <rwatson@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 219759 for review Message-ID: <201211120810.qAC8AmsB055178@skunkworks.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@219759?ac=10 Change 219759 by rwatson@rwatson_zenith_cl_cam_ac_uk on 2012/11/12 08:10:03 Clean up error handling and output in cheritest sandbox_invoke. Affected files ... .. //depot/projects/ctsrd/cheribsd/src/bin/cheritest/sandbox.c#2 edit Differences ... ==== //depot/projects/ctsrd/cheribsd/src/bin/cheritest/sandbox.c#2 (text+ko) ==== @@ -55,7 +55,7 @@ register_t); struct sandbox { - const char *sb_path; + char *sb_path; void *sb_mem; register_t sb_sandboxlen; struct chericap sb_segment; @@ -77,31 +77,30 @@ return (-1); } - sb = malloc(sizeof(*sb)); + sb = calloc(1, sizeof(*sb)); if (sb == NULL) { saved_errno = errno; warn("%s: malloc", __func__); - close(fd); - errno = saved_errno; - return (-1); + goto error; + } + sb->sb_path = strdup(path); + if (sb->sb_path == NULL) { + saved_errno = errno; + warn("%s: fstat %s", __func__, path); + goto error; } if (fstat(fd, &sb->sb_stat) < 0) { saved_errno = errno; warn("%s: fstat %s", __func__, path); - free(sb); - close(fd); - errno = saved_errno; - return (-1); + goto error; } /* For now, support only "small" sandboxed programs. */ if (sb->sb_stat.st_size >= sandboxlen/2) { + saved_errno = EINVAL; warnx("%s: %s too large", __func__, path); - free(sb); - close(fd); - errno = EINVAL; - return (-1); + goto error; } /* @@ -111,10 +110,7 @@ if (sb->sb_mem == MAP_FAILED) { saved_errno = errno; warn("%s: mmap region", __func__); - free(sb); - close(fd); - errno = saved_errno; - return (-1); + goto error; } if (mmap((uint8_t *)sb->sb_mem + 0x1000, sb->sb_stat.st_size, @@ -122,13 +118,10 @@ MAP_FAILED) { saved_errno = errno; warn("%s: mmap %s", __func__, path); - munmap(sb->sb_mem, sandboxlen); - free(sb); - close(fd); - errno = saved_errno; - return (-1); + goto error; } close(fd); + fd = -1; if (mmap((uint8_t *)sb->sb_mem + 0x1000 + roundup2(sb->sb_stat.st_size, 4096), @@ -137,10 +130,7 @@ MAP_FAILED) { saved_errno = errno; warn("%s: mmap heap/stack", __func__); - munmap(sb->sb_mem, sandboxlen); - free(sb); - errno = saved_errno; - return (-1); + goto error; } /* @@ -167,12 +157,25 @@ CHERI_CGETTYPE(v, 10); printf(" otype %p\n", (void *)v); CHERI_CGETBASE(v, 10); - printf(" base %p\n", (void *)v); + printf(" base %p", (void *)v); CHERI_CGETLEN(v, 10); printf(" length %p\n", (void *)v); *sbp = sb; return (0); + +error: + if (sb != NULL) { + if (sb->sb_path != NULL) + free(sb->sb_path); + if (sb->sb_mem != NULL) + munmap(sb->sb_mem, sandboxlen); + free(sb); + } + if (fd != -1) + close(fd); + errno = saved_errno; + return (-1); } register_t
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201211120810.qAC8AmsB055178>