Tools: handle file opening failure
Most of the cases, a file handle (stdin/out) is opened without checking for error. That operation may still fail, so check for it. Change-Id: I30c3e7b40858acd8b1662622129bd6557722dccd Reviewed-by: Ahmad Samir <a.samirh78@gmail.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
2a7c71bef0
commit
4183fa4c0b
|
|
@ -449,11 +449,14 @@ int runMoc(int argc, char **argv)
|
|||
|
||||
if (filename.isEmpty()) {
|
||||
filename = QStringLiteral("standard input");
|
||||
in.open(stdin, QIODevice::ReadOnly);
|
||||
if (!in.open(stdin, QIODevice::ReadOnly)) {
|
||||
fprintf(stderr, "moc: cannot open standard input: %s\n", qPrintable(in.errorString()));
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
in.setFileName(filename);
|
||||
if (!in.open(QIODevice::ReadOnly)) {
|
||||
fprintf(stderr, "moc: %s: No such file\n", qPrintable(filename));
|
||||
fprintf(stderr, "moc: cannot open %s: %s\n", qPrintable(filename), qPrintable(in.errorString()));
|
||||
return 1;
|
||||
}
|
||||
moc.filename = filename.toLocal8Bit();
|
||||
|
|
|
|||
|
|
@ -443,15 +443,19 @@ int main(int argc, char **argv)
|
|||
continue;
|
||||
|
||||
QFile f;
|
||||
bool fileIsOpen;
|
||||
QString fileName;
|
||||
if (arg == u'-') {
|
||||
f.open(stdin, QIODevice::ReadOnly | QIODevice::Text);
|
||||
fileName = "stdin"_L1;
|
||||
fileIsOpen = f.open(stdin, QIODevice::ReadOnly | QIODevice::Text);
|
||||
} else {
|
||||
fileName = arg;
|
||||
f.setFileName(arg);
|
||||
f.open(QIODevice::ReadOnly | QIODevice::Text);
|
||||
fileIsOpen = f.open(QIODevice::ReadOnly | QIODevice::Text);
|
||||
}
|
||||
if (!f.isOpen()) {
|
||||
if (!fileIsOpen) {
|
||||
fprintf(stderr, PROGRAMNAME ": could not open '%s': %s\n",
|
||||
qPrintable(arg), qPrintable(f.errorString()));
|
||||
qPrintable(fileName), qPrintable(f.errorString()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -477,11 +481,15 @@ int main(int argc, char **argv)
|
|||
|
||||
QFile output;
|
||||
if (outputFile.isEmpty()) {
|
||||
output.open(stdout, QIODevice::WriteOnly);
|
||||
if (!output.open(stdout, QIODevice::WriteOnly)) {
|
||||
fprintf(stderr, PROGRAMNAME ": could not open standard output: %s\n",
|
||||
qPrintable(output.errorString()));
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
output.setFileName(outputFile);
|
||||
if (!output.open(QIODevice::WriteOnly)) {
|
||||
fprintf(stderr, PROGRAMNAME ": could not open output file '%s': %s",
|
||||
fprintf(stderr, PROGRAMNAME ": could not open output file '%s': %s\n",
|
||||
qPrintable(outputFile), qPrintable(output.errorString()));
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -144,10 +144,18 @@ QDBusIntrospection::Interfaces QDBusXmlToCpp::readInput()
|
|||
QFile input(inputFile);
|
||||
if (inputFile.isEmpty() || inputFile == "-"_L1) {
|
||||
reporter.setFileName("<standard input>"_L1);
|
||||
input.open(stdin, QIODevice::ReadOnly);
|
||||
if (!input.open(stdin, QIODevice::ReadOnly)) {
|
||||
fprintf(stderr, PROGRAMNAME ": could not open standard input: %s\n",
|
||||
qPrintable(input.errorString()));
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
reporter.setFileName(inputFile);
|
||||
input.open(QIODevice::ReadOnly);
|
||||
if (!input.open(QIODevice::ReadOnly)) {
|
||||
fprintf(stderr, PROGRAMNAME ": could not open input file '%s': %s\n",
|
||||
qPrintable(inputFile), qPrintable(input.errorString()));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray data = input.readAll();
|
||||
|
|
|
|||
|
|
@ -348,7 +348,12 @@ void CppGenerator::operator () ()
|
|||
|
||||
{ // decls...
|
||||
QFile f (declFileName);
|
||||
f.open (QFile::WriteOnly);
|
||||
if (! f.open (QFile::WriteOnly))
|
||||
{
|
||||
fprintf (stderr, "*** cannot create %s: %s\n",
|
||||
qPrintable(declFileName), qPrintable(f.errorString()));
|
||||
return;
|
||||
}
|
||||
QTextStream out (&f);
|
||||
|
||||
QString prot = declFileName.toUpper ().replace (QLatin1Char ('.'), QLatin1Char ('_'));
|
||||
|
|
@ -380,7 +385,12 @@ void CppGenerator::operator () ()
|
|||
|
||||
{ // bits...
|
||||
QFile f (bitsFileName);
|
||||
f.open (QFile::WriteOnly);
|
||||
if (! f.open (QFile::WriteOnly))
|
||||
{
|
||||
fprintf (stderr, "*** cannot create %s: %s\n",
|
||||
qPrintable(bitsFileName), qPrintable(f.errorString()));
|
||||
return;
|
||||
}
|
||||
QTextStream out (&f);
|
||||
|
||||
// copyright headers must come first, otherwise the headers tests will fail
|
||||
|
|
@ -401,7 +411,12 @@ void CppGenerator::operator () ()
|
|||
if (! grammar.decl_file_name.isEmpty ())
|
||||
{
|
||||
QFile f (grammar.decl_file_name);
|
||||
f.open (QFile::WriteOnly);
|
||||
if (! f.open (QFile::WriteOnly))
|
||||
{
|
||||
fprintf (stderr, "*** cannot create %s: %s\n",
|
||||
qPrintable(grammar.decl_file_name), qPrintable(f.errorString()));
|
||||
return;
|
||||
}
|
||||
QTextStream out (&f);
|
||||
out << p.decls();
|
||||
}
|
||||
|
|
@ -409,7 +424,12 @@ void CppGenerator::operator () ()
|
|||
if (! grammar.impl_file_name.isEmpty ())
|
||||
{
|
||||
QFile f (grammar.impl_file_name);
|
||||
f.open (QFile::WriteOnly);
|
||||
if (! f.open (QFile::WriteOnly))
|
||||
{
|
||||
fprintf (stderr, "*** cannot create %s: %s\n",
|
||||
qPrintable(grammar.impl_file_name), qPrintable(f.errorString()));
|
||||
return;
|
||||
}
|
||||
QTextStream out (&f);
|
||||
out << p.impls();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -303,7 +303,8 @@ int runRcc(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
QFile errorDevice;
|
||||
errorDevice.open(stderr, QIODevice::WriteOnly|QIODevice::Text);
|
||||
if (!errorDevice.open(stderr, QIODevice::WriteOnly|QIODevice::Text))
|
||||
return 1;
|
||||
|
||||
if (library.verbose())
|
||||
errorDevice.write("Qt resource compiler\n");
|
||||
|
|
@ -341,7 +342,12 @@ int runRcc(int argc, char *argv[])
|
|||
mode &= ~QIODevice::Text;
|
||||
#endif // Q_OS_WIN
|
||||
// using this overload close() only flushes.
|
||||
out.open(stdout, mode);
|
||||
if (!out.open(stdout, mode)) {
|
||||
const QString msg = QString::fromLatin1("Unable to open standard output for writing: %1\n")
|
||||
.arg(out.errorString());
|
||||
errorDevice.write(msg.toUtf8());
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
out.setFileName(outFilename);
|
||||
if (!out.open(mode)) {
|
||||
|
|
|
|||
|
|
@ -245,9 +245,10 @@ bool Driver::uic(const QString &fileName, DomUI *ui, QTextStream *out)
|
|||
bool Driver::uic(const QString &fileName, QTextStream *out)
|
||||
{
|
||||
QFile f;
|
||||
if (fileName.isEmpty())
|
||||
f.open(stdin, QIODevice::ReadOnly);
|
||||
else {
|
||||
if (fileName.isEmpty()) {
|
||||
if (!f.open(stdin, QIODevice::ReadOnly))
|
||||
return false;
|
||||
} else {
|
||||
f.setFileName(fileName);
|
||||
if (!f.open(QIODevice::ReadOnly))
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -38,9 +38,10 @@ bool Uic::printDependencies()
|
|||
QString fileName = opt.inputFile;
|
||||
|
||||
QFile f;
|
||||
if (fileName.isEmpty())
|
||||
f.open(stdin, QIODevice::ReadOnly);
|
||||
else {
|
||||
if (fileName.isEmpty()) {
|
||||
if (!f.open(stdin, QIODevice::ReadOnly))
|
||||
return false;
|
||||
} else {
|
||||
f.setFileName(fileName);
|
||||
if (!f.open(QIODevice::ReadOnly))
|
||||
return false;
|
||||
|
|
|
|||
Loading…
Reference in New Issue