libselinux: simplify string copying
PATCH 1/3 libselinux: simplify string copying
Use strdup(3)/strndup(3) instead of allocating memory and then manually copying the content.
Signed-off-by Christian Göttsche <[email protected]>
--- libselinux/src/context.c | 11 +++++------ libselinux/src/get_default_type.c | 3 +-- libselinux/src/matchpathcon.c | 9 +++------ libselinux/utils/selabel_lookup_best_match.c | 10 ++++------ 4 files changed, 13 insertions(+), 20 deletions(-)
libselinux/src/context.c +5 -6
@@ -149,19 +149,18 @@ static int set_comp(context_private_t * n, int idx, const char *str)char *t =NULL ;const char *p;if (str) { - t = (char *)malloc (strlen (str) +1 ); -if (!t) { -return -1 ; - }for (p = str; *p; p++) {if (*p =='\t' || *p =='\n' || *p =='\r' || ((*p ==':' || *p ==' ' ) && idx !=COMP_RANGE )) { -free (t); errno =EINVAL ;return -1 ; } } -strcpy (t, str); + + t =strdup (str); +if (!t) { +return -1 ; + } }conditional_free (&n->component[idx]); n->component[idx] = t;
libselinux/src/get_default_type.c +1 -2
@@ -62,10 +62,9 @@ static int find_default_type(FILE * fp, const char *role, char **type)return -1 ; } - t =malloc (strlen (buf) - len); + t =strndup (ptr,strlen (buf) - len -1 );if (!t)return -1 ; -strcpy (t, ptr); *type = t;return 0 ; }
libselinux/src/matchpathcon.c +3 -6
@@ -215,10 +215,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)if (ret <0 || sb.st_ino != ino) { fl->specind = specind;free (fl->file); - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom; -strcpy (fl->file, file);return fl->specind; }
@@ -232,10 +231,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) __FUNCTION__, file, fl->file, con_array[fl->specind]);free (fl->file); - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom; -strcpy (fl->file, file);return fl->specind; }
@@ -248,10 +246,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) goto oom; fl->ino = ino; fl->specind = specind; - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom_freefl; -strcpy (fl->file, file); fl->next = prevfl->next; prevfl->next = fl;return fl->specind;
libselinux/utils/selabel_lookup_best_match.c +4 -6
@@ -30,7 +30,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)exit (1 ); } -static mode_t string_to_mode (char *s) +static mode_t string_to_mode (const char *s) {switch (s[0 ]) {case 'b' :
@@ -53,7 +53,7 @@ static mode_t string_to_mode(char *s)int main (int argc,char **argv) { -int raw =0 , mode =0 , rc, opt, i, num_links, string_len; +int raw =0 , mode =0 , rc, opt, i, num_links;char *validate =NULL , *path =NULL , *context =NULL , *file =NULL ;char **links =NULL ;
@@ -101,13 +101,11 @@ int main(int argc, char **argv) }for (i = optind, num_links =0 ; i < argc; i++, num_links++) { - string_len =strlen (argv[i]) +1 ; - links[num_links] =malloc (string_len); + links[num_links] =strdup (argv[i]);if (!links[num_links]) { -fprintf (stderr,"ERROR: malloc failed.\n" ); +fprintf (stderr,"ERROR: strdup failed.\n" );exit (1 ); } -strcpy (links[num_links], argv[i]); } }
Signature · 1 line
2.38.1
PATCH 2/3 checkpolicy: simplify string copying
Use strdup(3) instead of allocating memory and then manually copying the content.
Signed-off-by Christian Göttsche <[email protected]>
--- checkpolicy/checkpolicy.c | 10 ++++------ checkpolicy/policy_define.c | 3 +-- checkpolicy/test/dispol.c | 5 ++--- 3 files changed, 7 insertions(+), 11 deletions(-)
checkpolicy/checkpolicy.c +4 -6
@@ -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);
checkpolicy/policy_define.c +1 -2
@@ -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
checkpolicy/test/dispol.c +2 -3
@@ -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 ) {
Signature · 1 line
2.38.1
PATCH 3/3 libsepol: simplify string copying
In reply to Christian GöttscheUse strdup(3) instead of allocating memory and then manually copying the content.
Signed-off-by Christian Göttsche <[email protected]>
--- libsepol/src/policydb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
libsepol/src/policydb.c +1 -2
@@ -776,12 +776,11 @@ static int roles_init(policydb_t * p) rc = -ENOMEM ; goto out; } - key =malloc (strlen (OBJECT_R ) +1 ); + key =strdup (OBJECT_R );if (!key) { rc = -ENOMEM ; goto out_free_role; } -strcpy (key,OBJECT_R ); rc = symtab_insert(p, SYM_ROLES, key, role, (p->policy_type == POLICY_MOD ? SCOPE_REQ : SCOPE_DECL), 1,
Signature · 1 line
2.38.1
Quoted 5 lines · Christian Göttsche
Use strdup(3)/strndup(3) instead of allocating memory and then manually copying the content.
Signed-off-by Christian Göttsche <[email protected]>
For these three patches:
Acked-by James Carter <[email protected]>
Quoted 133 lines
--- libselinux/src/context.c | 11 +++++------ libselinux/src/get_default_type.c | 3 +-- libselinux/src/matchpathcon.c | 9 +++------ libselinux/utils/selabel_lookup_best_match.c | 10 ++++------ 4 files changed, 13 insertions(+), 20 deletions(-)libselinux/src/context.c +5
-6@@ -149,19 +149,18 @@ static int set_comp(context_private_t * n, int idx, const char *str)char *t =NULL ;const char *p;if (str) { - t = (char *)malloc (strlen (str) +1 ); -if (!t) { -return -1 ; - }for (p = str; *p; p++) {if (*p =='\t' || *p =='\n' || *p =='\r' || ((*p ==':' || *p ==' ' ) && idx !=COMP_RANGE )) { -free (t); errno =EINVAL ;return -1 ; } } -strcpy (t, str); + + t =strdup (str); +if (!t) { +return -1 ; + } }conditional_free (&n->component[idx]); n->component[idx] = t;libselinux/src/get_default_type.c +1
-2@@ -62,10 +62,9 @@ static int find_default_type(FILE * fp, const char *role, char **type)return -1 ; } - t =malloc (strlen (buf) - len); + t =strndup (ptr,strlen (buf) - len -1 );if (!t)return -1 ; -strcpy (t, ptr); *type = t;return 0 ; }libselinux/src/matchpathcon.c +3
-6@@ -215,10 +215,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)if (ret <0 || sb.st_ino != ino) { fl->specind = specind;free (fl->file); - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom; -strcpy (fl->file, file);return fl->specind; }@@ -232,10 +231,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) __FUNCTION__, file, fl->file, con_array[fl->specind]);free (fl->file); - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom; -strcpy (fl->file, file);return fl->specind; }@@ -248,10 +246,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) goto oom; fl->ino = ino; fl->specind = specind; - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom_freefl; -strcpy (fl->file, file); fl->next = prevfl->next; prevfl->next = fl;return fl->specind;libselinux/utils/selabel_lookup_best_match.c +4
-7@@ -30,7 +30,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)exit (1 ); } -static mode_t string_to_mode (char *s) +static mode_t string_to_mode (const char *s) {switch (s[0 ]) {case 'b' :@@ -53,7 +53,7 @@ static mode_t string_to_mode(char *s)int main (int argc,char **argv) { -int raw =0 , mode =0 , rc, opt, i, num_links, string_len; +int raw =0 , mode =0 , rc, opt, i, num_links;char *validate =NULL , *path =NULL , *context =NULL , *file =NULL ;char **links =NULL ;@@ -101,13 +101,11 @@ int main(int argc, char **argv) }for (i = optind, num_links =0 ; i < argc; i++, num_links++) { - string_len =strlen (argv[i]) +1 ; - links[num_links] =malloc (string_len); + links[num_links] =strdup (argv[i]);if (!links[num_links]) { -fprintf (stderr,"ERROR: malloc failed.\n" ); +fprintf (stderr,"ERROR: strdup failed.\n" );exit (1 ); } -strcpy (links[num_links], argv[i]); } } --2.38.1
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 8 lines
Quoted 4 lines
Use strdup(3)/strndup(3) instead of allocating memory and then manually copying the content.
Signed-off-by Christian Göttsche <[email protected]>For these three patches:
Acked-by James Carter <[email protected]>
These three patches have been merged.
Thanks,
Jim
Quoted 133 lines
Quoted 133 lines
--- libselinux/src/context.c | 11 +++++------ libselinux/src/get_default_type.c | 3 +-- libselinux/src/matchpathcon.c | 9 +++------ libselinux/utils/selabel_lookup_best_match.c | 10 ++++------ 4 files changed, 13 insertions(+), 20 deletions(-)libselinux/src/context.c +5
-6@@ -149,19 +149,18 @@ static int set_comp(context_private_t * n, int idx, const char *str)char *t =NULL ;const char *p;if (str) { - t = (char *)malloc (strlen (str) +1 ); -if (!t) { -return -1 ; - }for (p = str; *p; p++) {if (*p =='\t' || *p =='\n' || *p =='\r' || ((*p ==':' || *p ==' ' ) && idx !=COMP_RANGE )) { -free (t); errno =EINVAL ;return -1 ; } } -strcpy (t, str); + + t =strdup (str); +if (!t) { +return -1 ; + } }conditional_free (&n->component[idx]); n->component[idx] = t;libselinux/src/get_default_type.c +1
-2@@ -62,10 +62,9 @@ static int find_default_type(FILE * fp, const char *role, char **type)return -1 ; } - t =malloc (strlen (buf) - len); + t =strndup (ptr,strlen (buf) - len -1 );if (!t)return -1 ; -strcpy (t, ptr); *type = t;return 0 ; }libselinux/src/matchpathcon.c +3
-6@@ -215,10 +215,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file)if (ret <0 || sb.st_ino != ino) { fl->specind = specind;free (fl->file); - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom; -strcpy (fl->file, file);return fl->specind; }@@ -232,10 +231,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) __FUNCTION__, file, fl->file, con_array[fl->specind]);free (fl->file); - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom; -strcpy (fl->file, file);return fl->specind; }@@ -248,10 +246,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) goto oom; fl->ino = ino; fl->specind = specind; - fl->file =malloc (strlen (file) +1 ); + fl->file =strdup (file);if (!fl->file) goto oom_freefl; -strcpy (fl->file, file); fl->next = prevfl->next; prevfl->next = fl;return fl->specind;libselinux/utils/selabel_lookup_best_match.c +4
-7@@ -30,7 +30,7 @@ static __attribute__ ((__noreturn__)) void usage(const char *progname)exit (1 ); } -static mode_t string_to_mode (char *s) +static mode_t string_to_mode (const char *s) {switch (s[0 ]) {case 'b' :@@ -53,7 +53,7 @@ static mode_t string_to_mode(char *s)int main (int argc,char **argv) { -int raw =0 , mode =0 , rc, opt, i, num_links, string_len; +int raw =0 , mode =0 , rc, opt, i, num_links;char *validate =NULL , *path =NULL , *context =NULL , *file =NULL ;char **links =NULL ;@@ -101,13 +101,11 @@ int main(int argc, char **argv) }for (i = optind, num_links =0 ; i < argc; i++, num_links++) { - string_len =strlen (argv[i]) +1 ; - links[num_links] =malloc (string_len); + links[num_links] =strdup (argv[i]);if (!links[num_links]) { -fprintf (stderr,"ERROR: malloc failed.\n" ); +fprintf (stderr,"ERROR: strdup failed.\n" );exit (1 ); } -strcpy (links[num_links], argv[i]); } } --2.38.1