drivers/mtd/mtd_partition.c: Copy the partition name to internal buffer so that the caller can free the name argument

This commit is contained in:
Xiang Xiao 2018-08-28 07:06:37 -06:00 committed by Gregory Nutt
parent 159b88c871
commit 41a1e76735

View File

@ -93,7 +93,7 @@ struct mtd_partition_s
struct mtd_partition_s *pnext; /* Pointer to next partition struct */ struct mtd_partition_s *pnext; /* Pointer to next partition struct */
#endif #endif
#ifdef CONFIG_MTD_PARTITION_NAMES #ifdef CONFIG_MTD_PARTITION_NAMES
FAR const char *name; /* Name of the partition */ char name[11]; /* Name of the partition */
#endif #endif
}; };
@ -604,12 +604,6 @@ static ssize_t part_procfs_read(FAR struct file *filep, FAR char *buffer,
/* Copy data from the next known partition */ /* Copy data from the next known partition */
#ifdef CONFIG_MTD_PARTITION_NAMES #ifdef CONFIG_MTD_PARTITION_NAMES
if (attr->nextpart->name == NULL)
{
strcpy(partname, "(noname) ");
}
else
{
ptr = attr->nextpart->name; ptr = attr->nextpart->name;
for (x = 0; x < sizeof(partname) - 1; x++) for (x = 0; x < sizeof(partname) - 1; x++)
{ {
@ -630,7 +624,6 @@ static ssize_t part_procfs_read(FAR struct file *filep, FAR char *buffer,
} }
partname[x] = '\0'; partname[x] = '\0';
}
/* Terminate the partition name and add to output buffer */ /* Terminate the partition name and add to output buffer */
@ -860,7 +853,7 @@ FAR struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s *mtd, off_t firstblock,
part->blkpererase = blkpererase; part->blkpererase = blkpererase;
#ifdef CONFIG_MTD_PARTITION_NAMES #ifdef CONFIG_MTD_PARTITION_NAMES
part->name = NULL; strcpy(part->name, "(noname)");
#endif #endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS) #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
@ -906,11 +899,14 @@ int mtd_setpartitionname(FAR struct mtd_dev_s *mtd, FAR const char *name)
{ {
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)mtd; FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)mtd;
DEBUGASSERT(mtd); if (priv == NULL || name == NULL)
DEBUGASSERT(name); {
return -EINVAL;
}
/* Allocate space for the name */ /* Allocate space for the name */
priv->name = name;
strncpy(priv->name, name, sizeof(priv->name));
return OK; return OK;
} }
#endif #endif