]> gitweb.michael.orlitzky.com - apply-default-acl.git/commitdiff
stc/libadacl.c: use a "cleanup" routine in acl_update_entry().
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 11 Dec 2018 05:16:21 +0000 (00:16 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 11 Dec 2018 20:14:29 +0000 (15:14 -0500)
This is in preparation for comparing the qualifiers of the given and
existing ACL entries. Since acl_get_qualifier() can allocate memory,
we need to be sure that memory gets freed, even if an error occurs. A
"cleanup" routine and liberal use of "goto" is the standard pattern
throughout the rest of the library to deal with that problem.

src/libadacl.c

index 4ca60c0c0d972018486bc725c17752725a07ebeb..5bca89fe405dc1a030bdab0ef4ccd55b8d5eab71 100644 (file)
@@ -259,39 +259,47 @@ int acl_update_entry(acl_t aclp, acl_entry_t entry) {
     return ACL_ERROR;
   }
 
+  /* Our return value. Default to failure, and change to success if we
+     actually update something. */
+  int result = ACL_FAILURE;
+
   acl_entry_t existing_entry;
   /* Loop through the given ACL looking for matching entries. */
-  int result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry);
+  int get_entry_result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry);
 
-  while (result == ACL_SUCCESS) {
+  while (get_entry_result == ACL_SUCCESS) {
     acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
 
     if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) {
        perror("set_acl_tag_permset (acl_get_tag_type)");
-       return ACL_ERROR;
+       result = ACL_ERROR;
+       goto cleanup;
     }
 
     if (existing_tag == entry_tag) {
       /* If we update something, we're done and return ACL_SUCCESS */
       if (acl_set_permset(existing_entry, entry_permset) == ACL_ERROR) {
-        perror("acl_update_entry (acl_set_permset)");
-        return ACL_ERROR;
+       perror("acl_update_entry (acl_set_permset)");
+       result = ACL_ERROR;
+       goto cleanup;
       }
 
-      return ACL_SUCCESS;
+      result = ACL_SUCCESS;
+      goto cleanup;
     }
 
-    result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry);
+    get_entry_result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry);
   }
 
   /* This catches both the initial acl_get_entry and the ones at the
      end of the loop. */
-  if (result == ACL_ERROR) {
+  if (get_entry_result == ACL_ERROR) {
     perror("acl_update_entry (acl_get_entry)");
-    return ACL_ERROR;
+    result = ACL_ERROR;
   }
 
-  return ACL_FAILURE;
+ cleanup:
+  return result;
 }