diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2011-04-09 14:06:12 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2011-04-09 14:06:12 +0000 |
commit | 26e7768f380625f23e4d66f34e96a480b5b00752 (patch) | |
tree | ea758adc178906d742d1e45bd1f20db8e00c4158 /llvm/lib/MC/MCDisassembler/Disassembler.cpp | |
parent | 92c07c27be6fd548fb3dcf8905c93d9f5d87139b (diff) | |
download | bcm5719-llvm-26e7768f380625f23e4d66f34e96a480b5b00752.tar.gz bcm5719-llvm-26e7768f380625f23e4d66f34e96a480b5b00752.zip |
Fix potential buffer overflow on win32.
llvm-svn: 129214
Diffstat (limited to 'llvm/lib/MC/MCDisassembler/Disassembler.cpp')
-rw-r--r-- | llvm/lib/MC/MCDisassembler/Disassembler.cpp | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/llvm/lib/MC/MCDisassembler/Disassembler.cpp b/llvm/lib/MC/MCDisassembler/Disassembler.cpp index 47071981248..6ccade99aa0 100644 --- a/llvm/lib/MC/MCDisassembler/Disassembler.cpp +++ b/llvm/lib/MC/MCDisassembler/Disassembler.cpp @@ -128,9 +128,9 @@ public: } // namespace // -// LLVMDisasmInstruction() disassmbles a single instruction using the +// LLVMDisasmInstruction() disassembles a single instruction using the // disassembler context specified in the parameter DC. The bytes of the -// instuction are specified in the parameter Bytes, and contains at least +// instruction are specified in the parameter Bytes, and contains at least // BytesSize number of bytes. The instruction is at the address specified by // the PC parameter. If a valid instruction can be disassembled its string is // returned indirectly in OutString which whos size is specified in the @@ -155,16 +155,13 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, std::string InsnStr; raw_string_ostream OS(InsnStr); - raw_ostream &Out = OS; - IP->printInst(&Inst, Out); - - std::string p; - p = OS.str(); -#ifdef LLVM_ON_WIN32 - sprintf(OutString, "%s", p.c_str()); -#else - snprintf(OutString, OutStringSize, "%s", p.c_str()); -#endif + IP->printInst(&Inst, OS); + OS.flush(); + + size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); + std::memcpy(OutString, InsnStr.data(), OutputSize); + OutString[OutputSize] = '\0'; // Terminate string. + return Size; } |