SS_FSUpdate: Fix build error due to truncation by strncpy 83/319883/2
authorUnsung Lee <unsung.lee@samsung.com>
Wed, 19 Feb 2025 05:13:59 +0000 (14:13 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Wed, 19 Feb 2025 07:37:15 +0000 (16:37 +0900)
Fix build error when -Werror=stringop-truncation flag is turned on.
This flag returns build error when strncpy is used for string copy.
This is because strncpy does not insert '\0' at the end of destination.
On the other hand, snprintf inserts '\0' at the end of destination.

Change-Id: I150b475024653db7af1adda7f0ac7b10f38edbad
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/upgrade-apply-deltafs/engine/SS_FSUpdate.c

index 08d585cfbda5930a8d68e65dd5682b4cb2594292..b29742e8727f506c51d76d5bc2f1a45e177d7c47 100644 (file)
@@ -89,7 +89,7 @@ void SS_unicode_to_char(const char *src, char *dest, int size)
        if (src == NULL)
                return;
 
-       strncpy(dest, src, size);
+       snprintf(dest, size, "%s", src);
 }
 
 void SS_char_to_unicode(const char *src, char *dest, int size)
@@ -97,7 +97,7 @@ void SS_char_to_unicode(const char *src, char *dest, int size)
        if (src == NULL)
                return;
 
-       strncpy(dest, src, size);
+       snprintf(dest, size, "%s", src);
 }
 
 long SS_recursive_folder_creater(const char *path, const mode_t mode) {
@@ -162,8 +162,8 @@ SS_CopyFile(const char *strFromPath, const char *strToPath)
                return -1;
        }
 
-       SS_unicode_to_char((const char *)strFromPath, (char *)path1, MAX_PATH - 1);
-       SS_unicode_to_char((const char *)strToPath, (char *)path2, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)strFromPath, (char *)path1, MAX_PATH);
+       SS_unicode_to_char((const char *)strToPath, (char *)path2, MAX_PATH);
 
        //LOGL(LOG_SSENGINE, "%s -> %s \n", path1, path2);
 
@@ -200,7 +200,7 @@ long SS_DeleteFile(const char *strPath)
        char path[MAX_PATH] = { '\0' };
        int ret = 0;
 
-       SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH);
        //LOGL(LOG_SSENGINE, "%s\n", path);
        ret = unlink(path);
        if (ret == 0)
@@ -234,7 +234,7 @@ long SS_CreateFolder(const char *strPath)
        int ret = 0;
        char path[MAX_PATH] = { '\0' };
 
-       SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH);
        mode = S_IRUSR /*Read by owner */  |
                S_IWUSR /*Write by owner */  |
                S_IXUSR /*Execute by owner */  |
@@ -296,7 +296,7 @@ SS_OpenFile(const char *strPath, E_RW_TYPE wFlag, long *pwHandle)
        mode_t mode;
        char path[MAX_PATH] = { '\0' };
 
-       SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH);
 
        mode = SS_get_mode(wFlag);
        //LOGL(LOG_SSENGINE, "Path:%s  wFlag:%d  Mode:%d\n", path, wFlag, mode);
