diff --git a/BaseTools/ImageTool/ElfScan.c b/BaseTools/ImageTool/ElfScan.c index 9fd50656f6..62cdfa48c5 100644 --- a/BaseTools/ImageTool/ElfScan.c +++ b/BaseTools/ImageTool/ElfScan.c @@ -784,7 +784,7 @@ ScanElf ( mImageInfo.HeaderInfo.IsXip = true; mImageInfo.SegmentInfo.SegmentAlignment = (uint32_t)mPeAlignment; mImageInfo.RelocInfo.RelocsStripped = false; - mImageInfo.DebugInfo.SymbolsPathLen = strlen (ElfName) + 1; + mImageInfo.DebugInfo.SymbolsPathLen = strlen (ElfName); switch (mEhdr->e_machine) { case EM_386: @@ -805,14 +805,14 @@ ScanElf ( return RETURN_UNSUPPORTED; } - mImageInfo.DebugInfo.SymbolsPath = calloc (1, mImageInfo.DebugInfo.SymbolsPathLen); + mImageInfo.DebugInfo.SymbolsPath = malloc (mImageInfo.DebugInfo.SymbolsPathLen + 1); if (mImageInfo.DebugInfo.SymbolsPath == NULL) { fprintf (stderr, "ImageTool: Could not allocate memory for Debug Data\n"); free (mEhdr); return RETURN_OUT_OF_RESOURCES; }; - memcpy (mImageInfo.DebugInfo.SymbolsPath, ElfName, mImageInfo.DebugInfo.SymbolsPathLen); + memmove (mImageInfo.DebugInfo.SymbolsPath, ElfName, mImageInfo.DebugInfo.SymbolsPathLen + 1); if ((strcmp (ModuleType, "BASE") == 0) || (strcmp (ModuleType, "SEC") == 0) diff --git a/BaseTools/ImageTool/PeEmit.c b/BaseTools/ImageTool/PeEmit.c index 806d7e8ddd..ccdcdac4ce 100644 --- a/BaseTools/ImageTool/PeEmit.c +++ b/BaseTools/ImageTool/PeEmit.c @@ -180,7 +180,7 @@ EmitPeGetDebugSectionSize ( if (Image->DebugInfo.SymbolsPath != NULL) { return !BaseOverflowAlignUpU32 ( - sizeof (DebugData) + Image->DebugInfo.SymbolsPathLen, + sizeof (DebugData) + Image->DebugInfo.SymbolsPathLen + 1, Image->SegmentInfo.SegmentAlignment, DebugSize ); @@ -755,18 +755,18 @@ ToolImageEmitPeDebugTable ( Context->PeHdr->SizeOfImage += ALIGN_VALUE (Context->DebugTableSize, Image->SegmentInfo.SegmentAlignment); - assert (Image->DebugInfo.SymbolsPathLen <= Context->DebugTableSize); + assert (Image->DebugInfo.SymbolsPathLen + 1 <= Context->DebugTableSize); assert (Context->DebugTableSize <= *BufferSize); Data = (DebugData *) *Buffer; Data->Dir.Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW; - Data->Dir.SizeOfData = sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Image->DebugInfo.SymbolsPathLen; + Data->Dir.SizeOfData = sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Image->DebugInfo.SymbolsPathLen + 1; Data->Dir.RVA = (Context->UnsignedFileSize - ALIGN_VALUE (Context->DebugTableSize, Context->FileAlignment)) + sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); Data->Dir.FileOffset = Data->Dir.RVA; Data->Nb10.Signature = CODEVIEW_SIGNATURE_NB10; - snprintf (Data->Name, Image->DebugInfo.SymbolsPathLen, "%s", Image->DebugInfo.SymbolsPath); + memmove (Data->Name, Image->DebugInfo.SymbolsPath, Image->DebugInfo.SymbolsPathLen + 1); *BufferSize -= Context->DebugTableSize; *Buffer += Context->DebugTableSize; diff --git a/BaseTools/ImageTool/PeScan.c b/BaseTools/ImageTool/PeScan.c index 01388ad2a0..b9013819e7 100644 --- a/BaseTools/ImageTool/PeScan.c +++ b/BaseTools/ImageTool/PeScan.c @@ -369,7 +369,7 @@ ScanPeGetDebugInfo ( return false; } - DebugInfo->SymbolsPath = calloc (1, PdbPathSize); + DebugInfo->SymbolsPath = malloc (PdbPathSize); if (DebugInfo->SymbolsPath == NULL) { fprintf (stderr, "ImageTool: Could not allocate memory for SymbolsPath\n"); return false;