From f6630ece2ac846b0975c224027dc50d037fe09b1 Mon Sep 17 00:00:00 2001 From: form Date: Mon, 24 Apr 2000 03:33:27 +0000 Subject: [PATCH] Add xstrdup() - like strdup but get fatal error if memory is exhausted. Avoid duplicates in search path. ok espie@ --- gnu/usr.bin/ld/etc.c | 17 ++++++++++++++++- gnu/usr.bin/ld/ld.h | 3 ++- gnu/usr.bin/ld/shlib.c | 16 ++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/gnu/usr.bin/ld/etc.c b/gnu/usr.bin/ld/etc.c index d3716f03ece..1b6aec578c4 100644 --- a/gnu/usr.bin/ld/etc.c +++ b/gnu/usr.bin/ld/etc.c @@ -1,4 +1,4 @@ -/* * $OpenBSD: etc.c,v 1.2 1998/03/26 19:46:18 niklas Exp $*/ +/* * $OpenBSD: etc.c,v 1.3 2000/04/24 03:33:27 form Exp $*/ /* */ @@ -6,6 +6,21 @@ #include #include +/* + * Like strdup but get fatal error if memory is exhausted. + */ +char * +xstrdup(s) + char *s; +{ + char *result = strdup(s); + + if (!result) + errx(1, "virtual memory exhausted"); + + return result; +} + /* * Like malloc but get fatal error if memory is exhausted. */ diff --git a/gnu/usr.bin/ld/ld.h b/gnu/usr.bin/ld/ld.h index d5e6efabd1d..9eb86a3f806 100644 --- a/gnu/usr.bin/ld/ld.h +++ b/gnu/usr.bin/ld/ld.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ld.h,v 1.4 1999/05/10 16:18:33 espie Exp $ */ +/* $OpenBSD: ld.h,v 1.5 2000/04/24 03:33:27 form Exp $ */ /*- * This code is derived from software copyrighted by the Free Software @@ -640,6 +640,7 @@ void prline_file_name __P((struct file_entry *, FILE *)); int do_warnings __P((FILE *)); /* In etc.c: */ +char *xstrdup __P((char *)); void *xmalloc __P((size_t)); void *xrealloc __P((void *, size_t)); char *concat __P((const char *, const char *, const char *)); diff --git a/gnu/usr.bin/ld/shlib.c b/gnu/usr.bin/ld/shlib.c index 01343faf557..9c564e631d7 100644 --- a/gnu/usr.bin/ld/shlib.c +++ b/gnu/usr.bin/ld/shlib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: shlib.c,v 1.8 2000/01/16 14:31:26 espie Exp $ */ +/* $OpenBSD: shlib.c,v 1.9 2000/04/24 03:33:27 form Exp $ */ /* $NetBSD: shlib.c,v 1.13 1998/04/04 01:00:29 fvdl Exp $ */ /* @@ -77,10 +77,22 @@ void add_search_dir(name) char *name; { + int i, len; + + len = strlen(name); + + while (len > 1 && name[len - 1] == '/') + --len; + + for (i = 0; i < n_search_dirs; i++) + if (strlen(search_dirs[i]) == len && + !strncmp(search_dirs[i], name, len)) + return; n_search_dirs++; search_dirs = (char **) xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]); - search_dirs[n_search_dirs - 1] = strdup(name); + search_dirs[n_search_dirs - 1] = xmalloc(++len); + (void)strlcpy(search_dirs[n_search_dirs - 1], name, len); } void -- 2.20.1