Module Name:    src
Committed By:   mlelstv
Date:           Mon Mar 19 09:06:21 UTC 2018

Modified Files:
        src/sbin/gpt: gpt.8 gpt.h main.c set.c type.c unset.c

Log Message:
Check device parameter to avoid segfaults. Agument synopsis for -l option.


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/sbin/gpt/gpt.8
cvs rdiff -u -r1.36 -r1.37 src/sbin/gpt/gpt.h
cvs rdiff -u -r1.10 -r1.11 src/sbin/gpt/main.c
cvs rdiff -u -r1.13 -r1.14 src/sbin/gpt/set.c src/sbin/gpt/type.c \
    src/sbin/gpt/unset.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/gpt/gpt.8
diff -u src/sbin/gpt/gpt.8:1.57 src/sbin/gpt/gpt.8:1.58
--- src/sbin/gpt/gpt.8:1.57	Mon Jan 15 12:20:47 2018
+++ src/sbin/gpt/gpt.8	Mon Mar 19 09:06:20 2018
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.57 2018/01/15 12:20:47 sborrill Exp $
+.\" $NetBSD: gpt.8,v 1.58 2018/03/19 09:06:20 mlelstv Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -41,6 +41,15 @@
 .Ar command
 .Op Ar command_options
 .Ar device
