]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
chcon: avoid redundant context allocations
authorNamhyung Kim <namhyung@gmail.com>
Mon, 30 Jun 2014 15:12:42 +0000 (00:12 +0900)
committerPádraig Brady <P@draigBrady.com>
Tue, 1 Jul 2014 14:45:53 +0000 (15:45 +0100)
Since context is verified by security_check_context() it can be used in
change_file_context() without converting to context_t every time.

* src/chcon.c (change_file_context): Use specified_context directly.

src/chcon.c

index 8c18167ffb92e6169f233edb7a552b9a89868197..6940cf54eb63863deee383a258134bc1f954ead3 100644 (file)
@@ -141,7 +141,7 @@ static int
 change_file_context (int fd, char const *file)
 {
   security_context_t file_context = NULL;
-  context_t context;
+  context_t context IF_LINT (= NULL);
   security_context_t context_string;
   int errors = 0;
 
@@ -170,17 +170,14 @@ change_file_context (int fd, char const *file)
 
       if (compute_context_from_mask (file_context, &context))
         return 1;
+
+      context_string = context_str (context);
     }
   else
     {
-      /* FIXME: this should be done exactly once, in main.  */
-      context = context_new (specified_context);
-      if (!context)
-        abort ();
+      context_string = specified_context;
     }
 
-  context_string = context_str (context);
-
   if (file_context == NULL || ! STREQ (context_string, file_context))
     {
       int fail = (affect_symlink_referent
@@ -195,8 +192,11 @@ change_file_context (int fd, char const *file)
         }
     }
 
-  context_free (context);
-  freecon (file_context);
+  if (specified_context == NULL)
+    {
+      context_free (context);
+      freecon (file_context);
+    }
 
   return errors;
 }