Commit 71e8dd8c authored by Lisa's avatar Lisa

Fix arg parsing in akabei-create-db: exit cleanly in the main rather than in...

Fix arg parsing in akabei-create-db: exit cleanly in the main rather than in the validator, remove out-of-bounds accesses to arg list.
parent b3a55dc9
......@@ -15,57 +15,69 @@
Q_GLOBAL_STATIC_WITH_ARGS(QTextStream, in, (stdin));
namespace
{
void printHelp() {
(*out) << "Usage: akabei-create-db [action] [database] [targets]" << endl;
(*out) << "Usage: akabei-create-db --help" << endl;
}
}
namespace InputValidator
{
void validate(const QStringList &args)
bool validate(const QStringList &args)
{
if (args.at(1) == QLatin1String("--help")) {
(*out) << QObject::tr("Usage: akabei-create-db [action] [database] [targets]") << endl;
exit(0);
} else {
if (args.count() < 3) {
(*out) << QObject::tr("Not enough input arguments!") << endl;
(*out) << QObject::tr("Usage: akabei-create-db [action] [database] [targets]") << endl;
exit(1);
}
if (args.count() <= 1 || args.at(1) == QLatin1String("--help")) {
printHelp();
return false;
}
if (args.count() < 3) {
(*out) << QObject::tr("Not enough input arguments!") << endl;
printHelp();
return false;
}
if (args.at(1) == QLatin1String("generate")) {
if (!isValidDatabase(args.at(2))) {
(*out) << QObject::tr("%1 is not a valid database filename. Examples: core.db or core.db.tar.xz").arg(args.at(2)) << endl;
exit(1);
return false;
}
if (QFile::exists(args.at(2))) {
(*out) << QObject::tr("%1 already exists. Do you want to overwrite it? [y/N]").arg(args.at(2)) << endl;
QString answer = in->readLine();
if ((answer.isEmpty()) || ((answer != QLatin1String("y")) && (answer != QLatin1String("Y")))) {
(*out) << QObject::tr("Not overwriting. Exiting.") << endl;
exit(0);
return false;
}
}
} else if (args.at(1) == QLatin1String("remove")) {
if (!isValidDatabase(args.at(2))) {
(*out) << QObject::tr("%1 is not a valid database filename. Examples: core.db or core.db.tar.xz").arg(args.at(2)) << endl;
exit(1);
return false;
}
if (!QFile::exists(args.at(2))) {
(*out) << QObject::tr("%1 could not be found").arg(args.at(2)) << endl;
exit(1);
return false;
}
} else if (args.at(1) == QLatin1String("add")) {
if (!isValidDatabase(args.at(2))) {
(*out) << QObject::tr("%1 is not a valid database filename. Examples: core.db or core.db.tar.xz").arg(args.at(2)) << endl;
exit(1);
return false;
}
if (!QFile::exists(args.at(2))) {
(*out) << QObject::tr("%1 does not exist. Exiting.").arg(args.at(2)) << endl;
exit(1);
return false;
}
} else {
(*out) << QObject::tr("%1 is not a valid action. Valid actions: generate, add, remove.").arg(args.at(2)) << endl;
exit(1);
return false;
}
return true;
}
bool isValidDatabase(const QString &db)
......
......@@ -18,7 +18,11 @@ namespace InputValidator
{
bool isValidDatabase(const QString& db);
void validate(const QStringList & args);
// Returns true if the caller should proceed with the rest of operations,
// and false otherwise. 'False' can mean that the args were invalid, or
// that --help was used and we simply print the help message. The message
// is also printed in the invalid case.
bool validate(const QStringList & args);
} // namespace InputValidator
......
......@@ -23,7 +23,11 @@ int main(int argc, char **argv)
QStringList args = QCoreApplication::arguments();
InputValidator::validate(args);
bool should_proceed = InputValidator::validate(args);
if (!should_proceed) {
QCoreApplication::exit(1);
return 1;
}
QString command = args.at(1);
QString newDbPath = args.at(2);
......@@ -33,7 +37,6 @@ int main(int argc, char **argv)
targets.removeAt(0);
targets.removeAt(0);
CreateDb db(newDbPath, targets);
if (command == QLatin1String("generate")) {
......@@ -43,8 +46,10 @@ int main(int argc, char **argv)
} else if (command == QLatin1String("remove")) {
QTimer::singleShot(0, &db, &CreateDb::remove);
} else {
qWarning() << "No command specified, exiting.";
exit(1);
// This condition should be caught by the validator, but just in case.
qWarning() << "No valid command specified, exiting.";
QCoreApplication::exit(1);
return 1;
}
app.exec();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment