Create format strings for scanf statically.
authorRafal Krypa <r.krypa@samsung.com>
Fri, 26 Jul 2013 10:36:08 +0000 (12:36 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Mon, 19 Aug 2013 12:13:59 +0000 (14:13 +0200)
[Issue#]       SSDWSSP-372
[Feature]      Remove unneeded memory allocations for scanf format strings.
[Cause]        When reading Smack rules, fields length must be checked.
[Solution]     Create format strings with legnth specifiers at build time.
[Verification] Build, install, run tests.

Change-Id: Ib0b20e3d46fe0d4af957f13a37627d14831283d2

src/access-db.c
src/privilege-control.c

index 68b9bf9..ec6c2a4 100644 (file)
@@ -120,18 +120,11 @@ static int add_id_to_database_internal(const char * id, db_app_type_t app_type)
 static int get_all_ids_internal (char *** ids, int * len, db_app_type_t app_type)
 {
        int ret;
-       char* scanf_label_format AUTO_FREE;
        FILE* file_db AUTO_FCLOSE;
        const char* db_file_name = db_file_names[app_type];
        char smack_label[SMACK_LABEL_LEN + 1];
        element_t* begin_of_list = NULL;
 
-       if (asprintf(&scanf_label_format, "%%%ds\\n", SMACK_LABEL_LEN) < 0) {
-               C_LOGE("Error while creating scanf input label format");
-               ret = PC_ERR_MEM_OPERATION;
-               goto out;
-       }
-
        file_db = fopen(db_file_name, "r");
        if (NULL == file_db) {
                SECURE_SLOGE("Error while opening database file: %s", db_file_name);
@@ -153,7 +146,7 @@ static int get_all_ids_internal (char *** ids, int * len, db_app_type_t app_type
 
        // reading from file ("database")
        // notice that first element always stays with empty "value"
-       while (fscanf(file_db, scanf_label_format, smack_label) == 1) {
+       while (fscanf(file_db, "%" TOSTRING(SMACK_LABEL_LEN) "s\n", smack_label) == 1) {
                smack_label[SMACK_LABEL_LEN] = '\0';
                if (!smack_label_is_valid(smack_label)) {
                        C_LOGD("Found entry in database, but it's not correct SMACK label: \"%s\"", smack_label);
index bd990b1..488f0f5 100644 (file)
@@ -728,18 +728,11 @@ static int perm_to_smack_from_file(struct smack_accesses* smack,
 {
        C_LOGD("Enter function: %s", __func__);
 
-       char* format_string AUTO_FREE;
        char smack_subject[SMACK_LABEL_LEN + 1];
        char smack_object[SMACK_LABEL_LEN + 1];
-       char smack_accesses[10];
+       char smack_accesses[ACC_LEN + 1];
        FILE* file AUTO_FCLOSE;
 
-       if (asprintf(&format_string,"%%%ds %%%ds %%%lus\n",
-                       SMACK_LABEL_LEN, SMACK_LABEL_LEN, (unsigned long)sizeof(smack_accesses)) == -1) {
-               C_LOGE("asprintf failed");
-               return PC_ERR_MEM_OPERATION;
-       }
-
        file = fopen(rules_file_path, "r");
        C_LOGD("path = %s", rules_file_path);
        if (file == NULL) {
@@ -747,7 +740,11 @@ static int perm_to_smack_from_file(struct smack_accesses* smack,
                return PC_OPERATION_SUCCESS;
        }
 
-       while (fscanf(file, format_string, smack_subject, smack_object, smack_accesses) == 3) {
+       while (fscanf(file,
+                       "%" TOSTRING(SMACK_LABEL_LEN) "s "
+                       "%" TOSTRING(SMACK_LABEL_LEN) "s "
+                       "%" TOSTRING(ACC_LEN) "s\n",
+                       smack_subject, smack_object, smack_accesses) == 3) {
                if (!strcmp(smack_subject, app_label_template))
                        strcpy(smack_subject, app_label);