aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Pietrasiewicz <andrzej.p@collabora.com>2023-10-06 12:22:37 +0200
committerMarcus Meissner <marcus@jet.franken.de>2023-10-09 15:48:21 +0200
commit03f2672c36a0513d804e3763c81089fb82792686 (patch)
treeb0a870917239101174c97be077d6fa3ad1dd9686
parent5f8dd58af1127c36c7f807402683649e8ccad3d0 (diff)
downloadlibmtp-03f2672c36a0513d804e3763c81089fb82792686.tar.gz
Properly use dirname()/basename() in newfolder example
Citing man 3 dirname: On Linux both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions. These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. Alternatively, they may return a pointer to some part of path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required.
-rw-r--r--examples/newfolder.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/examples/newfolder.c b/examples/newfolder.c
index 70ea684..653f322 100644
--- a/examples/newfolder.c
+++ b/examples/newfolder.c
@@ -56,10 +56,14 @@ int
newfolder_function(char * path)
{
printf("Creating new folder %s\n",path);
- char * parent = dirname(path);
- char * folder = basename(path);
+ char * path_for_parent = strdup(path);
+ char * path_for_folder = strdup(path);
+ char * parent = dirname(path_for_parent);
+ char * folder = basename(path_for_folder);
int id = parse_path (parent,files,folders);
int newid = LIBMTP_Create_Folder(device, folder, id, 0);
+ free(path_for_folder);
+ free(path_for_parent);
if (newid == 0) {
printf("Folder creation failed.\n");
LIBMTP_Dump_Errorstack(device);