Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Add two utility functions to help use kmem with strings:...
details: https://anonhg.NetBSD.org/src/rev/6f8343d4e6cd
branches: trunk
changeset: 357367:6f8343d4e6cd
user: christos <christos%NetBSD.org@localhost>
date: Tue Nov 07 18:35:57 2017 +0000
description:
Add two utility functions to help use kmem with strings: kmem_strdupsize,
kmem_strfree.
diffstat:
sys/kern/kern_event.c | 8 +++-----
sys/kern/kern_veriexec.c | 9 ++++-----
sys/kern/subr_devsw.c | 9 +++------
sys/kern/subr_kmem.c | 27 +++++++++++++++++++++++++--
sys/sys/kmem.h | 5 ++++-
5 files changed, 39 insertions(+), 19 deletions(-)
diffs (168 lines):
diff -r d476eea159da -r 6f8343d4e6cd sys/kern/kern_event.c
--- a/sys/kern/kern_event.c Tue Nov 07 16:30:32 2017 +0000
+++ b/sys/kern/kern_event.c Tue Nov 07 18:35:57 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_event.c,v 1.96 2017/10/25 08:12:39 maya Exp $ */
+/* $NetBSD: kern_event.c,v 1.97 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.96 2017/10/25 08:12:39 maya Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.97 2017/11/07 18:35:57 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -370,9 +370,7 @@
/* Adding new slot */
kfilter = &user_kfilters[user_kfilterc++];
reuse:
- kfilter->namelen = strlen(name) + 1;
- kfilter->name = kmem_alloc(kfilter->namelen, KM_SLEEP);
- memcpy(__UNCONST(kfilter->name), name, kfilter->namelen);
+ kfilter->name = kmem_strdupsize(name, &kfilter->namelen, KM_SLEEP);
kfilter->filter = (kfilter - user_kfilters) + EVFILT_SYSCOUNT;
diff -r d476eea159da -r 6f8343d4e6cd sys/kern/kern_veriexec.c
--- a/sys/kern/kern_veriexec.c Tue Nov 07 16:30:32 2017 +0000
+++ b/sys/kern/kern_veriexec.c Tue Nov 07 18:35:57 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_veriexec.c,v 1.17 2017/09/13 22:24:46 sevan Exp $ */
+/* $NetBSD: kern_veriexec.c,v 1.18 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2005, 2006 Elad Efrat <elad%NetBSD.org@localhost>
@@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_veriexec.c,v 1.17 2017/09/13 22:24:46 sevan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_veriexec.c,v 1.18 2017/11/07 18:35:57 christos Exp $");
#include "opt_veriexec.h"
@@ -1104,9 +1104,8 @@
vfe->status = FINGERPRINT_NOTEVAL;
if (prop_bool_true(prop_dictionary_get(dict, "keep-filename"))) {
- vfe->filename_len = strlen(file) + 1;
- vfe->filename = kmem_alloc(vfe->filename_len, KM_SLEEP);
- strlcpy(vfe->filename, file, vfe->filename_len);
+ vfe->filename = kmem_strdupsize(file, &vfe->filename_len,
+ KM_SLEEP);
} else
vfe->filename = NULL;
diff -r d476eea159da -r 6f8343d4e6cd sys/kern/subr_devsw.c
--- a/sys/kern/subr_devsw.c Tue Nov 07 16:30:32 2017 +0000
+++ b/sys/kern/subr_devsw.c Tue Nov 07 18:35:57 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_devsw.c,v 1.37 2017/04/25 08:46:38 pgoyette Exp $ */
+/* $NetBSD: subr_devsw.c,v 1.38 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
@@ -69,7 +69,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.37 2017/04/25 08:46:38 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.38 2017/11/07 18:35:57 christos Exp $");
#ifdef _KERNEL_OPT
#include "opt_dtrace.h"
@@ -128,7 +128,6 @@
struct devsw_conv *conv;
char *name;
int error, i;
- size_t len;
if (devname == NULL || cdev == NULL)
return (EINVAL);
@@ -204,14 +203,12 @@
max_devsw_convs = new_convs;
}
- len = strlen(devname) + 1;
- name = kmem_alloc(len, KM_NOSLEEP);
+ name = kmem_strdupsize(devname, NULL, KM_NOSLEEP);
if (name == NULL) {
devsw_detach_locked(bdev, cdev);
error = ENOMEM;
goto fail;
}
- strlcpy(name, devname, len);
devsw_conv[i].d_name = name;
devsw_conv[i].d_bmajor = *bmajor;
diff -r d476eea159da -r 6f8343d4e6cd sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c Tue Nov 07 16:30:32 2017 +0000
+++ b/sys/kern/subr_kmem.c Tue Nov 07 18:35:57 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_kmem.c,v 1.63 2017/04/12 20:05:54 christos Exp $ */
+/* $NetBSD: subr_kmem.c,v 1.64 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
@@ -100,7 +100,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.63 2017/04/12 20:05:54 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.64 2017/11/07 18:35:57 christos Exp $");
#ifdef _KERNEL_OPT
#include "opt_kmem.h"
@@ -527,6 +527,29 @@
return str;
}
+char *
+kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
+{
+ size_t len = strlen(str) + 1;
+ char *ptr = kmem_alloc(len, flags);
+ if (ptr == NULL)
+ return NULL;
+
+ if (lenp)
+ *lenp = len;
+ memcpy(ptr, str, len);
+ return ptr;
+}
+
+void
+kmem_strfree(char *str)
+{
+ if (str == NULL)
+ return;
+
+ kmem_free(str, strlen(str) + 1);
+}
+
/* ------------------ DEBUG / DIAGNOSTIC ------------------ */
#if defined(KMEM_POISON) || defined(KMEM_REDZONE)
diff -r d476eea159da -r 6f8343d4e6cd sys/sys/kmem.h
--- a/sys/sys/kmem.h Tue Nov 07 16:30:32 2017 +0000
+++ b/sys/sys/kmem.h Tue Nov 07 18:35:57 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kmem.h,v 1.9 2012/02/05 03:40:08 rmind Exp $ */
+/* $NetBSD: kmem.h,v 1.10 2017/11/07 18:35:57 christos Exp $ */
/*-
* Copyright (c)2006 YAMAMOTO Takashi,
@@ -46,6 +46,9 @@
char * kmem_asprintf(const char *, ...) __printflike(1, 2);
+char * kmem_strdupsize(const char *, size_t *, km_flag_t);
+void kmem_strfree(char *);
+
/*
* km_flag_t values:
*/
Home |
Main Index |
Thread Index |
Old Index