mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Roman Zippel <zippel@linux-m68k.org>
To: linux-kernel@vger.kernel.org, torvalds@transmeta.com
Subject: [PATCH] module cleanup (2/5)
Date: Wed, 07 Aug 2002 00:51:36 +0200	[thread overview]
Message-ID: <E17cDAu-0002uy-00@scrub.xs4all.nl> (raw)

Hi,

This patch makes the various query functions more compact. It removes
the second loop to calculate the required space and integrates it into
the main loop.

diff -ur linux-2.5/kernel/module.c linux-mod/kernel/module.c
--- linux-2.5/kernel/module.c	Thu Aug  1 16:43:07 2002
+++ linux-mod/kernel/module.c	Thu Aug  1 14:35:31 2002
@@ -677,40 +649,34 @@
 {
 	struct module *mod;
 	size_t nmod, space, len;
+	int res = 0;
 
 	nmod = space = 0;
 
 	for (mod=module_list; mod != &kernel_module; mod=mod->next, ++nmod) {
 		len = strlen(mod->name)+1;
-		if (len > bufsize)
-			goto calc_space_needed;
-		if (copy_to_user(buf, mod->name, len))
-			return -EFAULT;
-		buf += len;
-		bufsize -= len;
+		if (len <= bufsize) {
+			if (copy_to_user(buf, mod->name, len))
+				return -EFAULT;
+			buf += len;
+			bufsize -= len;
+		} else {
+			bufsize = 0;
+			res = -ENOSPC;
+		}
 		space += len;
 	}
 
-	if (put_user(nmod, ret))
-		return -EFAULT;
-	else
-		return 0;
-
-calc_space_needed:
-	space += len;
-	while ((mod = mod->next) != &kernel_module)
-		space += strlen(mod->name)+1;
-
-	if (put_user(space, ret))
-		return -EFAULT;
-	else
-		return -ENOSPC;
+	if (put_user(res ? space : nmod, ret))
+		res = -EFAULT;
+	return res;
 }
 
 static int
 qm_deps(struct module *mod, char *buf, size_t bufsize, size_t *ret)
 {
 	size_t i, space, len;
+	int res = 0;
 
 	if (mod == &kernel_module)
 		return -EINVAL;
@@ -725,29 +691,21 @@
 		const char *dep_name = mod->deps[i].dep->name;
 
 		len = strlen(dep_name)+1;
-		if (len > bufsize)
-			goto calc_space_needed;
-		if (copy_to_user(buf, dep_name, len))
-			return -EFAULT;
-		buf += len;
-		bufsize -= len;
+		if (len <= bufsize) {
+			if (copy_to_user(buf, dep_name, len))
+				return -EFAULT;
+			buf += len;
+			bufsize -= len;
+		} else {
+			bufsize = 0;
+			res = -ENOSPC;
+		}
 		space += len;
 	}
 
-	if (put_user(i, ret))
-		return -EFAULT;
-	else
-		return 0;
-
-calc_space_needed:
-	space += len;
-	while (++i < mod->ndeps)
-		space += strlen(mod->deps[i].dep->name)+1;
-
-	if (put_user(space, ret))
-		return -EFAULT;
-	else
-		return -ENOSPC;
+	if (put_user(res ? space : i, ret))
+		res = -EFAULT;
+	return res;
 }
 
 static int
@@ -755,6 +713,7 @@
 {
 	size_t nrefs, space, len;
 	struct module_ref *ref;
+	int res = 0;
 
 	if (mod == &kernel_module)
 		return -EINVAL;
@@ -769,29 +728,21 @@
 		const char *ref_name = ref->ref->name;
 
 		len = strlen(ref_name)+1;
-		if (len > bufsize)
-			goto calc_space_needed;
-		if (copy_to_user(buf, ref_name, len))
-			return -EFAULT;
-		buf += len;
-		bufsize -= len;
+		if (len <= bufsize) {
+			if (copy_to_user(buf, ref_name, len))
+				return -EFAULT;
+			buf += len;
+			bufsize -= len;
+		} else {
+			bufsize = 0;
+			res = -ENOSPC;
+		}
 		space += len;
 	}
 
-	if (put_user(nrefs, ret))
-		return -EFAULT;
-	else
-		return 0;
-
-calc_space_needed:
-	space += len;
-	while ((ref = ref->next_ref) != NULL)
-		space += strlen(ref->ref->name)+1;
-
-	if (put_user(space, ret))
-		return -EFAULT;
-	else
-		return -ENOSPC;
+	if (put_user(res ? space : nrefs, ret))
+		res = -EFAULT;
+	return res;
 }
 
 static int
@@ -801,6 +752,7 @@
 	struct module_symbol *s;
 	char *strings;
 	unsigned long *vals;
+	int res = 0;
 
 	if (!MOD_CAN_QUERY(mod))
 		if (put_user(0, ret))
@@ -813,43 +765,37 @@
 	i = len = 0;
 	s = mod->syms;
 
-	if (space > bufsize)
-		goto calc_space_needed;
-
-	if (!access_ok(VERIFY_WRITE, buf, space))
-		return -EFAULT;
+	if (space > bufsize) {
+		bufsize = 0;
+		res = -ENOSPC;
+	} else {
+		bufsize -= space;
+		if (!access_ok(VERIFY_WRITE, buf, space))
+			return -EFAULT;
+	}
 
-	bufsize -= space;
 	vals = (unsigned long *)buf;
 	strings = buf+space;
 
 	for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
 		len = strlen(s->name)+1;
-		if (len > bufsize)
-			goto calc_space_needed;
-
-		if (copy_to_user(strings, s->name, len)
-		    || __put_user(s->value, vals+0)
-		    || __put_user(space, vals+1))
-			return -EFAULT;
+		if (len <= bufsize) {
+			if (copy_to_user(strings, s->name, len)
+			    || __put_user(s->value, vals+0)
+			    || __put_user(space, vals+1))
+				return -EFAULT;
 
-		strings += len;
-		bufsize -= len;
+			strings += len;
+			bufsize -= len;
+		} else {
+			bufsize = 0;
+			res = -ENOSPC;
+		}
 		space += len;
 	}
-	if (put_user(i, ret))
-		return -EFAULT;
-	else
-		return 0;
-
-calc_space_needed:
-	for (; i < mod->nsyms; ++i, ++s)
-		space += strlen(s->name)+1;
-
-	if (put_user(space, ret))
-		return -EFAULT;
-	else
-		return -ENOSPC;
+	if (put_user(res ? space : i, ret))
+		res = -EFAULT;
+	return res;
 }
 
 static int

                 reply	other threads:[~2002-08-06 22:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E17cDAu-0002uy-00@scrub.xs4all.nl \
    --to=zippel@linux-m68k.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@transmeta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®