libselinux: selinux_getenforcemode: remove out2 label
PATCH 1/1 libselinux: selinux_getenforcemode: remove out2 label
The out2 label previously only existed such that free(buf) isn't called if malloc(3) fails to allocate buf. However, posix says[1] that calling free(3) with a nullptr is valid:
Quoted 1 line
If ptr is a null pointer, no action shall occur.
Hence, remove the extra label to simplify the logic.
[1] https://pubs.opengroup.org/onlinepubs/009604499/functions/free.html
--- libselinux/src/compute_create.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
libselinux/src/compute_create.c +6 -7
@@ -81,34 +81,33 @@ int security_compute_create_name_raw(const char * scon,if (len <0 || (size_t )len >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; }if (objname &&object_name_encode (objname, buf + len, size - len) <0 ) { errno =ENAMETOOLONG ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
Signature · 1 line
2.50.1
PATCH v2 1/1 libselinux: remove out2 labels
The out2 label previously only existed such that free(buf) isn't called if malloc(3) fails to allocate buf. However, posix says[1] that calling free(3) with a nullptr is valid:
Quoted 1 line
If ptr is a null pointer, no action shall occur.
Hence, remove the extra labels to simplify the logic.
[1] https://pubs.opengroup.org/onlinepubs/009604499/functions/free.html
--- libselinux/src/canonicalize_context.c | 9 ++++----- libselinux/src/compute_av.c | 11 +++++------ libselinux/src/compute_create.c | 13 ++++++------- libselinux/src/compute_member.c | 11 +++++------ libselinux/src/compute_relabel.c | 11 +++++------ libselinux/src/compute_user.c | 15 +++++++-------- libselinux/src/get_initial_context.c | 7 +++---- libselinux/src/procattr.c | 9 ++++----- 8 files changed, 39 insertions(+), 47 deletions(-)
libselinux/src/canonicalize_context.c +4 -5
@@ -36,12 +36,12 @@ int security_canonicalize_context_raw(const char * con,if (strlcpy (buf, con, size) >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf) +1 );if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );
@@ -54,12 +54,11 @@ int security_canonicalize_context_raw(const char * con, *canoncon =strdup (buf);if (!(*canoncon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/compute_av.c +5 -6
@@ -46,17 +46,17 @@ int security_compute_av_flags_raw(const char * scon,if (ret <0 || (size_t )ret >= len) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , len); ret =read (fd, buf, len -1 );if (ret <0 ) - goto out2; + goto out; ret = sscanf(buf, "%x %x %x %x %u %x", &avd->allowed, &avd->decided,
@@ -64,7 +64,7 @@ int security_compute_av_flags_raw(const char * scon, &avd->seqno, &avd->flags);if (ret <5 ) { ret =-1 ; - goto out2; + goto out; }else if (ret <6 ) avd->flags =0 ;
@@ -79,9 +79,8 @@ int security_compute_av_flags_raw(const char * scon,map_decision (tclass, avd); ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/compute_create.c +6 -7
@@ -81,34 +81,33 @@ int security_compute_create_name_raw(const char * scon,if (len <0 || (size_t )len >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; }if (objname &&object_name_encode (objname, buf + len, size - len) <0 ) { errno =ENAMETOOLONG ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/compute_member.c +5 -6
@@ -41,27 +41,26 @@ int security_compute_member_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/compute_relabel.c +5 -6
@@ -41,27 +41,26 @@ int security_compute_relabel_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!*newcon) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/compute_user.c +7 -8
@@ -43,27 +43,27 @@ int security_compute_user_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out;if (sscanf (buf,"%u" , &nel) !=1 ) { ret =-1 ; - goto out2; + goto out; } ary =malloc ((nel +1 ) *sizeof (char *));if (!ary) { ret =-1 ; - goto out2; + goto out; } ptr = buf +strlen (buf) +1 ;
@@ -72,16 +72,15 @@ int security_compute_user_raw(const char * scon,if (!ary[i]) {freeconary (ary); ret =-1 ; - goto out2; + goto out; } ptr +=strlen (ptr) +1 ; } ary[nel] =NULL ; *con = ary; ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/get_initial_context.c +3 -4
@@ -46,17 +46,16 @@ int security_get_initial_context_raw(const char * name, char ** con) } ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *con =strdup (buf);if (!(*con)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }
libselinux/src/procattr.c +4 -5
@@ -153,22 +153,21 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr, ret =read (fd, buf, size -1 ); }while (ret <0 && errno ==EINTR );if (ret <0 ) - goto out2; + goto out;if (ret ==0 ) { *context =NULL ; - goto out2; + goto out; } *context =strdup (buf);if (!(*context)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf); errno_hold = errno;close (fd); errno = errno_hold;
Signature · 1 line
2.50.1
Re: [PATCH v2] libselinux: remove out2 labels
Quoted 5 lines · Rahul Sandhu
The out2 label previously only existed such that free(buf) isn't called if malloc(3) fails to allocate buf. However, posix says[1] that calling free(3) with a nullptr is valid:
Quoted 7 lines
Quoted 1 line
If ptr is a null pointer, no action shall occur.
Hence, remove the extra labels to simplify the logic.
[1] https://pubs.opengroup.org/onlinepubs/009604499/functions/free.html
Signed-off-by Rahul Sandhu <[email protected]>
Quoted 325 lines
--- libselinux/src/canonicalize_context.c | 9 ++++----- libselinux/src/compute_av.c | 11 +++++------ libselinux/src/compute_create.c | 13 ++++++------- libselinux/src/compute_member.c | 11 +++++------ libselinux/src/compute_relabel.c | 11 +++++------ libselinux/src/compute_user.c | 15 +++++++-------- libselinux/src/get_initial_context.c | 7 +++---- libselinux/src/procattr.c | 9 ++++----- 8 files changed, 39 insertions(+), 47 deletions(-)libselinux/src/canonicalize_context.c +4
-5@@ -36,12 +36,12 @@ int security_canonicalize_context_raw(const char * con,if (strlcpy (buf, con, size) >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf) +1 );if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );@@ -54,12 +54,11 @@ int security_canonicalize_context_raw(const char * con, *canoncon =strdup (buf);if (!(*canoncon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_av.c +5
-6@@ -46,17 +46,17 @@ int security_compute_av_flags_raw(const char * scon,if (ret <0 || (size_t )ret >= len) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , len); ret =read (fd, buf, len -1 );if (ret <0 ) - goto out2; + goto out; ret = sscanf(buf, "%x %x %x %x %u %x", &avd->allowed, &avd->decided,@@ -64,7 +64,7 @@ int security_compute_av_flags_raw(const char * scon, &avd->seqno, &avd->flags);if (ret <5 ) { ret =-1 ; - goto out2; + goto out; }else if (ret <6 ) avd->flags =0 ;@@ -79,9 +79,8 @@ int security_compute_av_flags_raw(const char * scon,map_decision (tclass, avd); ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_create.c +6
-7@@ -81,34 +81,33 @@ int security_compute_create_name_raw(const char * scon,if (len <0 || (size_t )len >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; }if (objname &&object_name_encode (objname, buf + len, size - len) <0 ) { errno =ENAMETOOLONG ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_member.c +5
-6@@ -41,27 +41,26 @@ int security_compute_member_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_relabel.c +5
-6@@ -41,27 +41,26 @@ int security_compute_relabel_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!*newcon) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_user.c +7
-8@@ -43,27 +43,27 @@ int security_compute_user_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out;if (sscanf (buf,"%u" , &nel) !=1 ) { ret =-1 ; - goto out2; + goto out; } ary =malloc ((nel +1 ) *sizeof (char *));if (!ary) { ret =-1 ; - goto out2; + goto out; } ptr = buf +strlen (buf) +1 ;@@ -72,16 +72,15 @@ int security_compute_user_raw(const char * scon,if (!ary[i]) {freeconary (ary); ret =-1 ; - goto out2; + goto out; } ptr +=strlen (ptr) +1 ; } ary[nel] =NULL ; *con = ary; ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/get_initial_context.c +3
-4@@ -46,17 +46,16 @@ int security_get_initial_context_raw(const char * name, char ** con) } ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *con =strdup (buf);if (!(*con)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/procattr.c +4
-6@@ -153,22 +153,21 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr, ret =read (fd, buf, size -1 ); }while (ret <0 && errno ==EINTR );if (ret <0 ) - goto out2; + goto out;if (ret ==0 ) { *context =NULL ; - goto out2; + goto out; } *context =strdup (buf);if (!(*context)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf); errno_hold = errno;close (fd); errno = errno_hold; --2.50.1
Re: [PATCH v2] libselinux: remove out2 labels
Quoted 9 lines · Stephen Smalley
Quoted 7 lines · Rahul Sandhu
The out2 label previously only existed such that free(buf) isn't called if malloc(3) fails to allocate buf. However, posix says[1] that calling free(3) with a nullptr is valid:
Quoted 1 line
If ptr is a null pointer, no action shall occur.
Quoted 7 lines
Quoted 5 lines
Hence, remove the extra labels to simplify the logic.
[1] https://pubs.opengroup.org/onlinepubs/009604499/functions/free.html
Signed-off-by Rahul Sandhu <[email protected]>Acked-by Stephen Smalley <[email protected]>
Thanks, applied.
Quoted 326 lines
Quoted 326 lines
--- libselinux/src/canonicalize_context.c | 9 ++++----- libselinux/src/compute_av.c | 11 +++++------ libselinux/src/compute_create.c | 13 ++++++------- libselinux/src/compute_member.c | 11 +++++------ libselinux/src/compute_relabel.c | 11 +++++------ libselinux/src/compute_user.c | 15 +++++++-------- libselinux/src/get_initial_context.c | 7 +++---- libselinux/src/procattr.c | 9 ++++----- 8 files changed, 39 insertions(+), 47 deletions(-)libselinux/src/canonicalize_context.c +4
-5@@ -36,12 +36,12 @@ int security_canonicalize_context_raw(const char * con,if (strlcpy (buf, con, size) >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf) +1 );if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );@@ -54,12 +54,11 @@ int security_canonicalize_context_raw(const char * con, *canoncon =strdup (buf);if (!(*canoncon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_av.c +5
-6@@ -46,17 +46,17 @@ int security_compute_av_flags_raw(const char * scon,if (ret <0 || (size_t )ret >= len) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , len); ret =read (fd, buf, len -1 );if (ret <0 ) - goto out2; + goto out; ret = sscanf(buf, "%x %x %x %x %u %x", &avd->allowed, &avd->decided,@@ -64,7 +64,7 @@ int security_compute_av_flags_raw(const char * scon, &avd->seqno, &avd->flags);if (ret <5 ) { ret =-1 ; - goto out2; + goto out; }else if (ret <6 ) avd->flags =0 ;@@ -79,9 +79,8 @@ int security_compute_av_flags_raw(const char * scon,map_decision (tclass, avd); ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_create.c +6
-7@@ -81,34 +81,33 @@ int security_compute_create_name_raw(const char * scon,if (len <0 || (size_t )len >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; }if (objname &&object_name_encode (objname, buf + len, size - len) <0 ) { errno =ENAMETOOLONG ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_member.c +5
-6@@ -41,27 +41,26 @@ int security_compute_member_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!(*newcon)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_relabel.c +5
-6@@ -41,27 +41,26 @@ int security_compute_relabel_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *newcon =strdup (buf);if (!*newcon) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/compute_user.c +7
-8@@ -43,27 +43,27 @@ int security_compute_user_raw(const char * scon,if (ret <0 || (size_t )ret >= size) { errno =EOVERFLOW ; ret =-1 ; - goto out2; + goto out; } ret =write (fd, buf,strlen (buf));if (ret <0 ) - goto out2; + goto out;memset (buf,0 , size); ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out;if (sscanf (buf,"%u" , &nel) !=1 ) { ret =-1 ; - goto out2; + goto out; } ary =malloc ((nel +1 ) *sizeof (char *));if (!ary) { ret =-1 ; - goto out2; + goto out; } ptr = buf +strlen (buf) +1 ;@@ -72,16 +72,15 @@ int security_compute_user_raw(const char * scon,if (!ary[i]) {freeconary (ary); ret =-1 ; - goto out2; + goto out; } ptr +=strlen (ptr) +1 ; } ary[nel] =NULL ; *con = ary; ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/get_initial_context.c +3
-4@@ -46,17 +46,16 @@ int security_get_initial_context_raw(const char * name, char ** con) } ret =read (fd, buf, size -1 );if (ret <0 ) - goto out2; + goto out; *con =strdup (buf);if (!(*con)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf);close (fd);return ret; }libselinux/src/procattr.c +4
-6@@ -153,22 +153,21 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr, ret =read (fd, buf, size -1 ); }while (ret <0 && errno ==EINTR );if (ret <0 ) - goto out2; + goto out;if (ret ==0 ) { *context =NULL ; - goto out2; + goto out; } *context =strdup (buf);if (!(*context)) { ret =-1 ; - goto out2; + goto out; } ret =0 ; - out2: -free (buf); out: +free (buf); errno_hold = errno;close (fd); errno = errno_hold; --2.50.1