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 @@ ...@@ -15,57 +15,69 @@
Q_GLOBAL_STATIC_WITH_ARGS(QTextStream, in, (stdin)); 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 namespace InputValidator
{ {
void validate(const QStringList &args) bool validate(const QStringList &args)
{ {
if (args.at(1) == QLatin1String("--help")) { if (args.count() <= 1 || args.at(1) == QLatin1String("--help")) {
(*out) << QObject::tr("Usage: akabei-create-db [action] [database] [targets]") << endl; printHelp();
exit(0); return false;
} else { }
if (args.count() < 3) {
(*out) << QObject::tr("Not enough input arguments!") << endl; if (args.count() < 3) {
(*out) << QObject::tr("Usage: akabei-create-db [action] [database] [targets]") << endl; (*out) << QObject::tr("Not enough input arguments!") << endl;
exit(1); printHelp();
} return false;
} }
if (args.at(1) == QLatin1String("generate")) { if (args.at(1) == QLatin1String("generate")) {
if (!isValidDatabase(args.at(2))) { 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; (*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))) { 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; (*out) << QObject::tr("%1 already exists. Do you want to overwrite it? [y/N]").arg(args.at(2)) << endl;
QString answer = in->readLine(); QString answer = in->readLine();
if ((answer.isEmpty()) || ((answer != QLatin1String("y")) && (answer != QLatin1String("Y")))) { if ((answer.isEmpty()) || ((answer != QLatin1String("y")) && (answer != QLatin1String("Y")))) {
(*out) << QObject::tr("Not overwriting. Exiting.") << endl; (*out) << QObject::tr("Not overwriting. Exiting.") << endl;
exit(0); return false;
} }
} }
} else if (args.at(1) == QLatin1String("remove")) { } else if (args.at(1) == QLatin1String("remove")) {
if (!isValidDatabase(args.at(2))) { 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; (*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))) { if (!QFile::exists(args.at(2))) {
(*out) << QObject::tr("%1 could not be found").arg(args.at(2)) << endl; (*out) << QObject::tr("%1 could not be found").arg(args.at(2)) << endl;
exit(1); return false;
} }
} else if (args.at(1) == QLatin1String("add")) { } else if (args.at(1) == QLatin1String("add")) {
if (!isValidDatabase(args.at(2))) { 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; (*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))) { if (!QFile::exists(args.at(2))) {
(*out) << QObject::tr("%1 does not exist. Exiting.").arg(args.at(2)) << endl; (*out) << QObject::tr("%1 does not exist. Exiting.").arg(args.at(2)) << endl;
exit(1); return false;
} }
} else { } else {
(*out) << QObject::tr("%1 is not a valid action. Valid actions: generate, add, remove.").arg(args.at(2)) << endl; (*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) bool isValidDatabase(const QString &db)
......
...@@ -18,7 +18,11 @@ namespace InputValidator ...@@ -18,7 +18,11 @@ namespace InputValidator
{ {
bool isValidDatabase(const QString& db); 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 } // namespace InputValidator
......
...@@ -23,7 +23,11 @@ int main(int argc, char **argv) ...@@ -23,7 +23,11 @@ int main(int argc, char **argv)
QStringList args = QCoreApplication::arguments(); 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 command = args.at(1);
QString newDbPath = args.at(2); QString newDbPath = args.at(2);
...@@ -33,7 +37,6 @@ int main(int argc, char **argv) ...@@ -33,7 +37,6 @@ int main(int argc, char **argv)
targets.removeAt(0); targets.removeAt(0);
targets.removeAt(0); targets.removeAt(0);
CreateDb db(newDbPath, targets); CreateDb db(newDbPath, targets);
if (command == QLatin1String("generate")) { if (command == QLatin1String("generate")) {
...@@ -43,8 +46,10 @@ int main(int argc, char **argv) ...@@ -43,8 +46,10 @@ int main(int argc, char **argv)
} else if (command == QLatin1String("remove")) { } else if (command == QLatin1String("remove")) {
QTimer::singleShot(0, &db, &CreateDb::remove); QTimer::singleShot(0, &db, &CreateDb::remove);
} else { } else {
qWarning() << "No command specified, exiting."; // This condition should be caught by the validator, but just in case.
exit(1); qWarning() << "No valid command specified, exiting.";
QCoreApplication::exit(1);
return 1;
} }
app.exec(); 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