Skip to content

Commit

Permalink
explicit validation on non-string inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
whatdoineed2do/Ray committed Apr 22, 2019
1 parent b3c6a2b commit 1660999
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/audiotag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ int main(int argc, char *argv[])
MP3_TAG_ERR("no files specified");
AudioTag::_usage();
}
if ( !opts.iflds.validate()) {
MP3_TAG_ERR("invalid options");
AudioTag::_usage();
}

const char* l;
if ( (l = AudioTag::_setlocale(opts.locale)) == NULL) {
Expand Down
31 changes: 30 additions & 1 deletion src/audiotagmeta.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "audiotagmeta.h"

#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <taglib/tstring.h>
#include <taglib/tpropertymap.h>
Expand Down Expand Up @@ -42,7 +47,31 @@ void Input::populate(const TagLib::Tag* tag_)

bool Input::validate() const
{
return true;
bool good = true;
if (good && trackno)
{
unsigned x = 0;
good = sscanf(trackno, "%ld", &x) == 1;
}
if (good && yr)
{
unsigned x = 0;
good = sscanf(yr, "%ld", &x) == 1;
}
if (good && disc)
{
// expect x/y
int x = -1, y = -1;
good = sscanf(disc, "%d/%d", &x, &y) == 2;
}
if (good && date)
{
struct tm tm;
char* ret = strptime(date, "%Y-%m-%d", &tm);
good = (ret && *ret == '\0');
}

return good;
}


Expand Down
6 changes: 4 additions & 2 deletions src/audiotagmeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Input
const char* trackno; // TRCK

// this block are valid tags but no direct taglib get/set i/f
/*const char* date; // TDRC, DATE*/
const char* date; // TDRC, DATE
const char* disc; // TPOS or DISCNUMBER or disc (as x/y)
const char* albumartist; // TPE2, ALBUMARTIST, aArt

Expand All @@ -84,6 +84,7 @@ class Input
yr = NULL;
trackno = NULL;

date = NULL;
disc = NULL;
albumartist = NULL;

Expand All @@ -99,6 +100,7 @@ class Input
genre(NULL),
yr(NULL),
trackno(NULL),
date(NULL),
disc(NULL),
albumartist(NULL)
{ reset(); }
Expand All @@ -116,7 +118,7 @@ class Input
}

operator bool() const
{ return artist || album || title || comment || genre || yr || trackno || disc || albumartist || !properties.isEmpty(); }
{ return artist || album || title || comment || genre || yr || trackno || date || disc || albumartist || !properties.isEmpty(); }


void strip()
Expand Down

0 comments on commit 1660999

Please sign in to comment.