Fix error check for fread
[platform/core/uifw/stt.git] / client / stt.c
index 178f26c..2035dea 100644 (file)
@@ -100,15 +100,15 @@ static bool __check_privilege(const char* uid, const char * privilege)
        char smack_label[1024] = {'\0',};
 
        if (!p_cynara) {
-           return false;
+               return false;
        }
 
        fp = fopen(label_path, "r");
        if (fp != NULL) {
-           if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
-               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread");
+               if (sizeof(smack_label) != fread(smack_label, 1, sizeof(smack_label), fp))
+                       SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread");
 
-           fclose(fp);
+               fclose(fp);
        }
 
        pid_t pid = getpid();
@@ -121,7 +121,7 @@ static bool __check_privilege(const char* uid, const char * privilege)
        }
 
        if (ret != CYNARA_API_ACCESS_ALLOWED)
-           return false;
+               return false;
        return true;
 }
 
@@ -1633,8 +1633,15 @@ int stt_start(stt_h stt, const char* language, const char* type)
                temp = strdup(language);
        }
 
+       if (NULL == temp) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
+               return STT_ERROR_OUT_OF_MEMORY;
+       }
+
        if (true == client->credential_needed && NULL == client->credential) {
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
+               free(temp);
+               temp = NULL;
                return STT_ERROR_PERMISSION_DENIED;
        }
 
@@ -1647,7 +1654,8 @@ int stt_start(stt_h stt, const char* language, const char* type)
                SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
        }
 
-       if (NULL != temp)       free(temp);
+       free(temp);
+       temp = NULL;
 
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
        SLOG(LOG_DEBUG, TAG_STTC, " ");
@@ -2543,8 +2551,15 @@ int stt_start_file(stt_h stt, const char* language, const char* type, const char
                temp = strdup(language);
        }
 
+       if (NULL == temp) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
+               return STT_ERROR_OUT_OF_MEMORY;
+       }
+
        if (true == client->credential_needed && NULL == client->credential) {
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
+               free(temp);
+               temp = NULL;
                return STT_ERROR_PERMISSION_DENIED;
        }
 
@@ -2557,7 +2572,8 @@ int stt_start_file(stt_h stt, const char* language, const char* type, const char
                SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
        }
 
-       if (NULL != temp)       free(temp);
+       free(temp);
+       temp = NULL;
 
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
        SLOG(LOG_DEBUG, TAG_STTC, " ");