@@ -332,7 +332,7 @@ SS_OpenFile(const char *strPath, E_RW_TYPE wFlag, long *pwHandle)
                        dir[i + 1] = '\0';
 
                        SS_char_to_unicode((const char *)dir,
-                                       (char *)dirShort, MAX_PATH - 1);
+                                       (char *)dirShort, MAX_PATH);
 
                        if (SS_CreateFolder(dirShort)) {
                                LOGE(" Fail create folder, Leave SS_OpenFile\n");
@@ -416,8 +416,8 @@ long SS_MoveFile(const char *strFromPath, const char *strToPath)
        if (!strFromPath || !strToPath)
                return -1;      //should never happen
 
-       SS_unicode_to_char(strFromPath, (char *)path1, MAX_PATH - 1);
-       SS_unicode_to_char(strToPath, (char *)path2, MAX_PATH - 1);
+       SS_unicode_to_char(strFromPath, (char *)path1, MAX_PATH);
+       SS_unicode_to_char(strToPath, (char *)path2, MAX_PATH);
 
        LOGL(LOG_INFO, "entered path1:%s | path2:%s\n", path1, path2);
        ret = stat(path1, &sbuf);
@@ -519,7 +519,7 @@ long SS_Unlink(char *pLinkName)
        //enumFileType fileType = FT_REGULAR_FILE;
 
 
-       SS_unicode_to_char((const char *)pLinkName, (char *)path, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)pLinkName, (char *)path, MAX_PATH);
 
        ret = unlink(path);
        if (ret < 0) {
@@ -544,8 +544,8 @@ SS_VerifyLinkReference(char *pLinkName, char *pReferenceFileName)
        char refpath[MAX_PATH] = { '\0' };
 
 
-       SS_unicode_to_char((const char *)pLinkName, (char *)path, MAX_PATH - 1);
-       SS_unicode_to_char((const char *)pReferenceFileName, (char *)refpath, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)pLinkName, (char *)path, MAX_PATH);
+       SS_unicode_to_char((const char *)pReferenceFileName, (char *)refpath, MAX_PATH);
 
        ret = readlink(path, linkedpath, MAX_PATH);
        if (ret < 0) {
@@ -571,8 +571,8 @@ SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName)
        //enumFileType fileType = FT_SYMBOLIC_LINK;
        struct stat sbuf;
 
-       SS_unicode_to_char((const char *)pLinkName, (char *)sympath, MAX_PATH - 1);
-       SS_unicode_to_char((const char *)pReferenceFileName, (char *)refpath, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)pLinkName, (char *)sympath, MAX_PATH);
+       SS_unicode_to_char((const char *)pReferenceFileName, (char *)refpath, MAX_PATH);
 
        ret = symlink(refpath, sympath);
        if (ret != 0) {
@@ -611,8 +611,8 @@ SS_HardLink(char *hardLinkName, char *referenceName)
        char hardpath[MAX_PATH] = { '\0' };
        char refpath[MAX_PATH] = { '\0' };
 
-       SS_unicode_to_char((const char *)hardLinkName, (char *)hardpath, MAX_PATH - 1);
-       SS_unicode_to_char((const char *)referenceName, (char *)refpath, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)hardLinkName, (char *)hardpath, MAX_PATH);
+       SS_unicode_to_char((const char *)referenceName, (char *)refpath, MAX_PATH);
 
        ret = link(refpath, hardpath);
        if (ret != 0) {
@@ -629,7 +629,7 @@ long SS_GetFileType(char *pLinkName, enumFileType * fileType)
        struct stat sbuf;
 
        //LOGL(LOG_SSENGINE, "\n");
-       SS_unicode_to_char((const char *)pLinkName, (char *)path, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)pLinkName, (char *)path, MAX_PATH);
 
        ret = lstat(path, &sbuf);
        if (ret < 0) {
@@ -781,7 +781,7 @@ long SS_SetFileAttributes(const char *ui16pFilePath,
                return S_SS_SUCCESS;
        }
 
-       SS_unicode_to_char((const char *)ui16pFilePath, (char *)setFilePath, MAX_PATH - 1);
+       SS_unicode_to_char((const char *)ui16pFilePath, (char *)setFilePath, MAX_PATH);
 
        ret = lstat(setFilePath, &sbuf);
        if (ret < 0) {
@@ -1042,7 +1042,7 @@ SS_GetAvailableFreeSpace(const char *partition_name,
        int result = 0;
        char path[MAX_PATH] = { '\0' };
 
-       SS_unicode_to_char(partition_name, (char *)path, MAX_PATH - 1);
+       SS_unicode_to_char(partition_name, (char *)path, MAX_PATH);
        //LOGL(LOG_SSENGINE, "Enter %s path=%s\n", __func__, path);
        struct statfs vfs;