summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Moore <Robert.Moore@intel.com>2012-11-08 13:27:52 -0800
committerRobert Moore <Robert.Moore@intel.com>2012-11-08 13:27:52 -0800
commit898b052723f64eff49cde99e337b5350e96fa8c8 (patch)
tree41c1390aba11bbc6844a615422dbc1a101251f85
parent29f4970e2f868b3225f69dee7a78a8bd142d4ec1 (diff)
downloadacpica-898b052723f64eff49cde99e337b5350e96fa8c8.tar.gz
iASL/Tools: Standardize use of stream I/O functions.
1) Check for I/O error after every fopen/fread/fwrite 2) Ensure proper order of size/count params for fread/fwrite 3) Use test of Actual != Requested after all fwrite, most fread 4) Standardize I/O error messages Improve reliability and maintainability of the code. Bob Moore, Lv Zheng.
-rw-r--r--source/common/adfile.c17
-rw-r--r--source/compiler/aslcompile.c6
-rw-r--r--source/compiler/aslerror.c9
-rw-r--r--source/compiler/aslfiles.c26
-rw-r--r--source/compiler/asllisting.c45
-rw-r--r--source/compiler/dttemplate.c27
-rw-r--r--source/compiler/prutils.c15
-rw-r--r--source/components/debugger/dbfileio.c26
-rw-r--r--source/tools/acpibin/abcompare.c40
-rw-r--r--source/tools/acpisrc/asfile.c2
-rw-r--r--source/tools/acpixtract/acpixtract.c10
11 files changed, 142 insertions, 81 deletions
diff --git a/source/common/adfile.c b/source/common/adfile.c
index 426a30046..e784177a4 100644
--- a/source/common/adfile.c
+++ b/source/common/adfile.c
@@ -196,19 +196,24 @@ AdWriteBuffer (
char *Buffer,
UINT32 Length)
{
- FILE *fp;
+ FILE *File;
ACPI_SIZE Actual;
- fp = fopen (Filename, "wb");
- if (!fp)
+ File = fopen (Filename, "wb");
+ if (!File)
{
- printf ("Couldn't open %s\n", Filename);
+ printf ("Could not open file %s\n", Filename);
return (-1);
}
- Actual = fwrite (Buffer, (size_t) Length, 1, fp);
- fclose (fp);
+ Actual = fwrite (Buffer, 1, (size_t) Length, File);
+ if (Actual != Length)
+ {
+ printf ("Could not write to file %s\n", Filename);
+ }
+
+ fclose (File);
return ((INT32) Actual);
}
diff --git a/source/compiler/aslcompile.c b/source/compiler/aslcompile.c
index cd1d60fd9..186ff4bb1 100644
--- a/source/compiler/aslcompile.c
+++ b/source/compiler/aslcompile.c
@@ -355,7 +355,7 @@ FlConsumeAnsiComment (
BOOLEAN ClosingComment = FALSE;
- while (fread (&Byte, 1, 1, Handle))
+ while (fread (&Byte, 1, 1, Handle) == 1)
{
/* Scan until comment close is found */
@@ -398,7 +398,7 @@ FlConsumeNewComment (
UINT8 Byte;
- while (fread (&Byte, 1, 1, Handle))
+ while (fread (&Byte, 1, 1, Handle) == 1)
{
Status->Offset++;
@@ -449,7 +449,7 @@ FlCheckForAscii (
/* Read the entire file */
- while (fread (&Byte, 1, 1, Handle))
+ while (fread (&Byte, 1, 1, Handle) == 1)
{
/* Ignore comment fields (allow non-ascii within) */
diff --git a/source/compiler/aslerror.c b/source/compiler/aslerror.c
index d4b61668f..bc547edc7 100644
--- a/source/compiler/aslerror.c
+++ b/source/compiler/aslerror.c
@@ -366,7 +366,7 @@ AePrintException (
else
{
RActual = fread (&SourceByte, 1, 1, SourceFile);
- if (!RActual)
+ if (RActual != 1)
{
fprintf (OutputFile,
"[*** iASL: Read error on source code temp file %s ***]",
@@ -383,6 +383,13 @@ AePrintException (
}
RActual = fread (&SourceByte, 1, 1, SourceFile);
+ if (RActual != 1)
+ {
+ fprintf (OutputFile,
+ "[*** iASL: Read error on source code temp file %s ***]",
+ Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename);
+ return;
+ }
Total++;
}
diff --git a/source/compiler/aslfiles.c b/source/compiler/aslfiles.c
index 1309d754d..88773a985 100644
--- a/source/compiler/aslfiles.c
+++ b/source/compiler/aslfiles.c
@@ -215,15 +215,14 @@ FlOpenFile (
File = fopen (Filename, Mode);
-
- Gbl_Files[FileId].Filename = Filename;
- Gbl_Files[FileId].Handle = File;
-
if (!File)
{
FlFileError (FileId, ASL_MSG_OPEN);
AslAbort ();
}
+
+ Gbl_Files[FileId].Filename = Filename;
+ Gbl_Files[FileId].Handle = File;
}
@@ -288,7 +287,7 @@ FlReadFile (
/* Read and check for error */
Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
- if (Actual != Length)
+ if (Actual < Length)
{
if (feof (Gbl_Files[FileId].Handle))
{
@@ -731,16 +730,17 @@ FlOpenIncludeWithPrefix (
/* Attempt to open the file, push if successful */
IncludeFile = fopen (Pathname, "r");
- if (IncludeFile)
+ if (!IncludeFile)
{
- /* Push the include file on the open input file stack */
-
- AslPushInputFileStack (IncludeFile, Pathname);
- return (IncludeFile);
+ fprintf (stderr, "Could not open include file %s\n", Pathname);
+ ACPI_FREE (Pathname);
+ return (NULL);
}
- ACPI_FREE (Pathname);
- return (NULL);
+ /* Push the include file on the open input file stack */
+
+ AslPushInputFileStack (IncludeFile, Pathname);
+ return (IncludeFile);
}
@@ -1024,7 +1024,7 @@ FlOpenMiscOutputFiles (
return (AE_OK);
}
- /* Create/Open a combined source output file */
+ /* Create/Open a combined source output file */
Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
if (!Filename)
diff --git a/source/compiler/asllisting.c b/source/compiler/asllisting.c
index 8064955c6..d22907bce 100644
--- a/source/compiler/asllisting.c
+++ b/source/compiler/asllisting.c
@@ -207,6 +207,10 @@ LsTreeWriteWalk (
UINT32 Level,
void *Context);
+static UINT32
+LsReadAmlOutputFile (
+ UINT8 *Buffer);
+
/*******************************************************************************
*
@@ -1354,6 +1358,38 @@ LsDoHexOutput (
/*******************************************************************************
*
+ * FUNCTION: LsReadAmlOutputFile
+ *
+ * PARAMETERS: Buffer - Where to return data
+ *
+ * RETURN: None.
+ *
+ * DESCRIPTION: Read a line of the AML output prior to formatting the data
+ *
+ ******************************************************************************/
+
+static UINT32
+LsReadAmlOutputFile (
+ UINT8 *Buffer)
+{
+ UINT32 Actual;
+
+
+ Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,
+ Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
+
+ if (ferror (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle))
+ {
+ FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
+ AslAbort ();
+ }
+
+ return (Actual);
+}
+
+
+/*******************************************************************************
+ *
* FUNCTION: LsDoHexOutputC
*
* PARAMETERS: None
@@ -1391,8 +1427,7 @@ LsDoHexOutputC (
{
/* Read enough bytes needed for one output line */
- LineLength = fread (FileData, 1, HEX_TABLE_LINE_SIZE,
- Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
+ LineLength = LsReadAmlOutputFile (FileData);
if (!LineLength)
{
break;
@@ -1479,8 +1514,7 @@ LsDoHexOutputAsl (
{
/* Read enough bytes needed for one output line */
- LineLength = fread (FileData, 1, HEX_TABLE_LINE_SIZE,
- Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
+ LineLength = LsReadAmlOutputFile (FileData);
if (!LineLength)
{
break;
@@ -1566,8 +1600,7 @@ LsDoHexOutputAsm (
{
/* Read enough bytes needed for one output line */
- LineLength = fread (FileData, 1, HEX_TABLE_LINE_SIZE,
- Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
+ LineLength = LsReadAmlOutputFile (FileData);
if (!LineLength)
{
break;
diff --git a/source/compiler/dttemplate.c b/source/compiler/dttemplate.c
index 38d888035..d0777a293 100644
--- a/source/compiler/dttemplate.c
+++ b/source/compiler/dttemplate.c
@@ -352,6 +352,7 @@ DtCreateOneTemplate (
char *DisasmFilename;
FILE *File;
ACPI_STATUS Status = AE_OK;
+ ACPI_SIZE Actual;
/* New file will have a .asl suffix */
@@ -406,18 +407,32 @@ DtCreateOneTemplate (
}
else
{
- /* Special ACPI tables - DSDT, SSDT, FACS, RSDP */
+ /* Special ACPI tables - DSDT, SSDT, FADT, RSDP */
AcpiOsPrintf (" */\n\n");
if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT))
{
- fwrite (TemplateDsdt, sizeof (TemplateDsdt) -1, 1, File);
+ Actual = fwrite (TemplateDsdt, 1, sizeof (TemplateDsdt) -1, File);
+ if (Actual != sizeof (TemplateDsdt) -1)
+ {
+ fprintf (stderr,
+ "Could not write to output file %s\n", DisasmFilename);
+ Status = AE_ERROR;
+ goto Cleanup;
+ }
}
else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_SSDT))
{
- fwrite (TemplateSsdt, sizeof (TemplateSsdt) -1, 1, File);
+ Actual = fwrite (TemplateSsdt, 1, sizeof (TemplateSsdt) -1, File);
+ if (Actual != sizeof (TemplateSsdt) -1)
+ {
+ fprintf (stderr,
+ "Could not write to output file %s\n", DisasmFilename);
+ Status = AE_ERROR;
+ goto Cleanup;
+ }
}
- else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
+ else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS)) /* FADT */
{
AcpiDmDumpDataTable (ACPI_CAST_PTR (ACPI_TABLE_HEADER,
TemplateFacs));
@@ -431,7 +446,8 @@ DtCreateOneTemplate (
{
fprintf (stderr,
"%4.4s, Unrecognized ACPI table signature\n", Signature);
- return (AE_ERROR);
+ Status = AE_ERROR;
+ goto Cleanup;
}
}
@@ -439,6 +455,7 @@ DtCreateOneTemplate (
"Created ACPI table template for [%4.4s], written to \"%s\"\n",
Signature, DisasmFilename);
+Cleanup:
fclose (File);
AcpiOsRedirectOutput (stdout);
ACPI_FREE (DisasmFilename);
diff --git a/source/compiler/prutils.c b/source/compiler/prutils.c
index e1b032352..b3948f14f 100644
--- a/source/compiler/prutils.c
+++ b/source/compiler/prutils.c
@@ -409,16 +409,17 @@ PrOpenIncludeWithPrefix (
/* Attempt to open the file, push if successful */
IncludeFile = fopen (Pathname, "r");
- if (IncludeFile)
+ if (!IncludeFile)
{
- /* Push the include file on the open input file stack */
-
- PrPushInputFileStack (IncludeFile, Pathname);
- return (IncludeFile);
+ fprintf (stderr, "Could not open include file %s\n", Pathname);
+ ACPI_FREE (Pathname);
+ return (NULL);
}
- ACPI_FREE (Pathname);
- return (NULL);
+ /* Push the include file on the open input file stack */
+
+ PrPushInputFileStack (IncludeFile, Pathname);
+ return (IncludeFile);
}
diff --git a/source/components/debugger/dbfileio.c b/source/components/debugger/dbfileio.c
index 697ffe342..ac5b3202d 100644
--- a/source/components/debugger/dbfileio.c
+++ b/source/components/debugger/dbfileio.c
@@ -207,17 +207,16 @@ AcpiDbOpenDebugFile (
AcpiDbCloseDebugFile ();
AcpiGbl_DebugFile = fopen (Name, "w+");
- if (AcpiGbl_DebugFile)
- {
- AcpiOsPrintf ("Debug output file %s opened\n", Name);
- ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
- AcpiGbl_DbOutputToFile = TRUE;
- }
- else
+ if (!AcpiGbl_DebugFile)
{
AcpiOsPrintf ("Could not open debug file %s\n", Name);
+ return;
}
+ AcpiOsPrintf ("Debug output file %s opened\n", Name);
+ ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
+ AcpiGbl_DbOutputToFile = TRUE;
+
#endif
}
#endif
@@ -360,7 +359,7 @@ AcpiDbReadTable (
{
/* Read the table header */
- if (fread (&TableHeader, 1, sizeof (TableHeader), fp) !=
+ if (fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp) !=
sizeof (ACPI_TABLE_HEADER))
{
AcpiOsPrintf ("Could not read the table header\n");
@@ -459,7 +458,6 @@ AcpiDbReadTable (
AcpiOsFree (*Table);
*Table = NULL;
*TableLength = 0;
-
return (AE_ERROR);
}
@@ -557,15 +555,15 @@ AcpiDbReadTableFromFile (
char *Filename,
ACPI_TABLE_HEADER **Table)
{
- FILE *fp;
+ FILE *File;
UINT32 TableLength;
ACPI_STATUS Status;
/* Open the file */
- fp = fopen (Filename, "rb");
- if (!fp)
+ File = fopen (Filename, "rb");
+ if (!File)
{
AcpiOsPrintf ("Could not open input file %s\n", Filename);
return (AE_ERROR);
@@ -574,8 +572,8 @@ AcpiDbReadTableFromFile (
/* Get the entire file */
fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
- Status = AcpiDbReadTable (fp, Table, &TableLength);
- fclose(fp);
+ Status = AcpiDbReadTable (File, Table, &TableLength);
+ fclose(File);
if (ACPI_FAILURE (Status))
{
diff --git a/source/tools/acpibin/abcompare.c b/source/tools/acpibin/abcompare.c
index 954133b12..d221ba525 100644
--- a/source/tools/acpibin/abcompare.c
+++ b/source/tools/acpibin/abcompare.c
@@ -342,7 +342,7 @@ void
AbDisplayHeader (
char *File1Path)
{
- UINT32 Actual1;
+ UINT32 Actual;
File1 = fopen (File1Path, "rb");
@@ -352,8 +352,8 @@ AbDisplayHeader (
return;
}
- Actual1 = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
- if (Actual1 < sizeof (ACPI_TABLE_HEADER))
+ Actual = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
+ if (Actual != sizeof (ACPI_TABLE_HEADER))
{
printf ("File %s does not contain an ACPI table header\n", File1Path);
return;
@@ -380,7 +380,7 @@ void
AbComputeChecksum (
char *File1Path)
{
- UINT32 Actual1;
+ UINT32 Actual;
ACPI_TABLE_HEADER *Table;
UINT8 Checksum;
@@ -392,8 +392,8 @@ AbComputeChecksum (
return;
}
- Actual1 = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
- if (Actual1 < sizeof (ACPI_TABLE_HEADER))
+ Actual = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
+ if (Actual < sizeof (ACPI_TABLE_HEADER))
{
printf ("File %s does not contain an ACPI table header\n", File1Path);
return;
@@ -421,10 +421,10 @@ AbComputeChecksum (
/* Read the entire table, including header */
fseek (File1, 0, SEEK_SET);
- Actual1 = fread (Table, 1, Header1.Length, File1);
- if (Actual1 < Header1.Length)
+ Actual = fread (Table, 1, Header1.Length, File1);
+ if (Actual != Header1.Length)
{
- printf ("could not read table\n");
+ printf ("could not read table, length %u\n", Header1.Length);
return;
}
@@ -455,8 +455,8 @@ AbComputeChecksum (
Header1.Checksum = Checksum;
- Actual1 = fwrite (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
- if (Actual1 < sizeof (ACPI_TABLE_HEADER))
+ Actual = fwrite (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
+ if (Actual != sizeof (ACPI_TABLE_HEADER))
{
printf ("Could not write updated table header\n");
return;
@@ -506,14 +506,14 @@ AbCompareAmlFiles (
/* Read the ACPI header from each file */
Actual1 = fread (&Header1, 1, sizeof (ACPI_TABLE_HEADER), File1);
- if (Actual1 < sizeof (ACPI_TABLE_HEADER))
+ if (Actual1 != sizeof (ACPI_TABLE_HEADER))
{
printf ("File %s does not contain an ACPI table header\n", File1Path);
return (-1);
}
Actual2 = fread (&Header2, 1, sizeof (ACPI_TABLE_HEADER), File2);
- if (Actual2 < sizeof (ACPI_TABLE_HEADER))
+ if (Actual2 != sizeof (ACPI_TABLE_HEADER))
{
printf ("File %s does not contain an ACPI table header\n", File2Path);
return (-1);
@@ -552,7 +552,7 @@ AbCompareAmlFiles (
Actual2 = fread (&Char2, 1, 1, File2);
Offset = sizeof (ACPI_TABLE_HEADER);
- while (Actual1 && Actual2)
+ while ((Actual1 == 1) && (Actual2 == 1))
{
if (Char1 != Char2)
{
@@ -600,7 +600,7 @@ AbCompareAmlFiles (
/******************************************************************************
*
- * FUNCTION: AsGetFile
+ * FUNCTION: AbGetFile
*
* DESCRIPTION: Open a file and read it entirely into a new buffer
*
@@ -624,7 +624,7 @@ AbGetFile (
File = fopen (Filename, "rb");
if (!File)
{
- printf ("Could not open %s\n", Filename);
+ printf ("Could not open file %s\n", Filename);
return (NULL);
}
@@ -694,7 +694,7 @@ AbDumpAmlFile (
FileOutHandle = fopen (File2Path, "wb");
if (!FileOutHandle)
{
- printf ("Could not open %s\n", File2Path);
+ printf ("Could not open file %s\n", File2Path);
return (-1);
}
@@ -746,14 +746,14 @@ AbExtractAmlFile (
FileHandle = fopen (File1Path, "rt");
if (!FileHandle)
{
- printf ("Could not open %s\n", File1Path);
+ printf ("Could not open file %s\n", File1Path);
return (-1);
}
FileOutHandle = fopen (File2Path, "w+b");
if (!FileOutHandle)
{
- printf ("Could not open %s\n", File2Path);
+ printf ("Could not open file %s\n", File2Path);
return (-1);
}
@@ -809,7 +809,7 @@ AbExtractAmlFile (
/* Write the converted (binary) byte */
- if (fwrite (&Value, 1, 1, FileOutHandle) < 1)
+ if (fwrite (&Value, 1, 1, FileOutHandle) != 1)
{
printf ("Error writing byte %u to output file: %s\n",
Count, File2Path);
diff --git a/source/tools/acpisrc/asfile.c b/source/tools/acpisrc/asfile.c
index 885a1e417..c9e9bdc50 100644
--- a/source/tools/acpisrc/asfile.c
+++ b/source/tools/acpisrc/asfile.c
@@ -783,7 +783,7 @@ AsGetFile (
File = fopen (Filename, "rb");
if (!File)
{
- printf ("Could not open %s\n", Filename);
+ printf ("Could not open file %s\n", Filename);
return (-1);
}
diff --git a/source/tools/acpixtract/acpixtract.c b/source/tools/acpixtract/acpixtract.c
index 3d51cfe0d..30d6345b6 100644
--- a/source/tools/acpixtract/acpixtract.c
+++ b/source/tools/acpixtract/acpixtract.c
@@ -407,7 +407,7 @@ AxCountTableInstances (
InputFile = fopen (InputPathname, "rt");
if (!InputFile)
{
- printf ("Could not open %s\n", InputPathname);
+ printf ("Could not open file %s\n", InputPathname);
return (0);
}
@@ -537,7 +537,7 @@ AxExtractTables (
InputFile = fopen (InputPathname, "rt");
if (!InputFile)
{
- printf ("Could not open %s\n", InputPathname);
+ printf ("Could not open file %s\n", InputPathname);
return (-1);
}
@@ -632,7 +632,7 @@ AxExtractTables (
OutputFile = fopen (Filename, "w+b");
if (!OutputFile)
{
- printf ("Could not open %s\n", Filename);
+ printf ("Could not open file %s\n", Filename);
Status = -1;
goto CleanupAndExit;
}
@@ -667,7 +667,7 @@ AxExtractTables (
BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
if (BytesWritten != BytesConverted)
{
- printf ("Write error on %s\n", Filename);
+ printf ("Error when writing file %s\n", Filename);
fclose (OutputFile);
OutputFile = NULL;
Status = -1;
@@ -737,7 +737,7 @@ AxListTables (
InputFile = fopen (InputPathname, "rt");
if (!InputFile)
{
- printf ("Could not open %s\n", InputPathname);
+ printf ("Could not open file %s\n", InputPathname);
return (-1);
}