checkpolicy: simplify string copying

Use strdup(3) instead of allocating memory and then manually copying the
content.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2022-11-09 21:09:38 +01:00 committed by James Carter
parent d3c6828a6a
commit aaaed69911
3 changed files with 7 additions and 11 deletions

View file

@ -1148,12 +1148,11 @@ int main(int argc, char **argv)
FGETS(ans, sizeof(ans), stdin);
ans[strlen(ans) - 1] = 0;
name = malloc((strlen(ans) + 1) * sizeof(char));
name = strdup(ans);
if (name == NULL) {
fprintf(stderr, "couldn't malloc string.\n");
fprintf(stderr, "couldn't strdup string.\n");
break;
}
strcpy(name, ans);
printf("state? ");
FGETS(ans, sizeof(ans), stdin);
@ -1296,12 +1295,11 @@ int main(int argc, char **argv)
FGETS(ans, sizeof(ans), stdin);
ans[strlen(ans) - 1] = 0;
name = malloc((strlen(ans) + 1) * sizeof(char));
name = strdup(ans);
if (!name) {
fprintf(stderr, "couldn't malloc string.\n");
fprintf(stderr, "couldn't strdup string.\n");
break;
}
strcpy(name, ans);
printf("port? ");
FGETS(ans, sizeof(ans), stdin);

View file

@ -117,12 +117,11 @@ int insert_id(const char *id, int push)
char *newid = 0;
int error;
newid = (char *)malloc(strlen(id) + 1);
newid = strdup(id);
if (!newid) {
yyerror("out of memory");
return -1;
}
strcpy(newid, id);
if (push)
error = queue_push(id_queue, (queue_element_t) newid);
else

View file

@ -486,12 +486,11 @@ int main(int argc, char **argv)
}
ans[strlen(ans) - 1] = 0;
name = malloc((strlen(ans) + 1) * sizeof(char));
name = strdup(ans);
if (name == NULL) {
fprintf(stderr, "couldn't malloc string.\n");
fprintf(stderr, "couldn't strdup string.\n");
break;
}
strcpy(name, ans);
printf("state? ");
if (fgets(ans, sizeof(ans), stdin) == NULL) {