diff --git a/arch/arm/src/cxd56xx/cxd56_backuplog.c b/arch/arm/src/cxd56xx/cxd56_backuplog.c index 011738e4f3c..cc95479f8f8 100644 --- a/arch/arm/src/cxd56xx/cxd56_backuplog.c +++ b/arch/arm/src/cxd56xx/cxd56_backuplog.c @@ -251,7 +251,7 @@ void *up_backuplog_alloc(const char *name, size_t size) { if (0 == header->entry[index].size) { - strncpy(header->entry[index].name, name, CXD56_LOG_ENTRY_NAME); + strlcpy(header->entry[index].name, name, CXD56_LOG_ENTRY_NAME); header->entry[index].addr = (void *)((uint32_t)header + (allocated << AREASHIFT)); header->entry[index].size = size; diff --git a/arch/arm/src/cxd56xx/cxd56_powermgr_procfs.c b/arch/arm/src/cxd56xx/cxd56_powermgr_procfs.c index fa18097bac8..c14b86724e0 100644 --- a/arch/arm/src/cxd56xx/cxd56_powermgr_procfs.c +++ b/arch/arm/src/cxd56xx/cxd56_powermgr_procfs.c @@ -758,8 +758,8 @@ static int cxd56_powermgr_procfs_readdir(struct fs_dirent_s *dir, } entry->d_type = DTYPE_FILE; - strncpy(entry->d_name, g_powermg_procfs_dir[procfs->index], - strlen(g_powermg_procfs_dir[procfs->index])+1); + strlcpy(entry->d_name, g_powermg_procfs_dir[procfs->index], + sizeof(entry->d_name)); procfs->index++; return OK; diff --git a/arch/arm/src/lc823450/lc823450_procfs_dvfs.c b/arch/arm/src/lc823450/lc823450_procfs_dvfs.c index ddc499b2e29..5bea17194af 100644 --- a/arch/arm/src/lc823450/lc823450_procfs_dvfs.c +++ b/arch/arm/src/lc823450/lc823450_procfs_dvfs.c @@ -269,14 +269,12 @@ static ssize_t dvfs_write(struct file *filep, const char *buffer, int n; int tmp; - n = MIN(buflen, DVFS_LINELEN - 1); - strncpy(line, buffer, n); - line[n] = '\0'; + n = MIN(buflen, DVFS_LINELEN); + strlcpy(line, buffer, n); n = strcspn(line, " "); - n = MIN(n, sizeof(cmd) - 1); - strncpy(cmd, line, n); - cmd[n] = '\0'; + n = MIN(n, sizeof(cmd)); + strlcpy(cmd, line, n); if (0 == strcmp(cmd, "cur_freq")) { diff --git a/arch/arm/src/rtl8720c/amebaz_driver.c b/arch/arm/src/rtl8720c/amebaz_driver.c index 26d09bb7be2..e1d4766ab50 100644 --- a/arch/arm/src/rtl8720c/amebaz_driver.c +++ b/arch/arm/src/rtl8720c/amebaz_driver.c @@ -1177,7 +1177,7 @@ int amebaz_wl_initialize(unsigned char mode) else { - strncpy(wrq.ifr_name, "wlan0", IFNAMSIZ); + strlcpy(wrq.ifr_name, "wlan0", IFNAMSIZ); wrq.u.mode = IW_MODE_INFRA; return amebaz_wl_set_mode(gp_wlan_dev[0], &wrq); } diff --git a/arch/risc-v/src/bl602/bl602_netdev.c b/arch/risc-v/src/bl602/bl602_netdev.c index 07a5a22b362..abb8471d72e 100644 --- a/arch/risc-v/src/bl602/bl602_netdev.c +++ b/arch/risc-v/src/bl602/bl602_netdev.c @@ -1610,8 +1610,7 @@ bl602_net_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg) return -ENOMEM; } - strncpy(passphrase, (char *)ext->key, ext->key_len); - passphrase[ext->key_len] = 0; + strlcpy(passphrase, (char *)ext->key, ext->key_len + 1); wifi_mgmr_sta_passphr_set(passphrase); kmm_free(passphrase); diff --git a/arch/risc-v/src/esp32c3/esp32c3_partition.c b/arch/risc-v/src/esp32c3/esp32c3_partition.c index fdd73ac8792..c83dfc5efdc 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_partition.c +++ b/arch/risc-v/src/esp32c3/esp32c3_partition.c @@ -606,8 +606,7 @@ int esp32c3_partition_init(void) break; } - strncpy(label, (char *)info->label, PARTITION_LABEL_LEN); - label[PARTITION_LABEL_LEN] = '\0'; + strlcpy(label, (char *)info->label, sizeof(label)); sprintf(path, "%s%s", path_base, label); finfo("INFO: [label]: %s\n", label); diff --git a/arch/risc-v/src/mpfs/mpfs_ihc.c b/arch/risc-v/src/mpfs/mpfs_ihc.c index a4aaa211781..7e7cf491e78 100644 --- a/arch/risc-v/src/mpfs/mpfs_ihc.c +++ b/arch/risc-v/src/mpfs/mpfs_ihc.c @@ -1159,8 +1159,8 @@ static int mpfs_rptun_init(const char *shmemname, const char *cpuname) #endif dev->rptun.ops = &g_mpfs_rptun_ops; - strncpy(dev->cpuname, cpuname, RPMSG_NAME_SIZE); - strncpy(dev->shmemname, shmemname, RPMSG_NAME_SIZE); + strlcpy(dev->cpuname, cpuname, sizeof(dev->cpuname)); + strlcpy(dev->shmemname, shmemname, sizeof(dev->shmemname)); list_add_tail(&g_dev_list, &dev->node); ret = rptun_initialize(&dev->rptun); diff --git a/arch/xtensa/src/esp32/esp32_partition.c b/arch/xtensa/src/esp32/esp32_partition.c index 46bb0952d7a..86705af5b65 100644 --- a/arch/xtensa/src/esp32/esp32_partition.c +++ b/arch/xtensa/src/esp32/esp32_partition.c @@ -604,8 +604,7 @@ int esp32_partition_init(void) break; } - strncpy(label, (char *)info->label, PARTITION_LABEL_LEN); - label[PARTITION_LABEL_LEN] = 0; + strlcpy(label, (char *)info->label, sizeof(label)); sprintf(path, "%s%s", path_base, label); finfo("INFO: [label]: %s\n", label); diff --git a/boards/arm/cxd56xx/common/src/cxd56_crashdump.c b/boards/arm/cxd56xx/common/src/cxd56_crashdump.c index 346614cb2ab..d8311b11657 100644 --- a/boards/arm/cxd56xx/common/src/cxd56_crashdump.c +++ b/boards/arm/cxd56xx/common/src/cxd56_crashdump.c @@ -136,7 +136,7 @@ void board_crashdump(uintptr_t currentsp, void *tcb, offset = len - sizeof(pdump->info.filename); } - strncpy(pdump->info.filename, (char *)&filename[offset], + strlcpy(pdump->info.filename, (char *)&filename[offset], sizeof(pdump->info.filename)); } @@ -151,7 +151,7 @@ void board_crashdump(uintptr_t currentsp, void *tcb, /* Save Context */ #if CONFIG_TASK_NAME_SIZE > 0 - strncpy(pdump->info.name, rtcb->name, CONFIG_TASK_NAME_SIZE); + strlcpy(pdump->info.name, rtcb->name, sizeof(pdump->info.name)); #endif pdump->info.pid = rtcb->pid; diff --git a/boards/arm/lpc17xx_40xx/u-blox-c027/src/lpc17_40_ubxmdm.c b/boards/arm/lpc17xx_40xx/u-blox-c027/src/lpc17_40_ubxmdm.c index ebfbd203332..066dedf67bc 100644 --- a/boards/arm/lpc17xx_40xx/u-blox-c027/src/lpc17_40_ubxmdm.c +++ b/boards/arm/lpc17xx_40xx/u-blox-c027/src/lpc17_40_ubxmdm.c @@ -364,7 +364,7 @@ static int lpc17_40_getstatus(struct ubxmdm_lower * lower, for (i = 0; i < UBXMDM_REGISTER_COUNT; i++) { - strncpy(status->register_values[i].name, + strlcpy(status->register_values[i].name, lpc17_40_ubxmdm_name_pins[i].name, 3); status->register_values[i].val = diff --git a/boards/arm/stm32/nucleo-f429zi/src/stm32_bbsram.c b/boards/arm/stm32/nucleo-f429zi/src/stm32_bbsram.c index f9cde7c3340..52fa845f0b1 100644 --- a/boards/arm/stm32/nucleo-f429zi/src/stm32_bbsram.c +++ b/boards/arm/stm32/nucleo-f429zi/src/stm32_bbsram.c @@ -408,7 +408,7 @@ void board_crashdump(uintptr_t currentsp, void *tcb, offset = len - sizeof(pdump->info.filename); } - strncpy(pdump->info.filename, (char *)&filename[offset], + strlcpy(pdump->info.filename, (char *)&filename[offset], sizeof(pdump->info.filename)); } @@ -423,7 +423,7 @@ void board_crashdump(uintptr_t currentsp, void *tcb, /* Save Context */ #if CONFIG_TASK_NAME_SIZE > 0 - strncpy(pdump->info.name, rtcb->name, CONFIG_TASK_NAME_SIZE); + strlcpy(pdump->info.name, rtcb->name, sizeof(pdump->info.name)); #endif pdump->info.pid = rtcb->pid; diff --git a/boards/arm/stm32f7/nucleo-144/src/stm32_bbsram.c b/boards/arm/stm32f7/nucleo-144/src/stm32_bbsram.c index 4a7ca10af47..a4bec805314 100644 --- a/boards/arm/stm32f7/nucleo-144/src/stm32_bbsram.c +++ b/boards/arm/stm32f7/nucleo-144/src/stm32_bbsram.c @@ -408,7 +408,7 @@ void board_crashdump(uintptr_t currentsp, void *tcb, offset = len - sizeof(pdump->info.filename); } - strncpy(pdump->info.filename, (char *)&filename[offset], + strlcpy(pdump->info.filename, (char *)&filename[offset], sizeof(pdump->info.filename)); } @@ -423,7 +423,7 @@ void board_crashdump(uintptr_t currentsp, void *tcb, /* Save Context */ #if CONFIG_TASK_NAME_SIZE > 0 - strncpy(pdump->info.name, rtcb->name, CONFIG_TASK_NAME_SIZE); + strlcpy(pdump->info.name, rtcb->name, sizeof(pdump->info.name)); #endif pdump->info.pid = rtcb->pid; diff --git a/boards/renesas/rx65n/rx65n-grrose/src/rx65n_sbram.c b/boards/renesas/rx65n/rx65n-grrose/src/rx65n_sbram.c index 158052ee44a..5d1e306355a 100644 --- a/boards/renesas/rx65n/rx65n-grrose/src/rx65n_sbram.c +++ b/boards/renesas/rx65n/rx65n-grrose/src/rx65n_sbram.c @@ -362,7 +362,7 @@ void board_crashdump(uintptr_t currentsp, FAR void *tcb, offset = len - sizeof(pdump->info.filename); } - strncpy((char *)pdump->info.filename, (char *)&filename[offset], + strlcpy((char *)pdump->info.filename, (char *)&filename[offset], sizeof(pdump->info.filename)); } @@ -377,7 +377,7 @@ void board_crashdump(uintptr_t currentsp, FAR void *tcb, /* Save Context */ #if CONFIG_TASK_NAME_SIZE > 0 - strncpy(pdump->info.name, rtcb->name, CONFIG_TASK_NAME_SIZE); + strlcpy(pdump->info.name, rtcb->name, sizeof(pdump->info.name)); #endif pdump->info.pid = rtcb->pid; diff --git a/boards/renesas/rx65n/rx65n-rsk2mb/src/rx65n_sbram.c b/boards/renesas/rx65n/rx65n-rsk2mb/src/rx65n_sbram.c index ab63ccfbf57..653ea7f3e3f 100644 --- a/boards/renesas/rx65n/rx65n-rsk2mb/src/rx65n_sbram.c +++ b/boards/renesas/rx65n/rx65n-rsk2mb/src/rx65n_sbram.c @@ -360,7 +360,7 @@ void board_crashdump(uintptr_t currentsp, FAR void *tcb, offset = len - sizeof(pdump->info.filename); } - strncpy((char *)pdump->info.filename, (char *)&filename[offset], + strlcpy((char *)pdump->info.filename, (char *)&filename[offset], sizeof(pdump->info.filename)); } @@ -375,7 +375,7 @@ void board_crashdump(uintptr_t currentsp, FAR void *tcb, /* Save Context */ #if CONFIG_TASK_NAME_SIZE > 0 - strncpy(pdump->info.name, rtcb->name, CONFIG_TASK_NAME_SIZE); + strlcpy(pdump->info.name, rtcb->name, sizeof(pdump->info.name)); #endif pdump->info.pid = rtcb->pid; diff --git a/drivers/mtd/mtd_partition.c b/drivers/mtd/mtd_partition.c index 243c613f149..316745b149c 100644 --- a/drivers/mtd/mtd_partition.c +++ b/drivers/mtd/mtd_partition.c @@ -425,7 +425,8 @@ static int part_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg) info->sectorsize = priv->blocksize; info->startsector = priv->firstblock; - strncpy(info->parent, priv->parent->name, NAME_MAX); + strlcpy(info->parent, priv->parent->name, + sizeof(info->parent)); ret = OK; } diff --git a/drivers/mtd/smart.c b/drivers/mtd/smart.c index de232c454b4..4b84e408d0d 100644 --- a/drivers/mtd/smart.c +++ b/drivers/mtd/smart.c @@ -5928,10 +5928,9 @@ static int smart_fsck_directory(FAR struct smart_struct_s *dev, } #ifdef CONFIG_DEBUG_FS_INFO - strncpy(entryname, + strlcpy(entryname, (const char *) (cur + sizeof(struct smart_entry_header_s)), - dev->namesize); - entryname[dev->namesize] = '\0'; + sizeof(entryname)); finfo("Check entry (name=%s flags=%02x logsector=%02x)\n", entryname, entry->flags, entry->firstsector); #endif @@ -6210,7 +6209,7 @@ int smart_initialize(int minor, FAR struct mtd_dev_s *mtd, dev->namesize = CONFIG_SMARTFS_MAXNAMLEN; if (partname) { - strncpy(dev->partname, partname, SMART_PARTNAME_SIZE); + strlcpy(dev->partname, partname, SMART_PARTNAME_SIZE); } else { diff --git a/drivers/net/phy_notify.c b/drivers/net/phy_notify.c index 9f9f0bfc69c..6f503cfdef6 100644 --- a/drivers/net/phy_notify.c +++ b/drivers/net/phy_notify.c @@ -322,7 +322,7 @@ int phy_notify_subscribe(FAR const char *intf, pid_t pid, client->pid = pid; client->event = *event; - strncpy(client->intf, intf, IFNAMSIZ + 1); + strlcpy(client->intf, intf, IFNAMSIZ + 1); client->intf[IFNAMSIZ] = '\0'; /* Attach/re-attach the PHY interrupt */ diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 195e2e826d4..6e54c117601 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1009,7 +1009,7 @@ static int tun_dev_init(FAR struct tun_device_s *priv, if (devfmt) { - strncpy(priv->dev.d_ifname, devfmt, IFNAMSIZ); + strlcpy(priv->dev.d_ifname, devfmt, IFNAMSIZ); } /* Register the device with the OS so that socket IOCTLs can be performed */ @@ -1342,7 +1342,7 @@ static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg) tun->free_tuns &= ~(1 << intf); priv = filep->f_priv; - strncpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ); + strlcpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ); tundev_unlock(tun); return OK; diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c index 6a30536d772..c09906773b3 100644 --- a/drivers/note/noteram_driver.c +++ b/drivers/note/noteram_driver.c @@ -190,7 +190,7 @@ static void noteram_record_taskname(pid_t pid, const char *name) ti->size = tilen; ti->pid[0] = pid & 0xff; ti->pid[1] = (pid >> 8) & 0xff; - strncpy(ti->name, name, namelen + 1); + strlcpy(ti->name, name, namelen + 1); g_noteram_taskname.buffer_used += tilen; } #endif @@ -748,7 +748,7 @@ static int noteram_ioctl(struct file *filep, int cmd, unsigned long arg) taskname = noteram_get_taskname(param->pid); if (taskname != NULL) { - strncpy(param->taskname, taskname, CONFIG_TASK_NAME_SIZE + 1); + strlcpy(param->taskname, taskname, CONFIG_TASK_NAME_SIZE + 1); param->taskname[CONFIG_TASK_NAME_SIZE] = '\0'; ret = 0; } diff --git a/drivers/video/video.c b/drivers/video/video.c index 0826c97b601..35ddd9fdbe9 100644 --- a/drivers/video/video.c +++ b/drivers/video/video.c @@ -1919,7 +1919,7 @@ static int video_queryctrl(FAR struct v4l2_queryctrl *ctrl) ctrl->step = ext_ctrl.step; ctrl->default_value = ext_ctrl.default_value; ctrl->flags = ext_ctrl.flags; - strncpy(ctrl->name, ext_ctrl.name, sizeof(ctrl->name)); + strlcpy(ctrl->name, ext_ctrl.name, sizeof(ctrl->name)); return OK; } @@ -1942,7 +1942,7 @@ static void set_parameter_name(uint32_t id, char *name) /* copy size = 32 is due to V4L2 specification. */ - strncpy(name, g_video_parameter_name[cnt].name, 32); + strlcpy(name, g_video_parameter_name[cnt].name, 32); } static int video_query_ext_ctrl(FAR struct v4l2_query_ext_ctrl *attr) @@ -1975,7 +1975,7 @@ static int video_query_ext_ctrl(FAR struct v4l2_query_ext_ctrl *attr) attr->maximum = VIDEO_SCENE_MAX - 1; attr->step = 1; attr->default_value = 0; - strncpy(attr->name, "Scene Mode", 32); + strlcpy(attr->name, "Scene Mode", 32); } else { diff --git a/drivers/wireless/gs2200m.c b/drivers/wireless/gs2200m.c index 84b8af51cb7..9e9739ec3b7 100644 --- a/drivers/wireless/gs2200m.c +++ b/drivers/wireless/gs2200m.c @@ -2945,11 +2945,11 @@ static int gs2200m_ioctl_ifreq(FAR struct gs2200m_dev_s *dev, memcpy(&in[0], &dev->net_dev.d_ipaddr, sizeof(in[0])); memcpy(&in[1], &dev->net_dev.d_netmask, sizeof(in[1])); memcpy(&in[2], &dev->net_dev.d_draddr, sizeof(in[2])); - strncpy(addr[0], inet_ntoa_r(in[0], inetaddr, sizeof(inetaddr)), + strlcpy(addr[0], inet_ntoa_r(in[0], inetaddr, sizeof(inetaddr)), sizeof(addr[0])); - strncpy(addr[1], inet_ntoa_r(in[1], inetaddr, sizeof(inetaddr)), + strlcpy(addr[1], inet_ntoa_r(in[1], inetaddr, sizeof(inetaddr)), sizeof(addr[1])); - strncpy(addr[2], inet_ntoa_r(in[2], inetaddr, sizeof(inetaddr)), + strlcpy(addr[2], inet_ntoa_r(in[2], inetaddr, sizeof(inetaddr)), sizeof(addr[2])); gs2200m_set_addresses(dev, addr[0], addr[1], addr[2]); diff --git a/fs/procfs/fs_procfs.c b/fs/procfs/fs_procfs.c index ccaa3b86d47..5405e0693a1 100644 --- a/fs/procfs/fs_procfs.c +++ b/fs/procfs/fs_procfs.c @@ -885,7 +885,7 @@ static int procfs_readdir(FAR struct inode *mountpt, level1->subdirlen + 1]; level1->lastlen = strcspn(name, "/"); level1->lastread = name; - strncpy(entry->d_name, name, level1->lastlen); + strlcpy(entry->d_name, name, level1->lastlen); /* Some of the search entries contain '**' wildcards. When we * report the entry name, we must remove this wildcard search diff --git a/fs/smartfs/smartfs_utils.c b/fs/smartfs/smartfs_utils.c index 90b3db185eb..22d9cc15953 100644 --- a/fs/smartfs/smartfs_utils.c +++ b/fs/smartfs/smartfs_utils.c @@ -529,8 +529,7 @@ int smartfs_finddirentry(struct smartfs_mountpt_s *fs, goto errout; } - strncpy(fs->fs_workbuffer, segment, seglen); - fs->fs_workbuffer[seglen] = '\0'; + strlcpy(fs->fs_workbuffer, segment, seglen + 1); /* Search for "." and ".." as segment names */ @@ -669,10 +668,8 @@ int smartfs_finddirentry(struct smartfs_mountpt_s *fs, kmm_malloc(fs->fs_llformat.namesize + 1); } - memset(direntry->name, 0, - fs->fs_llformat.namesize + 1); - strncpy(direntry->name, entry->name, - fs->fs_llformat.namesize); + strlcpy(direntry->name, entry->name, + fs->fs_llformat.namesize + 1); direntry->datlen = 0; /* Scan the file's sectors to calculate the length @@ -1056,7 +1053,7 @@ int smartfs_createentry(FAR struct smartfs_mountpt_s *fs, entry->utc = time(NULL); #endif memset(entry->name, 0, fs->fs_llformat.namesize); - strncpy(entry->name, filename, fs->fs_llformat.namesize); + strlcpy(entry->name, filename, fs->fs_llformat.namesize); /* Now write the new entry to the parent directory sector */ @@ -1102,7 +1099,7 @@ int smartfs_createentry(FAR struct smartfs_mountpt_s *fs, } memset(direntry->name, 0, fs->fs_llformat.namesize + 1); - strncpy(direntry->name, filename, fs->fs_llformat.namesize); + strlcpy(direntry->name, filename, fs->fs_llformat.namesize); ret = OK; diff --git a/fs/spiffs/src/spiffs_core.c b/fs/spiffs/src/spiffs_core.c index 1e676babc68..381eb561e11 100644 --- a/fs/spiffs/src/spiffs_core.c +++ b/fs/spiffs/src/spiffs_core.c @@ -1392,7 +1392,7 @@ int spiffs_fobj_create(FAR struct spiffs_s *fs, #ifdef CONFIG_SPIFFS_LEADING_SLASH objndx_hdr.name[0] = '/'; #endif - strncpy((char *)objndx_hdr.name + SPIFFS_LEADING_SLASH_SIZE, + strlcpy((char *)objndx_hdr.name + SPIFFS_LEADING_SLASH_SIZE, (const char *)name, CONFIG_SPIFFS_NAME_MAX); @@ -1481,7 +1481,7 @@ int spiffs_fobj_update_ndxhdr(FAR struct spiffs_s *fs, if (name != NULL) { - strncpy((FAR char *)objhdr->name + SPIFFS_LEADING_SLASH_SIZE, + strlcpy((FAR char *)objhdr->name + SPIFFS_LEADING_SLASH_SIZE, (FAR const char *)name, CONFIG_SPIFFS_NAME_MAX); } diff --git a/fs/userfs/fs_userfs.c b/fs/userfs/fs_userfs.c index 81bf34b1546..f4efa49aaf9 100644 --- a/fs/userfs/fs_userfs.c +++ b/fs/userfs/fs_userfs.c @@ -234,7 +234,7 @@ static int userfs_open(FAR struct file *filep, FAR const char *relpath, req->oflags = oflags; req->mode = mode; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_OPEN_REQUEST_S(pathlen + 1), 0, @@ -1115,7 +1115,7 @@ static int userfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, req = (FAR struct userfs_opendir_request_s *)priv->iobuffer; req->req = USERFS_REQ_OPENDIR; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_OPENDIR_REQUEST_S(pathlen + 1), 0, @@ -1696,7 +1696,7 @@ static int userfs_unlink(FAR struct inode *mountpt, req = (FAR struct userfs_unlink_request_s *)priv->iobuffer; req->req = USERFS_REQ_UNLINK; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_UNLINK_REQUEST_S(pathlen + 1), 0, @@ -1783,7 +1783,7 @@ static int userfs_mkdir(FAR struct inode *mountpt, req->req = USERFS_REQ_MKDIR; req->mode = mode; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_MKDIR_REQUEST_S(pathlen + 1), 0, @@ -1869,7 +1869,7 @@ static int userfs_rmdir(FAR struct inode *mountpt, req = (FAR struct userfs_rmdir_request_s *)priv->iobuffer; req->req = USERFS_REQ_RMDIR; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_RMDIR_REQUEST_S(pathlen + 1), 0, @@ -1960,8 +1960,8 @@ static int userfs_rename(FAR struct inode *mountpt, req->req = USERFS_REQ_RENAME; req->newoffset = oldpathlen; - strncpy(req->oldrelpath, oldrelpath, oldpathlen); - strncpy(&req->oldrelpath[oldpathlen], newrelpath, newpathlen); + strlcpy(req->oldrelpath, oldrelpath, oldpathlen); + strlcpy(&req->oldrelpath[oldpathlen], newrelpath, newpathlen); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_RENAME_REQUEST_S(oldpathlen, newpathlen), @@ -2047,7 +2047,7 @@ static int userfs_stat(FAR struct inode *mountpt, FAR const char *relpath, req = (FAR struct userfs_stat_request_s *)priv->iobuffer; req->req = USERFS_REQ_STAT; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_STAT_REQUEST_S(pathlen + 1), 0, @@ -2139,7 +2139,7 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, req->buf = *buf; req->flags = flags; - strncpy(req->relpath, relpath, priv->mxwrite); + strlcpy(req->relpath, relpath, priv->mxwrite); nsent = psock_sendto(&priv->psock, priv->iobuffer, SIZEOF_USERFS_CHSTAT_REQUEST_S(pathlen + 1), 0, diff --git a/libs/libc/dirent/lib_nftw.c b/libs/libc/dirent/lib_nftw.c index 34e56774d1a..596d78176d9 100644 --- a/libs/libc/dirent/lib_nftw.c +++ b/libs/libc/dirent/lib_nftw.c @@ -234,8 +234,7 @@ int nftw(FAR const char *path, nftw_cb_t fn, int fdlimit, int flags) { char pathbuf[PATH_MAX + 1]; - strncpy(pathbuf, path, PATH_MAX); - pathbuf[PATH_MAX] = '\0'; + strlcpy(pathbuf, path, sizeof(pathbuf)); return do_nftw(pathbuf, fn, fdlimit, flags, 0); } diff --git a/libs/libc/misc/lib_utsname.c b/libs/libc/misc/lib_utsname.c index 942e8cba180..6bfe7961862 100644 --- a/libs/libc/misc/lib_utsname.c +++ b/libs/libc/misc/lib_utsname.c @@ -84,26 +84,23 @@ int uname(FAR struct utsname *name) /* Copy the strings. Assure that each is NUL terminated. */ - strncpy(name->sysname, "NuttX", SYS_NAMELEN); + strlcpy(name->sysname, "NuttX", sizeof(name->sysname)); /* Get the hostname */ ret = gethostname(name->nodename, HOST_NAME_MAX); name->nodename[HOST_NAME_MAX - 1] = '\0'; - strncpy(name->release, CONFIG_VERSION_STRING, SYS_NAMELEN); - name->release[SYS_NAMELEN - 1] = '\0'; + strlcpy(name->release, CONFIG_VERSION_STRING, sizeof(name->release)); #if defined(__DATE__) && defined(__TIME__) snprintf(name->version, VERSION_NAMELEN, "%s %s %s", CONFIG_VERSION_BUILD, __DATE__, __TIME__); #else - strncpy(name->version, CONFIG_VERSION_BUILD, VERSION_NAMELEN); + strlcpy(name->version, CONFIG_VERSION_BUILD, sizeof(name->version)); #endif - name->version[VERSION_NAMELEN - 1] = '\0'; - strncpy(name->machine, CONFIG_ARCH, SYS_NAMELEN); - name->machine[SYS_NAMELEN - 1] = '\0'; + strlcpy(name->machine, CONFIG_ARCH, sizeof(name->machine)); return ret; } diff --git a/libs/libc/net/lib_getifaddrs.c b/libs/libc/net/lib_getifaddrs.c index 82630e8df6c..ced90587fc3 100644 --- a/libs/libc/net/lib_getifaddrs.c +++ b/libs/libc/net/lib_getifaddrs.c @@ -226,7 +226,7 @@ int getifaddrs(FAR struct ifaddrs **addrs) } myaddrs->addrs.ifa_name = myaddrs->name; - strncpy(myaddrs->name, req.lifr_name, IF_NAMESIZE); + strlcpy(myaddrs->name, req.lifr_name, IF_NAMESIZE); myaddrs->addrs.ifa_flags = flags; } diff --git a/libs/libc/net/lib_indextoname.c b/libs/libc/net/lib_indextoname.c index 48cc0031b4d..acd547f1498 100644 --- a/libs/libc/net/lib_indextoname.c +++ b/libs/libc/net/lib_indextoname.c @@ -61,7 +61,7 @@ FAR char *if_indextoname(unsigned int ifindex, FAR char *ifname) req.ifr_ifindex = ifindex; if (ioctl(sockfd, SIOCGIFNAME, (unsigned long)&req) >= 0) { - strncpy(ifname, req.ifr_name, IF_NAMESIZE); + strlcpy(ifname, req.ifr_name, IF_NAMESIZE); close(sockfd); return ifname; } diff --git a/libs/libc/netdb/lib_gethostbyaddrr.c b/libs/libc/netdb/lib_gethostbyaddrr.c index 702a9fd69aa..d96ff24d005 100644 --- a/libs/libc/netdb/lib_gethostbyaddrr.c +++ b/libs/libc/netdb/lib_gethostbyaddrr.c @@ -211,7 +211,7 @@ out_copyname: return -ERANGE; } - strncpy(dest, g_lo_hostname, buflen); + strlcpy(dest, g_lo_hostname, buflen); host->h_name = dest; return 0; diff --git a/libs/libc/netdb/lib_gethostentbynamer.c b/libs/libc/netdb/lib_gethostentbynamer.c index b7f6a2fc64f..ab179f98023 100644 --- a/libs/libc/netdb/lib_gethostentbynamer.c +++ b/libs/libc/netdb/lib_gethostentbynamer.c @@ -195,7 +195,7 @@ static int lib_numeric_address(FAR const char *name, return -ERANGE; } - strncpy(ptr, name, buflen); + strlcpy(ptr, name, buflen); /* Set the address to h_name */ @@ -278,7 +278,7 @@ static int lib_localhost(FAR const char *name, FAR struct hostent_s *host, return -ERANGE; } - strncpy(dest, name, buflen); + strlcpy(dest, name, buflen); /* Set the address to h_name */ @@ -408,7 +408,7 @@ static int lib_find_answer(FAR const char *name, FAR struct hostent_s *host, return -ERANGE; } - strncpy(ptr, name, buflen); + strlcpy(ptr, name, buflen); /* Set the address to h_name */ @@ -545,7 +545,7 @@ static int lib_dns_lookup(FAR const char *name, FAR struct hostent_s *host, /* And copy name */ DEBUGASSERT(buflen >= namelen + 1); - strncpy(ptr, name, buflen); + strlcpy(ptr, name, buflen); /* Set the address to h_name */ diff --git a/libs/libc/spawn/lib_psfa_addopen.c b/libs/libc/spawn/lib_psfa_addopen.c index 8ae60d91f35..6a3aeecb213 100644 --- a/libs/libc/spawn/lib_psfa_addopen.c +++ b/libs/libc/spawn/lib_psfa_addopen.c @@ -96,7 +96,7 @@ int posix_spawn_file_actions_addopen( entry->fd = fd; entry->oflags = oflags; entry->mode = mode; - strncpy(entry->path, path, len + 1); + strlcpy(entry->path, path, len + 1); /* And add it to the file action list */ diff --git a/libs/libc/stdio/lib_libvscanf.c b/libs/libc/stdio/lib_libvscanf.c index fffad3a0817..095a953266c 100644 --- a/libs/libc/stdio/lib_libvscanf.c +++ b/libs/libc/stdio/lib_libvscanf.c @@ -337,8 +337,7 @@ int lib_vscanf(FAR struct lib_instream_s *obj, FAR int *lastc, { for (tc = fmt; isdigit(fmt_char(fmt)); fmt++) ; - strncpy(tmp, tc, fmt - tc); - tmp[fmt - tc] = '\0'; + strlcpy(tmp, tc, fmt - tc + 1); width = atoi(tmp); fmt--; } diff --git a/libs/libc/string/lib_strerrorr.c b/libs/libc/string/lib_strerrorr.c index 65c3f3a94f2..625d8532fd8 100644 --- a/libs/libc/string/lib_strerrorr.c +++ b/libs/libc/string/lib_strerrorr.c @@ -54,6 +54,6 @@ int strerror_r(int errnum, FAR char *buf, size_t buflen) FAR const char *errstr = strerror(errnum); DEBUGASSERT(buf != NULL); - strncpy(buf, errstr, buflen); + strlcpy(buf, errstr, buflen); return OK; } diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c index 3cd312b8d8a..5058a8d2e27 100644 --- a/libs/libc/time/lib_localtime.c +++ b/libs/libc/time/lib_localtime.c @@ -1613,13 +1613,12 @@ static int tzparse(FAR const char *name, FAR struct state_s *sp, } cp = sp->chars; - strncpy(cp, stdname, stdlen); + stdlen += 1; + strlcpy(cp, stdname, stdlen); cp += stdlen; - *cp++ = '\0'; if (dstlen != 0) { - strncpy(cp, dstname, dstlen); - *(cp + dstlen) = '\0'; + strlcpy(cp, dstname, dstlen + 1); } return 0; diff --git a/libs/libc/unistd/lib_gethostname.c b/libs/libc/unistd/lib_gethostname.c index 0689aa794b3..57dcf734e5d 100644 --- a/libs/libc/unistd/lib_gethostname.c +++ b/libs/libc/unistd/lib_gethostname.c @@ -111,7 +111,7 @@ int gethostname(FAR char *name, size_t namelen) */ flags = enter_critical_section(); - strncpy(name, g_hostname, namelen); + strlcpy(name, g_hostname, namelen); leave_critical_section(flags); return 0; diff --git a/net/icmpv6/icmpv6_autoconfig.c b/net/icmpv6/icmpv6_autoconfig.c index 9c956c5baae..14b277ed4c4 100644 --- a/net/icmpv6/icmpv6_autoconfig.c +++ b/net/icmpv6/icmpv6_autoconfig.c @@ -190,7 +190,7 @@ static int icmpv6_send_message(FAR struct net_driver_s *dev, bool advertise) /* Remember the routing device name */ - strncpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname, + strlcpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname, IFNAMSIZ); /* Allocate resources to receive a callback. This and the following diff --git a/net/icmpv6/icmpv6_neighbor.c b/net/icmpv6/icmpv6_neighbor.c index 8c175bdc75b..65c668efeca 100644 --- a/net/icmpv6/icmpv6_neighbor.c +++ b/net/icmpv6/icmpv6_neighbor.c @@ -264,7 +264,7 @@ int icmpv6_neighbor(const net_ipv6addr_t ipaddr) /* Remember the routing device name */ - strncpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname, + strlcpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname, IFNAMSIZ); /* Now loop, testing if the address mapping is in the Neighbor Table and diff --git a/net/local/local_accept.c b/net/local/local_accept.c index 6cf82c69cab..cef26ce3daa 100644 --- a/net/local/local_accept.c +++ b/net/local/local_accept.c @@ -173,8 +173,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr, client->lc_peer = conn; #endif /* CONFIG_NET_LOCAL_SCM */ - strncpy(conn->lc_path, client->lc_path, UNIX_PATH_MAX - 1); - conn->lc_path[UNIX_PATH_MAX - 1] = '\0'; + strlcpy(conn->lc_path, client->lc_path, sizeof(conn->lc_path)); conn->lc_instance_id = client->lc_instance_id; /* Open the server-side write-only FIFO. This should not diff --git a/net/local/local_bind.c b/net/local/local_bind.c index 5af4455b7f9..2f1f27f5255 100644 --- a/net/local/local_bind.c +++ b/net/local/local_bind.c @@ -92,8 +92,7 @@ int psock_local_bind(FAR struct socket *psock, /* Copy the path into the connection structure */ - strncpy(conn->lc_path, unaddr->sun_path, UNIX_PATH_MAX - 1); - conn->lc_path[UNIX_PATH_MAX - 1] = '\0'; + strlcpy(conn->lc_path, unaddr->sun_path, sizeof(conn->lc_path)); conn->lc_instance_id = -1; } } diff --git a/net/local/local_connect.c b/net/local/local_connect.c index 567191693bc..feb562090ac 100644 --- a/net/local/local_connect.c +++ b/net/local/local_connect.c @@ -306,9 +306,8 @@ int psock_local_connect(FAR struct socket *psock, client->lc_type = conn->lc_type; client->lc_proto = conn->lc_proto; - strncpy(client->lc_path, unaddr->sun_path, - UNIX_PATH_MAX - 1); - client->lc_path[UNIX_PATH_MAX - 1] = '\0'; + strlcpy(client->lc_path, unaddr->sun_path, + sizeof(client->lc_path)); client->lc_instance_id = local_generate_instance_id(); /* The client is now bound to an address */ diff --git a/net/netdev/netdev_ifconf.c b/net/netdev/netdev_ifconf.c index 360228468d2..323cc0811fb 100644 --- a/net/netdev/netdev_ifconf.c +++ b/net/netdev/netdev_ifconf.c @@ -200,7 +200,7 @@ static int ifconf_ipv6_callback(FAR struct net_driver_s *dev, FAR void *arg) * transferred is returned in lifc_len. */ - strncpy(req->lifr_name, dev->d_ifname, IFNAMSIZ); + strlcpy(req->lifr_name, dev->d_ifname, IFNAMSIZ); inaddr->sin6_family = AF_INET6; inaddr->sin6_port = 0; diff --git a/net/procfs/net_procfs.c b/net/procfs/net_procfs.c index e0e239a07b5..cbfee89b06a 100644 --- a/net/procfs/net_procfs.c +++ b/net/procfs/net_procfs.c @@ -516,7 +516,7 @@ static int netprocfs_readdir(FAR struct fs_dirent_s *dir, /* Copy the network statistics directory entry */ entry->d_type = DTYPE_FILE; - strncpy(entry->d_name, "stat", NAME_MAX + 1); + strlcpy(entry->d_name, "stat", sizeof(entry->d_name)); } else #ifdef CONFIG_NET_MLD @@ -525,7 +525,7 @@ static int netprocfs_readdir(FAR struct fs_dirent_s *dir, /* Copy the MLD directory entry */ entry->d_type = DTYPE_FILE; - strncpy(entry->d_name, "mld", NAME_MAX + 1); + strlcpy(entry->d_name, "mld", sizeof(entry->d_name)); } else #endif @@ -536,7 +536,7 @@ static int netprocfs_readdir(FAR struct fs_dirent_s *dir, /* Copy the network statistics directory entry */ entry->d_type = DTYPE_DIRECTORY; - strncpy(entry->d_name, "route", NAME_MAX + 1); + strlcpy(entry->d_name, "route", sizeof(entry->d_name)); } else #endif @@ -584,7 +584,7 @@ static int netprocfs_readdir(FAR struct fs_dirent_s *dir, /* Copy the device statistics file entry */ entry->d_type = DTYPE_FILE; - strncpy(entry->d_name, dev->d_ifname, NAME_MAX + 1); + strlcpy(entry->d_name, dev->d_ifname, sizeof(entry->d_name)); } /* Set up the next directory entry offset. NOTE that we could use the diff --git a/net/procfs/net_procfs_route.c b/net/procfs/net_procfs_route.c index cd78441c4de..da5537d41ee 100644 --- a/net/procfs/net_procfs_route.c +++ b/net/procfs/net_procfs_route.c @@ -715,7 +715,7 @@ static int route_readdir(FAR struct fs_dirent_s *dir, /* Save the filename and file type */ entry->d_type = DTYPE_FILE; - strncpy(entry->d_name, dname, NAME_MAX + 1); + strlcpy(entry->d_name, dname, sizeof(entry->d_name)); /* Set up the next directory entry offset. NOTE that we could use the * standard f_pos instead of our own private index. diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 7b0908c44c8..ed2b8c24e78 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -483,8 +483,7 @@ static void nxtask_setup_name(FAR struct task_tcb_s *tcb, { /* Copy the name into the TCB */ - strncpy(tcb->cmn.name, name, CONFIG_TASK_NAME_SIZE); - tcb->cmn.name[CONFIG_TASK_NAME_SIZE] = '\0'; + strlcpy(tcb->cmn.name, name, sizeof(tcb->cmn.name)); } #else # define nxtask_setup_name(t,n)