Add error checking for poorly-formed crossystem args

R=petkov@chromium.org
BUG=chromium-os:13322
TEST=manual

The following command lines should cause crossystem to fail with a
warning about a poorly formed parameter:
  crossystem ''
  crossystem '=cros_debug'
  crossystem '?cros_debug'

The following command line should warn that you can't use both = and ?:
  crossystem cros_debug?=0
(that is, it warns, not just compares with '=0')

The following should print 'UNEQUAL'
  crossystem cros_debug? || echo UNEQUAL
(because it's comparing cros_debug with an empty string)

Review URL: http://codereview.chromium.org/6718012

Change-Id: I2e2851515f4914b16aba64065600fb92d9ad1a63
This commit is contained in:
Randall Spangler
2011-03-21 16:58:54 -07:00
parent 7141571d55
commit f27583f083

View File

@@ -99,6 +99,8 @@ void PrintHelp(const char *progname) {
* Returns the parameter, or NULL if no match. */
const Param* FindParam(const char* name) {
const Param* p;
if (!name)
return NULL;
for (p = sys_param_list; p->name; p++) {
if (!strcasecmp(p->name, name))
return p;
@@ -220,22 +222,34 @@ int main(int argc, char* argv[]) {
/* Otherwise, loop through params and get/set them */
for (i = 1; i < argc && retval == 0; i++) {
int has_set = (NULL != strchr(argv[i], '='));
int has_expect = (NULL != strchr(argv[i], '?'));
char* has_set = strchr(argv[i], '=');
char* has_expect = strchr(argv[i], '?');
char* name = strtok(argv[i], "=?");
char* value = strtok(NULL, "=?");
const Param* p = FindParam(name);
if (!p) {
fprintf(stderr, "Invalid parameter name: %s\n", name);
const Param* p;
/* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */
if (!name || has_set == argv[i] || has_expect == argv[i]) {
fprintf(stderr, "Poorly formed parameter\n");
PrintHelp(progname);
return 1;
}
if (!value)
value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */
if (has_set && has_expect) {
fprintf(stderr, "Use either = or ? in a parameter, but not both.\n");
PrintHelp(progname);
return 1;
}
/* Find the parameter */
p = FindParam(name);
if (!p) {
fprintf(stderr, "Invalid parameter name: %s\n", name);
PrintHelp(progname);
return 1;
}
if (i > 1)
printf(" "); /* Output params space-delimited */
if (has_set)