Skip to content

Commit

Permalink
Change path_get_basename()'s return type to const char*
Browse files Browse the repository at this point in the history
This makes it clear that the return value is immutable and moreover suggests
that the return vale is not allocated and thus should be treated carefully.
  • Loading branch information
tihmstar authored and nikias committed Nov 14, 2023
1 parent 83600e9 commit acecac3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,10 @@ int _plist_dict_copy_item(plist_t target_dict, plist_t source_dict, const char *
return 0;
}

char* path_get_basename(char* path)
const char* path_get_basename(char* path)
{
#ifdef WIN32
char *p = path + strlen(path);
const char *p = path + strlen(path);
while (p > path) {
if ((*p == '/') || (*p == '\\')) {
return p+1;
Expand All @@ -708,7 +708,7 @@ char* path_get_basename(char* path)
}
return p;
#else
char *p = strrchr(path, '/');
const char *p = strrchr(path, '/');
return p ? p + 1 : path;
#endif
}
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ int _plist_dict_copy_data(plist_t target_dict, plist_t source_dict, const char *
int _plist_dict_copy_string(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);
int _plist_dict_copy_item(plist_t target_dict, plist_t source_dict, const char *key, const char *alt_source_key);

char* path_get_basename(char* path);
const char* path_get_basename(char* path);

#ifdef __cplusplus
}
Expand Down
3 changes: 1 addition & 2 deletions src/idevicerestore.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,7 @@ int idevicerestore_start(struct idevicerestore_client_t* client)
if (stat(client->cache_dir, &st) < 0) {
mkdir_with_parents(client->cache_dir, 0755);
}
char* ipsw_basename = path_get_basename(client->ipsw->path);
ipsw_basename = strdup(ipsw_basename);
char* ipsw_basename = strdup(path_get_basename(client->ipsw->path));
char* p = strrchr(ipsw_basename, '.');
if (p && isalpha(*(p+1))) {
*p = '\0';
Expand Down

2 comments on commit acecac3

@tihmstar
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you also commit the input arg to const char *?
Since the input isn't modified, it is safe to call this function with a const char *

@nikias
Copy link
Member

@nikias nikias commented on acecac3 Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed that, see ecae6c6

Please sign in to comment.