From e9cb23e15d8fad0c504755b3e29c44d5ff716864 Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Mon, 28 Dec 2009 02:20:23 +0000 Subject: [PATCH] Portability: terminate abnormally via abort() instead of segfault, watch the return value from write(), and avoid signed arithmetic on unsigned values. --- lib/libarchive/archive_check_magic.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/libarchive/archive_check_magic.c b/lib/libarchive/archive_check_magic.c index 8f1fa2f00adb..1381a186be83 100644 --- a/lib/libarchive/archive_check_magic.c +++ b/lib/libarchive/archive_check_magic.c @@ -50,7 +50,16 @@ __FBSDID("$FreeBSD$"); static void errmsg(const char *m) { - write(2, m, strlen(m)); + size_t s = strlen(m); + ssize_t written; + + while (s > 0) { + written = write(2, m, strlen(m)); + if (written <= 0) + return; + m += written; + s -= written; + } } static void @@ -60,8 +69,7 @@ diediedie(void) /* Cause a breakpoint exception */ DebugBreak(); #endif - *(char *)0 = 1; /* Deliberately segfault and force a coredump. */ - _exit(1); /* If that didn't work, just exit with an error. */ + abort(); /* Terminate the program abnormally. */ } static const char * @@ -85,7 +93,7 @@ write_all_states(unsigned int states) unsigned int lowbit; /* A trick for computing the lowest set bit. */ - while ((lowbit = states & (-states)) != 0) { + while ((lowbit = states & (1 + ~states)) != 0) { states &= ~lowbit; /* Clear the low bit. */ errmsg(state_name(lowbit)); if (states != 0)