amdgpu: Simplify error handling in parse_one_line

* Move empty/commented line check before the strdup and return -EAGAIN
  directly
* Initialize r = -EAGAIN and remove redundant assignments
* Set r = -ENOMEM if last strdup fails, and remove redundant goto

Acked-by: Slava Abramov <slava.abramov@amd.com>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
main
Michel Dänzer 2017-12-01 16:59:38 +01:00 committed by Michel Dänzer
parent 85c6b0b00a
commit 5219809a32
1 changed files with 16 additions and 29 deletions

View File

@ -45,63 +45,50 @@ static int parse_one_line(const char *line, struct amdgpu_asic_id *id)
char *s_rid; char *s_rid;
char *s_name; char *s_name;
char *endptr; char *endptr;
int r = 0; int r = -EINVAL;
/* ignore empty line and commented line */
if (strlen(line) == 0 || line[0] == '#')
return -EAGAIN;
buf = strdup(line); buf = strdup(line);
if (!buf) if (!buf)
return -ENOMEM; return -ENOMEM;
/* ignore empty line and commented line */
if (strlen(line) == 0 || line[0] == '#') {
r = -EAGAIN;
goto out;
}
/* device id */ /* device id */
s_did = strtok_r(buf, ",", &saveptr); s_did = strtok_r(buf, ",", &saveptr);
if (!s_did) { if (!s_did)
r = -EINVAL;
goto out; goto out;
}
id->did = strtol(s_did, &endptr, 16); id->did = strtol(s_did, &endptr, 16);
if (*endptr) { if (*endptr)
r = -EINVAL;
goto out; goto out;
}
/* revision id */ /* revision id */
s_rid = strtok_r(NULL, ",", &saveptr); s_rid = strtok_r(NULL, ",", &saveptr);
if (!s_rid) { if (!s_rid)
r = -EINVAL;
goto out; goto out;
}
id->rid = strtol(s_rid, &endptr, 16); id->rid = strtol(s_rid, &endptr, 16);
if (*endptr) { if (*endptr)
r = -EINVAL;
goto out; goto out;
}
/* marketing name */ /* marketing name */
s_name = strtok_r(NULL, ",", &saveptr); s_name = strtok_r(NULL, ",", &saveptr);
if (!s_name) { if (!s_name)
r = -EINVAL;
goto out; goto out;
}
/* trim leading whitespaces or tabs */ /* trim leading whitespaces or tabs */
while (isblank(*s_name)) while (isblank(*s_name))
s_name++; s_name++;
if (strlen(s_name) == 0) { if (strlen(s_name) == 0)
r = -EINVAL;
goto out; goto out;
}
id->marketing_name = strdup(s_name); id->marketing_name = strdup(s_name);
if (id->marketing_name == NULL) { if (id->marketing_name)
r = -EINVAL; r = 0;
goto out; else
} r = -ENOMEM;
out: out:
free(buf); free(buf);