+.Nm
+.Ar set
+.Fl l
+.Nm
+.Ar unset
+.Fl l
+.Nm
+.Ar type
+.Fl l
 .Sh DESCRIPTION
 The
 .Nm

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.36 src/sbin/gpt/gpt.h:1.37
--- src/sbin/gpt/gpt.h:1.36	Wed Sep  6 18:17:18 2017
+++ src/sbin/gpt/gpt.h	Mon Mar 19 09:06:20 2018
@@ -81,6 +81,7 @@ gpt_t	gpt_open(const char *, int, int, o
 #define GPT_NOSYNC	0x08
 #define GPT_FILE	0x10
 #define GPT_TIMESTAMP	0x20
+#define GPT_OPTDEV      0x8000
 
 void*	gpt_read(gpt_t, off_t, size_t);
 off_t	gpt_last(gpt_t);

Index: src/sbin/gpt/main.c
diff -u src/sbin/gpt/main.c:1.10 src/sbin/gpt/main.c:1.11
--- src/sbin/gpt/main.c:1.10	Thu Feb 16 22:40:19 2017
+++ src/sbin/gpt/main.c	Mon Mar 19 09:06:20 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.10 2017/02/16 22:40:19 christos Exp $	*/
+/*	$NetBSD: main.c,v 1.11 2018/03/19 09:06:20 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2002 Marcel Moolenaar
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: main.c,v 1.10 2017/02/16 22:40:19 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.11 2018/03/19 09:06:20 mlelstv Exp $");
 #endif
 
 #include <stdio.h>
@@ -241,6 +241,9 @@ main(int argc, char *argv[])
 		if (gpt == NULL)
 			return EXIT_FAILURE;
 	} else {
+		if ((cmdsw[i]->flags & GPT_OPTDEV) == 0)
+			errx(EXIT_FAILURE,
+			     "Command %s needs a device parameter", cmd);
 		argc++;
 		gpt = NULL;
 	}

Index: src/sbin/gpt/set.c
diff -u src/sbin/gpt/set.c:1.13 src/sbin/gpt/set.c:1.14
--- src/sbin/gpt/set.c:1.13	Tue Dec 29 16:45:04 2015
+++ src/sbin/gpt/set.c	Mon Mar 19 09:06:20 2018
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: set.c,v 1.13 2015/12/29 16:45:04 christos Exp $");
+__RCSID("$NetBSD: set.c,v 1.14 2018/03/19 09:06:20 mlelstv Exp $");
 #endif
 
 #include <sys/types.h>
@@ -60,7 +60,7 @@ struct gpt_cmd c_set = {
 	"set",
 	cmd_set,
 	sethelp, __arraycount(sethelp),
-	0,
+	GPT_OPTDEV,
 };
 
 #define usage() gpt_usage(NULL, &c_set)
@@ -75,11 +75,11 @@ cmd_set(gpt_t gpt, int argc, char *argv[
 	while ((ch = getopt(argc, argv, "a:i:l")) != -1) {
 		switch(ch) {
 		case 'a':
-			if (gpt_attr_get(gpt, &attributes) == -1)
+			if (gpt == NULL || gpt_attr_get(gpt, &attributes) == -1)
 				return usage();
 			break;
 		case 'i':
-			if (gpt_uint_get(gpt, &entry) == -1)
+			if (gpt == NULL || gpt_uint_get(gpt, &entry) == -1)
 				return usage();
 			break;
 		case 'l':
@@ -90,7 +90,7 @@ cmd_set(gpt_t gpt, int argc, char *argv[
 		}
 	}
 
-	if (argc != optind)
+	if (gpt == NULL || argc != optind)
 		return usage();
 
 	return gpt_attr_update(gpt, entry, attributes, 0);
Index: src/sbin/gpt/type.c
diff -u src/sbin/gpt/type.c:1.13 src/sbin/gpt/type.c:1.14
--- src/sbin/gpt/type.c:1.13	Sun Dec  6 00:39:26 2015
+++ src/sbin/gpt/type.c	Mon Mar 19 09:06:20 2018
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: type.c,v 1.13 2015/12/06 00:39:26 christos Exp $");
+__RCSID("$NetBSD: type.c,v 1.14 2018/03/19 09:06:20 mlelstv Exp $");
 #endif
 
 #include <sys/types.h>
@@ -61,7 +61,7 @@ struct gpt_cmd c_type = {
 	"type",
 	cmd_type,
 	typehelp, __arraycount(typehelp),
-	0,
+	GPT_OPTDEV,
 };
 
 #define usage() gpt_usage(NULL, &c_type)
@@ -91,17 +91,17 @@ cmd_type(gpt_t gpt, int argc, char *argv
 			gpt_uuid_help("\t");
 			return 0;
 		case 'T':
-			if (gpt_uuid_get(gpt, &newtype) == -1)
+			if (gpt == NULL || gpt_uuid_get(gpt, &newtype) == -1)
 				return -1;
 			break;
 		default:
-			if (gpt_add_find(gpt, &find, ch) == -1)
+			if (gpt == NULL || gpt_add_find(gpt, &find, ch) == -1)
 				return usage();
 			break;
 		}
 	}
 
-	if (gpt_uuid_is_nil(newtype) || argc != optind)
+	if (gpt == NULL || gpt_uuid_is_nil(newtype) || argc != optind)
 		return usage();
 
 	return gpt_change_ent(gpt, &find, change, &newtype);
Index: src/sbin/gpt/unset.c
diff -u src/sbin/gpt/unset.c:1.13 src/sbin/gpt/unset.c:1.14
--- src/sbin/gpt/unset.c:1.13	Tue Dec 29 16:45:04 2015
+++ src/sbin/gpt/unset.c	Mon Mar 19 09:06:20 2018
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: unset.c,v 1.13 2015/12/29 16:45:04 christos Exp $");
+__RCSID("$NetBSD: unset.c,v 1.14 2018/03/19 09:06:20 mlelstv Exp $");
 #endif
 
 #include <sys/types.h>
@@ -53,13 +53,14 @@ static int cmd_unset(gpt_t, int, char *[
 
 static const char *unsethelp[] = {
 	"-a attribute -i index",
+	"-l",
 };
 
 struct gpt_cmd c_unset = {
 	"unset",
 	cmd_unset,
 	unsethelp, __arraycount(unsethelp),
-	0,
+	GPT_OPTDEV,
 };
 
 #define usage() gpt_usage(NULL, &c_unset)
@@ -74,11 +75,11 @@ cmd_unset(gpt_t gpt, int argc, char *arg
 	while ((ch = getopt(argc, argv, "a:i:l")) != -1) {
 		switch(ch) {
 		case 'a':
-			if (gpt_attr_get(gpt, &attributes) == -1)
+			if (gpt == NULL || gpt_attr_get(gpt, &attributes) == -1)
 				return usage();
 			break;
 		case 'i':
-			if (gpt_uint_get(gpt, &entry) == -1)
+			if (gpt == NULL || gpt_uint_get(gpt, &entry) == -1)
 				return usage();
 			break;
 		case 'l':
@@ -89,7 +90,7 @@ cmd_unset(gpt_t gpt, int argc, char *arg
 		}
 	}
 
-	if (argc != optind)
+	if (gpt == NULL || argc != optind)
 		return usage();
 
 	return gpt_attr_update(gpt, entry, 0, attributes);

Reply via email to