Skip to content

Commit

Permalink
replace strdup() with our cs_strdup(), which call cs_mem_malloc() int…
Browse files Browse the repository at this point in the history
…ernally
  • Loading branch information
aquynh committed Jan 15, 2014
1 parent fc947e0 commit a9ffb44
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions arch/AArch64/AArch64BaseInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion arch/AArch64/AArch64GenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions arch/AArch64/AArch64InstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -780,15 +780,15 @@ 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)
SStream_concat(O, ", ");
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);
Expand All @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion arch/ARM/ARMGenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion arch/Mips/MipsGenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion arch/Mips/MipsInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion arch/PowerPC/PPCGenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86ATTInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86GenAsmWriter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86GenAsmWriter1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/X86IntelInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
11 changes: 11 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a9ffb44

Please sign in to comment.