The API for the MFlash file system library (mflash_file.h) is not const-correct, in that the filename parameters in the mflash_file_t struct and mflash_file_save() and mflash_file_mmap() functions are not declared "const char *" but instead "char *".
typedef struct
{
char *path;
uint32_t max_size;
} mflash_file_t;
status_t mflash_file_save(char *path, uint8_t *data, uint32_t size);
status_t mflash_file_mmap(char *path, uint8_t **pdata, uint32_t *psize);
This throws a warning when declaring a table of mflash_file_t structs in a C++ translation unit:
C:\NXP\workspace\[redacted]\config/kvstore_config.h:50:28: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
50 | #define KVSTORE_FILE_PATH ("/kvstore")
| ~^~~~~~~~~~~
../source/program.cpp:47:15: note: in expansion of macro 'KVSTORE_FILE_PATH'
47 | { .path = KVSTORE_FILE_PATH, .max_size = 3000 },
| ^~~~~~~~~~~~~~~~~
String literals should never be treated as writable; in some cases, this can cause excessive memory usage if strings are allocated to RAM instead of flash because they are declared writable.
Please fix the API for these functions. Also, please add missing extern "C" { } declarations for this and other header files; I have to place the #include <mflash_file.h> declaration inside an extern "C" { } declaration in order for my application to link correctly.
Dana M.
Solved! Go to Solution.
Hi, @dmarks_ls
Happy new year!
I have reported this issue to our internal expert team.
And our internal expert team will check and decide whether and how to correct it in our future release.
Thanks for pointing out our non-rigorous.
If there is no other concerns on this case, it will be closed automatically because of no any feedback from you.
Best regards,
Christine.
Hi, @dmarks_ls
To quick locate the code you mentioned, would you mind let me know your SDK version and which example project are you referring to?
So that I can download the same SDK version and import same example project to locate the issue you mentioned.
Best regards,
Christine.
I'm using the latest SDK (v2.16.1), and use any example that includes the MFlash driver (e.g. "wifi_mqtt"). I've attached my "fixed" version, which simply involves replacing all instances of "char *" with "const char *".
Dana M.
Hi, @dmarks_ls
Thanks for your feedback!
I checked the code you mentioned and I found that here it mentioned the struct mflash_file_t should be const, so I agree with your point.
/*! @brief Initializes mflash filesystem and driver. Creates new filesystem unless already in place. */
status_t mflash_init(const mflash_file_t *dir_template, bool init_drv);
I will report this to our internal team and they will decide whether revise the code because as you know this is just a warning.
And also it depends on the compiler, for some strict compiler, it will report this warning.
Thanks for pointing out our non-rigorous.
Best regards,
Christine.
Hi, @dmarks_ls
Happy new year!
I have reported this issue to our internal expert team.
And our internal expert team will check and decide whether and how to correct it in our future release.
Thanks for pointing out our non-rigorous.
If there is no other concerns on this case, it will be closed automatically because of no any feedback from you.
Best regards,
Christine.
Hi, @dmarks_ls
For your information, our driver owner will add the const specifier for the next release and this will be release 25.03.
Best regards,
Christine.
Hi, @dmarks_ls
I will check this issue and reply to you once have any updates.
Best regards,
Christine.