Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Correct usage of asprintf(3)

From Tiago Cunha:

Previous use of asprintf(3) relied on implementation-specific behavior
of setting the destination pointer to NULL, rather than on the defined
behavior of returning -1.

From asprintf(3):
   If sufficient space cannot be allocated, these functions will
   return -1.  The value of ret in this situation is
   implementation-dependent (on OpenBSD, ret will be set to the null
   pointer, but this behavior should not be relied upon).

This corrects that.
  • Loading branch information...
commit 1ae9828f4580dd0e0031395a8faf16b25dbf8e96 1 parent 3ad8460
@ryanflannery authored
Showing with 18 additions and 22 deletions.
  1. +4 −6 commands.c
  2. +2 −3 keybindings.c
  3. +2 −4 meta_info.c
  4. +10 −9 vitunes.c
View
10 commands.c
@@ -306,9 +306,8 @@ cmd_write(int argc, char *argv[])
bool will_clobber;
/* build filename for playlist */
- asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, argv[1]);
- if (filename == NULL)
- err(1, "cmd_write: asprintf(3) failed");
+ if (asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, argv[1]) == -1)
+ err(1, "cmd_write: asprintf failed");
/* check to see if playlist with that name already exists */
will_clobber = false;
@@ -408,9 +407,8 @@ cmd_new(int argc, char *argv[])
}
name = argv[1];
- asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, name);
- if (filename == NULL)
- err(1, "cmd_new: asprintf(3) failed");
+ if (asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, name) == -1)
+ err(1, "cmd_new: asprintf failed");
}
/* create & setup playlist */
View
5 keybindings.c
@@ -1028,9 +1028,8 @@ kba_cut(KbaArgs a UNUSED)
return;
}
- asprintf(&warning, "Are you sure you want to delete '%s'?", p->name);
- if (warning == NULL)
- err(1, "cut: asprintf(3) failed");
+ if (asprintf(&warning, "Are you sure you want to delete '%s'?", p->name) == -1)
+ err(1, "cut: asprintf failed");
/* make sure user wants this */
if (user_get_yesno(warning, &response) != 0) {
View
6 meta_info.c
@@ -225,15 +225,13 @@ mi_extract(const char *filename)
/* track number */
if (taglib_tag_track(tag) > 0) {
- asprintf(&(mi->cinfo[MI_CINFO_TRACK]), "%3i", taglib_tag_track(tag));
- if (mi->cinfo[MI_CINFO_TRACK] == NULL)
+ if (asprintf(&(mi->cinfo[MI_CINFO_TRACK]), "%3i", taglib_tag_track(tag)) == -1)
err(1, "mi_extract: asprintf failed for CINFO_TRACK");
}
/* year */
if (taglib_tag_year(tag) > 0) {
- asprintf(&(mi->cinfo[MI_CINFO_YEAR]), "%i", taglib_tag_year(tag));
- if (mi->cinfo[MI_CINFO_YEAR] == NULL)
+ if (asprintf(&(mi->cinfo[MI_CINFO_YEAR]), "%i", taglib_tag_year(tag)) == -1)
err(1, "mi_extract: asprintf failed for CINFO_YEAR");
}
View
19 vitunes.c
@@ -99,15 +99,16 @@ main(int argc, char *argv[])
errx(1, "$HOME not set. Can't find my config files.");
/* build paths & other needed strings */
- asprintf(&vitunes_dir, VITUNES_DIR_FMT, home);
- asprintf(&conf_file, CONF_FILE_FMT, home);
- asprintf(&db_file, DB_FILE_FMT, home);
- asprintf(&playlist_dir, PLAYLIST_DIR_FMT, home);
- asprintf(&player_backend, "%s", DEFAULT_PLAYER_BACKEND);
- if (vitunes_dir == NULL || conf_file == NULL
- || db_file == NULL || playlist_dir == NULL
- || player_backend == NULL)
- err(1, "failed to create needed file names");
+ if (asprintf(&vitunes_dir, VITUNES_DIR_FMT, home) == -1)
+ err(1, "main: asprintf failed");
+ if (asprintf(&conf_file, CONF_FILE_FMT, home) == -1)
+ err(1, "main: asprintf failed");
+ if (asprintf(&db_file, DB_FILE_FMT, home) == -1)
+ err(1, "main: asprintf failed");
+ if (asprintf(&playlist_dir, PLAYLIST_DIR_FMT, home) == -1)
+ err(1, "main: asprintf failed");
+ if (asprintf(&player_backend, "%s", DEFAULT_PLAYER_BACKEND) == -1)
+ err(1, "main: asprintf failed");
/* handle command-line switches & e-commands */
handle_switches(argc, argv);
Please sign in to comment.
Something went wrong with that request. Please try again.