sys: Add a wrapper to basename

Basename is a very tricky function because there are 2 different implementations
available, POXIS and GNU. They can have different outputs and GNU is readonly and
POSIX is read-write. For more information take a look at the GNU basename manual.

In most areas of swupd it was expected to have the GNU basename used, but when dirname
was needed library libgen.h was included and the POSIX basename is used instead. As
incorrect usages of basename can cause memory problems a wrapper was created to make
sure we are always using the GNU basename, unless specified.

in verify and staging there are calls to the POSIX version of basename and it looks like
this expected from the code. So it wasn't changed for now.

Signed-off-by: Otavio Pontes <otavio.pontes@intel.com>
This commit is contained in:
Otavio Pontes
2019-10-29 09:10:36 -07:00
parent a4c7f93194
commit 52fffd30c2
13 changed files with 40 additions and 25 deletions
+1 -1
View File
@@ -173,7 +173,7 @@ struct list *get_alias_definitions(void)
iters = system_alias_files;
iteru = user_alias_files;
while (iters && iteru) {
int pivot = strcmp(basename(iteru->data), basename(iters->data));
int pivot = strcmp(sys_basename(iteru->data), sys_basename(iters->data));
if (pivot == 0) {
if (iters == system_alias_files) {
system_alias_files = iters->next;
-1
View File
@@ -22,7 +22,6 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
-1
View File
@@ -23,7 +23,6 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
-1
View File
@@ -26,7 +26,6 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
+5 -7
View File
@@ -23,7 +23,6 @@
#define _GNU_SOURCE
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <string.h>
#include "config.h"
@@ -156,20 +155,19 @@ skip_mom:
while (item) {
if (MoM) {
bundle_manifest = mom_search_bundle(MoM, basename((char *)item->data));
bundle_manifest = mom_search_bundle(MoM, sys_basename((char *)item->data));
}
if (bundle_manifest) {
name = get_printable_bundle_name(bundle_manifest->filename, bundle_manifest->is_experimental);
print("%s\n", name);
free(name);
} else {
string_or_die(&name, basename((char *)item->data));
print("%s\n", sys_basename((char *)item->data));
}
print("%s\n", name);
free_string(&name);
free(item->data);
item = item->next;
}
list_free_list(bundles);
list_free_list_and_data(bundles, free);
free_string(&path);
manifest_free(MoM);
-1
View File
@@ -25,7 +25,6 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
-1
View File
@@ -25,7 +25,6 @@
#include <bsdiff.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
+1 -1
View File
@@ -47,7 +47,7 @@ static struct option opts[] = {
static void usage(const char *name)
{
print("Usage:\n");
print(" swupd %s [OPTION...] filename\n\n", basename((char *)name));
print(" swupd %s [OPTION...] filename\n\n", sys_basename(name));
print("Help Options:\n");
print(" -h, --help Show help options\n\n");
print("Application Options:\n");
+15 -3
View File
@@ -17,22 +17,25 @@
*
*/
// Make sure we are getting the gnu version of basename
#define _GNU_SOURCE
#include "sys.h"
#include <libgen.h>
#undef basename
#include <string.h>
#include "list.h"
#include "log.h"
#include "macros.h"
#include "memory.h"
#include "strings.h"
#include "sys.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/wait.h>
@@ -354,6 +357,15 @@ char *sys_dirname(const char *path)
return dir;
}
char *sys_basename(const char *path)
{
if (!path) {
return NULL;
}
return basename(path);
}
char *sys_path_join(const char *prefix, const char *path)
{
size_t len = 0;
+12
View File
@@ -163,6 +163,18 @@ bool systemd_in_container(void);
*/
char *sys_dirname(const char *path);
/**
* @brief Safe GNU implementation of basename
*
* Make sure the GNU implementation of basename will be used and not the
* posix one. The GNU implementation is read-only and won't change the value
* of path. For more information consult GNU basename manual.
*
* A pointer for a portion of the path string will be returned and shoudn't be
* freed. If you need to keep it you need to strdup it.
*/
char *sys_basename(const char *path);
/**
* @brief Join 2 paths using the default path separator.
*
+4 -4
View File
@@ -17,7 +17,7 @@
*
*/
#define _GNU_SOURCE // for basename()
#define _GNU_SOURCE
#include <getopt.h>
#include <locale.h>
#include <stdio.h>
@@ -81,8 +81,8 @@ static const struct option prog_opts[] = {
static void print_help(const char *name)
{
print("Usage:\n");
print(" %s [OPTION...]\n", basename((char *)name));
print(" or %s [OPTION...] SUBCOMMAND [OPTION...]\n\n", basename((char *)name));
print(" %s [OPTION...]\n", sys_basename(name));
print(" or %s [OPTION...] SUBCOMMAND [OPTION...]\n\n", sys_basename(name));
print("Help Options:\n");
print(" -h, --help Show help options\n");
print(" -v, --version Output version information and exit\n\n");
@@ -95,7 +95,7 @@ static void print_help(const char *name)
entry++;
}
print("\n");
print("To view subcommand options, run `%s SUBCOMMAND --help'\n", basename((char *)name));
print("To view subcommand options, run `%s SUBCOMMAND --help'\n", sys_basename(name));
}
/* this function prints the copyright message for the --version command */
+2 -3
View File
@@ -23,7 +23,6 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
@@ -59,8 +58,8 @@ void telemetry(telem_prio_t level, const char *class, const char *fmt, ...)
close(fd);
filename_n = basename(filename);
if (!filename_n) {
filename_n = sys_basename(filename);
if (!filename_n || !filename_n[0]) {
free_string(&filename);
goto error;
}
-1
View File
@@ -25,7 +25,6 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>