replace strdup() with our cs_strdup(), which call cs_mem_malloc() internally

This commit is contained in:
Nguyen Anh Quynh 2014-01-15 18:27:01 +08:00
parent fc947e0419
commit a9ffb440f8
13 changed files with 28 additions and 15 deletions

View File

@ -39,7 +39,7 @@ char *NamedImmMapper_toString(NamedImmMapper *N, uint32_t Value, bool *Valid)
// return true if s1 == lower(f2), and false otherwise
static bool compare_lower_str(char *s1, char *s2)
{
char *lower = strdup(s2), *c;
char *lower = cs_strdup(s2), *c;
for (c = lower; *c; c++)
*c = tolower((int) *c);
@ -85,7 +85,7 @@ static char *utostr(uint64_t X, bool isNeg)
if (isNeg) *--BufPtr = '-'; // Add negative sign...
char *result = strdup(BufPtr);
char *result = cs_strdup(BufPtr);
return result;
}

View File

@ -11308,7 +11308,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
return false;
}
char *tmp = strdup(AsmString), *AsmMnem, *AsmOps;
char *tmp = cs_strdup(AsmString), *AsmMnem, *AsmOps;
AsmMnem = tmp;
AsmOps = strchr(tmp, ' ');
if (AsmOps) {

View File

@ -567,7 +567,7 @@ static void printSImm7ScaledOperand(MCInst *MI, unsigned OpNum,
static void printVPRRegister(MCInst *MI, unsigned OpNo, SStream *O)
{
unsigned Reg = MCOperand_getReg(MCInst_getOperand(MI, OpNo));
char *Name = strdup(getRegisterName(Reg));
char *Name = cs_strdup(getRegisterName(Reg));
Name[0] = 'v';
SStream_concat(O, "%s", Name);
cs_mem_free(Name);
@ -780,7 +780,7 @@ static void printVectorList(MCInst *MI, unsigned OpNum,
unsigned SubRegIdx = IsVec64 ? AArch64_dsub_0 : AArch64_qsub_0;
unsigned I;
for (I = 0; I < Count; I++) {
char *Name = strdup(getRegisterName(MCRegisterInfo_getSubReg(MRI, Reg, SubRegIdx++)));
char *Name = cs_strdup(getRegisterName(MCRegisterInfo_getSubReg(MRI, Reg, SubRegIdx++)));
Name[0] = 'v';
SStream_concat(O, "%s%s", Name, LayoutStr);
if (I != Count - 1)
@ -788,7 +788,7 @@ static void printVectorList(MCInst *MI, unsigned OpNum,
cs_mem_free(Name);
}
} else { // Print the register directly when NumVecs is 1.
char *Name = strdup(getRegisterName(Reg));
char *Name = cs_strdup(getRegisterName(Reg));
Name[0] = 'v';
SStream_concat(O, "%s%s", Name, LayoutStr);
cs_mem_free(Name);
@ -812,7 +812,7 @@ void AArch64_post_printer(csh handle, cs_insn *flat_insn, char *insn_asm)
void AArch64_printInst(MCInst *MI, SStream *O, void *Info)
{
if (printAliasInstr(MI, O, Info)) {
char *mnem = strdup(O->buffer);
char *mnem = cs_strdup(O->buffer);
char *tab = strchr(mnem, '\t');
if (tab) {
*tab = '\0';

View File

@ -8694,7 +8694,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
return false;
}
char *tmp = strdup(AsmString), *AsmMnem, *AsmOps;
char *tmp = cs_strdup(AsmString), *AsmMnem, *AsmOps;
AsmMnem = tmp;
AsmOps = strchr(tmp, ' ');
if (AsmOps) {

View File

@ -4562,7 +4562,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
return false;
}
char *tmp = strdup(AsmString), *AsmMnem, *AsmOps;
char *tmp = cs_strdup(AsmString), *AsmMnem, *AsmOps;
AsmMnem = tmp;
AsmOps = strchr(tmp, ' ');
if (AsmOps) {

View File

@ -199,7 +199,7 @@ void Mips_printInst(MCInst *MI, SStream *O, void *info)
printInstruction(MI, O, NULL);
else {
// fixup instruction id due to the change in alias instruction
char *mnem = strdup(O->buffer);
char *mnem = cs_strdup(O->buffer);
char *tab = strchr(mnem, '\t');
if (tab)
*tab = '\0';

View File

@ -3456,7 +3456,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS, void *info)
return false;
}
char *tmp = strdup(AsmString), *AsmMnem, *AsmOps;
char *tmp = cs_strdup(AsmString), *AsmMnem, *AsmOps;
AsmMnem = tmp;
AsmOps = strchr(tmp, ' ');
if (AsmOps) {

View File

@ -447,7 +447,7 @@ void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info)
// Try to print any aliases first.
if (printAliasInstr(MI, OS)) {
char *mnem = strdup(OS->buffer);
char *mnem = cs_strdup(OS->buffer);
char *tab = strchr(mnem, '\t');
if (tab)
*tab = '\0';

View File

@ -13374,7 +13374,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS)
return false;
}
char *tmp = strdup(AsmString), *AsmMnem, *AsmOps;
char *tmp = cs_strdup(AsmString), *AsmMnem, *AsmOps;
AsmMnem = tmp;
AsmOps = strchr(tmp, ' ');
if (AsmOps) {

View File

@ -12863,7 +12863,7 @@ static bool printAliasInstr(MCInst *MI, SStream *OS)
return false;
}
char *tmp = strdup(AsmString), *AsmMnem, *AsmOps;
char *tmp = cs_strdup(AsmString), *AsmMnem, *AsmOps;
AsmMnem = tmp;
AsmOps = strchr(tmp, ' ');
if (AsmOps) {

View File

@ -224,7 +224,7 @@ void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
// O << "\tlock\n";
if (printAliasInstr(MI, O)) {
char *mnem = strdup(O->buffer);
char *mnem = cs_strdup(O->buffer);
char *tab = strchr(mnem, '\t');
if (tab)
*tab = '\0';

11
utils.c
View File

@ -86,3 +86,14 @@ unsigned int count_positive(unsigned char *list)
return c;
}
char *cs_strdup(const char *str)
{
size_t len = strlen(str)+ 1;
void *new = cs_mem_malloc(len);
if (new == NULL)
return NULL;
return (char *)memmove(new, str, len);
}

View File

@ -49,5 +49,7 @@ unsigned int count_positive(unsigned char *list);
#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))
char *cs_strdup(const char *str);
#endif