util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libfdisk: fix get_partition_unused_primary()
@ 2014-11-14  5:27 Boris Egorov
  2014-11-14  9:11 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Boris Egorov @ 2014-11-14  5:27 UTC (permalink / raw)
  To: util-linux; +Cc: Boris Egorov

Was:
Mentioned function returns -1 if adding of primary partition is
impossible. Caller treats this value as size_t (res variable) and then
compares it for negative values, totally ignoring errors.

Becomes:
Now function takes address to variable and fills it with partition
number. Caller treats return value as int and use it appropriately.

Signed-off-by: Boris Egorov <egorov@linux.com>
---
 libfdisk/src/dos.c | 46 +++++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c
index b4456a9..0edb5fd 100644
--- a/libfdisk/src/dos.c
+++ b/libfdisk/src/dos.c
@@ -186,24 +186,28 @@ static int is_cleared_partition(struct dos_partition *p)
 }
 
 static int get_partition_unused_primary(struct fdisk_context *cxt,
-					struct fdisk_partition *pa)
+					struct fdisk_partition *pa,
+					size_t *partno)
 {
-	size_t org = cxt->label->nparts_max, n;
+	size_t org, n;
 	int rc;
 
+	assert(cxt);
+	assert(cxt->label);
+	assert(partno);
+
+	org = cxt->label->nparts_max;
+
 	cxt->label->nparts_max = 4;
 	rc = fdisk_partition_next_partno(pa, cxt, &n);
 	cxt->label->nparts_max = org;
 
-	switch (rc) {
-	case 1:
+	if (rc == 1) {
 		fdisk_info(cxt, _("All primary partitions have been defined already."));
 		return -1;
-	case 0:
-		return n;
-	default:
-		return rc;
 	}
+	*partno = rc == 0 ? n : rc;
+	return 0;
 }
 
 static int seek_sector(struct fdisk_context *cxt, sector_t secno)
@@ -1464,8 +1468,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
 			fdisk_warnx(cxt, _("Extended partition already exists."));
 			return -EINVAL;
 		}
-		res = get_partition_unused_primary(cxt, pa);
-		if (res >= 0) {
+		rc = get_partition_unused_primary(cxt, pa, &res);
+		if (rc == 0) {
 			rc = add_partition(cxt, res, pa);
 			goto done;
 		}
@@ -1473,8 +1477,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
 	/* pa specifies start, but outside extended partition */
 	} else if (pa && fdisk_partition_has_start(pa) && l->ext_offset) {
 		DBG(LABEL, ul_debug("DOS: pa template %p: add primary", pa));
-		res = get_partition_unused_primary(cxt, pa);
-		if (res >= 0) {
+		rc = get_partition_unused_primary(cxt, pa, &res);
+		if (rc == 0) {
 			rc = add_partition(cxt, res, pa);
 			goto done;
 		}
@@ -1531,8 +1535,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
 	} else if (cxt->label->nparts_max >= MAXIMUM_PARTS) {
 		fdisk_info(cxt, _("All logical partitions are in use. "
 				  "Adding a primary partition."));
-		res = get_partition_unused_primary(cxt, pa);
-		if (res >= 0)
+		rc = get_partition_unused_primary(cxt, pa, &res);
+		if (rc == 0)
 			rc = add_partition(cxt, res, pa);
 	} else {
 		char hint[BUFSIZ];
@@ -1541,8 +1545,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
 
 		/* the default layout for scripts is to create primary partitions */
 		if (cxt->script) {
-			res = get_partition_unused_primary(cxt, pa);
-			if (res >= 0)
+			rc = get_partition_unused_primary(cxt, pa, &res);
+			if (rc == 0)
 				rc = add_partition(cxt, res, pa);
 			goto done;
 		}
@@ -1573,16 +1577,16 @@ static int dos_add_partition(struct fdisk_context *cxt,
 		fdisk_free_ask(ask);
 
 		if (c == 'p') {
-			res = get_partition_unused_primary(cxt, pa);
-			if (res >= 0)
+			rc = get_partition_unused_primary(cxt, pa, &res);
+			if (rc == 0)
 				rc = add_partition(cxt, res, pa);
 			goto done;
 		} else if (c == 'l' && l->ext_offset) {
 			rc = add_logical(cxt, pa, &res);
 			goto done;
 		} else if (c == 'e' && !l->ext_offset) {
-			res = get_partition_unused_primary(cxt, pa);
-			if (res >= 0) {
+			rc = get_partition_unused_primary(cxt, pa, &res);
+			if (rc == 0) {
 				struct fdisk_partition *xpa = NULL;
 				struct fdisk_parttype *t;
 
@@ -1607,7 +1611,7 @@ static int dos_add_partition(struct fdisk_context *cxt,
 done:
 	if (rc == 0) {
 		cxt->label->nparts_cur++;
-		if (partno && res >= 0)
+		if (partno)
 			*partno = res;
 	}
 	return rc;
-- 
2.1.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] libfdisk: fix get_partition_unused_primary()
  2014-11-14  5:27 [PATCH] libfdisk: fix get_partition_unused_primary() Boris Egorov
@ 2014-11-14  9:11 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2014-11-14  9:11 UTC (permalink / raw)
  To: Boris Egorov; +Cc: util-linux

On Fri, Nov 14, 2014 at 11:27:16AM +0600, Boris Egorov wrote:
> Now function takes address to variable and fills it with partition
> number. Caller treats return value as int and use it appropriately.

Yep, it's better. 

>  static int get_partition_unused_primary(struct fdisk_context *cxt,
...
> +	*partno = rc == 0 ? n : rc;
        ^^^^^^^^^^^
I have replaced this line with

 if (rc == 0)
    *partno = n;

it seems we don't have to mix partno and return codes here at all.

> +	return 0;

 return rc;


 Applied, thanks!

    Karel
    

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-11-14  9:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-14  5:27 [PATCH] libfdisk: fix get_partition_unused_primary() Boris Egorov
2014-11-14  9:11 ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).