* Fix android compilation

- Fix static build
  - Fix dupped symbols
* Fix all errors reported by clang-analyzer
  - Some null dereferences
  - Some uninitialized variable uses
* Fix all important warnings from the farm
  - Remove *all* uses of alloca
  - Fix many %llx format string portability issues
* Fix manpage typos reported by lintian (thanks sre)
This commit is contained in:
pancake 2011-12-06 00:27:57 +01:00
parent 81124cadcd
commit f908b15fb5
23 changed files with 132 additions and 99 deletions

View File

@ -1,9 +1,7 @@
BIN=rabin2
BINDEPS=r_lib r_bin r_util r_cons r_egg r_magic r_core r_db
BINDEPS=r_lib r_magic r_core r_db r_bin r_egg r_cons
BINDEPS+=r_config r_line r_io r_cmd r_print r_flags r_asm
BINDEPS+=r_debug r_hash r_lang r_anal r_parse r_bp r_reg
BINDEPS+=r_search r_syscall r_sign r_diff r_socket r_fs r_magic
BINDEPS+=r_search r_syscall r_sign r_diff r_socket r_fs r_magic r_util
include ../binr.mk

View File

@ -429,6 +429,9 @@ int main(int argc, char **argv) {
*p2++ = 0;
data = malloc (strlen (p2));
datalen = r_hex_str2bin (p2, data);
} else {
data = NULL;
datalen = 0;
}
code = malloc (strlen (p));
codelen = r_hex_str2bin (p, code);

View File

@ -24,7 +24,7 @@ static int usage () {
" -w [off:hex] patch hexpairs at given offset\n"
" -p [padding] add padding after compilation (padding=n10s32)\n"
" ntas : begin nop, trap, 'a', sequence\n"
" NTAS : same as above, but at begining\n"
" NTAS : same as above, but at beginning\n"
" -s show assembler\n"
" -r show raw bytes instead of hexpairs\n"
" -x execute\n"

View File

@ -179,6 +179,11 @@ int main(int argc, char **argv) {
ptr += 1;
}
data = malloc (tlen);
if (!data) {
r_anal_free (anal);
r_anal_op_free (op);
return 1;
}
r_hex_str2bin ((char *)buf, data);
if (!len || len > tlen) len = tlen;
free (buf);

View File

@ -1,4 +1,4 @@
BIN=rasm2
BINDEPS=r_asm r_util r_lib r_parse r_db
BINDEPS=r_asm r_db r_util r_lib r_parse
include ../binr.mk

View File

@ -54,6 +54,8 @@ static int rasm_disasm(char *buf, ut64 offset, ut64 len, int ascii, int bin, int
ut64 word = 0, clen = 0;
if (bin) {
if (len<0)
return R_FALSE;
clen = len; // XXX
data = (ut8*)buf;
} else if (ascii) {
@ -63,7 +65,7 @@ static int rasm_disasm(char *buf, ut64 offset, ut64 len, int ascii, int bin, int
for (; *ptr; ptr++)
if (*ptr!=' ' && *ptr!='\n' && *ptr!='\r')
if (!(++word%2)) clen++;
data = malloc (clen);
data = malloc (clen+1);
if (r_hex_str2bin (buf, data)==-1)
goto beach;
}

View File

@ -39,14 +39,17 @@ static void print_address(bfd_vma address, struct disassemble_info *info) {
static int buf_fprintf(void *stream, const char *format, ...) {
va_list ap;
char *tmp;
int ret;
if (buf_global == NULL)
return 0;
va_start (ap, format);
tmp = alloca (strlen (format)+strlen (buf_global)+2);
tmp = malloc (strlen (format)+strlen (buf_global)+2);
if (!tmp) return 0;
sprintf (tmp, "%s%s", buf_global, format);
vsprintf (buf_global, tmp, ap);
ret = vsprintf (buf_global, tmp, ap);
va_end (ap);
return 0;
free (tmp);
return ret;
}
static int disassemble(RAsm *a, struct r_asm_op_t *op, const ut8 *buf, ut64 len) {

View File

@ -68,12 +68,14 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6
free (ptr);
return NULL;
}
for (tokcount=0; ; tokcount++) {
tokens[0] = NULL;
for (tokcount=0; tokcount<1023; tokcount++) {
tok = strtok (tokcount? NULL: ptr, ",");
if (tok == NULL)
break;
tokens[tokcount] = r_str_trim_head_tail (tok);
}
tokens[tokcount] = NULL;
for (at = from, matchcount = 0; at < to; at += core->blocksize-OPSZ) {
if (r_cons_singleton ()->breaked)
break;

View File

@ -1795,7 +1795,7 @@ static void r_core_magic_at(RCore *core, const char *file, ut64 addr, int depth,
if (!memcmp (q+1, "0x", 2))
sscanf (q+3, "%"PFMT64x, &addr);
else sscanf (q+1, "%"PFMT64d, &addr);
if (!*fmt) fmt = file;
if (!fmt || !*fmt) fmt = file;
r_core_magic_at (core, fmt, addr, depth, 1);
*q = '@';
}
@ -2352,10 +2352,8 @@ static int cmd_egg(void *data, const char *input) {
static int cmd_flag(void *data, const char *input) {
RCore *core = (RCore *)data;
int len = strlen (input)+1;
char *str = alloca (len);
char *str = strdup (input+1);
ut64 off = core->offset;
memcpy (str, input+1, len);
switch (*input) {
case '+':
@ -2480,6 +2478,7 @@ static int cmd_flag(void *data, const char *input) {
" fo ; show fortunes\n");
break;
}
free (str);
return 0;
}
@ -2539,20 +2538,19 @@ static void var_help() {
static int var_cmd(RCore *core, const char *str) {
RAnalFcn *fcn = r_anal_fcn_find (core->anal, core->offset,
R_ANAL_FCN_TYPE_FCN|R_ANAL_FCN_TYPE_SYM);
char *p,*p2,*p3;
int type, delta, len = strlen(str)+1;
char *p, *p2, *p3, *ostr;
int type, delta;
p = alloca (len); // XXX: remove this alloca
memcpy (p, str, len);
str = p;
ostr = p = strdup (str);
str = (const char *)ostr;
switch (*str) {
case 'V': // show vars in human readable format
r_anal_var_list_show (core->anal, fcn, core->offset);
return 0;
break;
case '?':
var_help ();
return 0;
break;
case 'v': // frame variable
case 'a': // stack arg
case 'A': // fastcall arg
@ -2590,7 +2588,7 @@ static int var_cmd(RCore *core, const char *str) {
p = strchr (str, ' ');
if (p==NULL) {
var_help();
return 0;
break;
}
p[0]='\0'; p++;
p2 = strchr (p, ' ');
@ -2608,6 +2606,7 @@ static int var_cmd(RCore *core, const char *str) {
var_help ();
break;
}
free (ostr);
return 0;
}
#endif
@ -3260,11 +3259,12 @@ static int cmd_write(void *data, const char *input) {
ut8 *buf;
const char *arg;
int wseek, i, size, len = strlen (input);
char *tmp, *str = alloca (len)+1;
char *tmp, *str, *ostr;
RCore *core = (RCore *)data;
#define WSEEK(x,y) if(wseek)r_core_seek_delta(x,y)
wseek = r_config_get_i (core->config, "cfg.wseek");
memcpy (str, input+1, len);
str = ostr = strdup (input+1);
switch (*input) {
case 'p':
if (input[1]==' ' && input[2]) {
@ -3408,29 +3408,34 @@ static int cmd_write(void *data, const char *input) {
} else eprintf ("Cannot open file '%s'\n", arg);
break;
case 'w':
str = str+1;
str++;
len = (len-1)<<1;
tmp = alloca (len);
for (i=0; i<len; i++) {
if (i%2) tmp[i] = 0;
else tmp[i] = str[i>>1];
}
str = tmp;
r_io_set_fd (core->io, core->file->fd);
r_io_write_at (core->io, core->offset, (const ut8*)str, len);
WSEEK (core, len);
r_core_block_read (core, 0);
if (len>0) tmp = malloc (len+1);
else tmp = NULL;
if (tmp) {
for (i=0; i<len; i++) {
if (i%2) tmp[i] = 0;
else tmp[i] = str[i>>1];
}
str = tmp;
r_io_set_fd (core->io, core->file->fd);
r_io_write_at (core->io, core->offset, (const ut8*)str, len);
WSEEK (core, len);
r_core_block_read (core, 0);
free (tmp);
} else eprintf ("Cannot malloc %d\n", len);
break;
case 'x':
{
int len = strlen (input);
ut8 *buf = alloca (len);
ut8 *buf = malloc (len+1);
len = r_hex_str2bin (input+1, buf);
if (len != -1) {
r_core_write_at (core, core->offset, buf, len);
WSEEK (core, len);
r_core_block_read (core, 0);
} else eprintf ("Error: invalid hexpair string\n");
free (buf);
}
break;
case 'a':
@ -3493,18 +3498,16 @@ static int cmd_write(void *data, const char *input) {
case 'b':
{
int len = strlen (input);
ut8 *buf = alloca (len);
len = r_hex_str2bin (input+1, buf);
if (len > 0) {
r_mem_copyloop (core->block, buf, core->blocksize, len);
r_core_write_at (core, core->offset, core->block, core->blocksize);
WSEEK (core, core->blocksize);
r_core_block_read (core, 0);
} else {
eprintf ("Wrong argument\n");
}
break;
ut8 *buf = malloc (len+1);
if (buf) {
len = r_hex_str2bin (input+1, buf);
if (len > 0) {
r_mem_copyloop (core->block, buf, core->blocksize, len);
r_core_write_at (core, core->offset, core->block, core->blocksize);
WSEEK (core, core->blocksize);
r_core_block_read (core, 0);
} else eprintf ("Wrong argument\n");
} else eprintf ("Cannot malloc %d\n", len+1);
}
break;
case 'm':
@ -3635,6 +3638,7 @@ static int cmd_write(void *data, const char *input) {
// " wf file o s ; write contents of file from optional offset 'o' and size 's'.\n"
break;
}
free (ostr);
return 0;
}
@ -4687,7 +4691,7 @@ static int cmd_macro(void *data, const char *input) {
static int r_core_cmd_pipe(RCore *core, char *radare_cmd, char *shell_cmd) {
#if __UNIX__
int fds[2];
int stdout_fd, status;
int stdout_fd, status = 0;
stdout_fd = dup (1);
pipe (fds);
@ -5312,12 +5316,14 @@ static int step_until(RCore *core, ut64 addr) {
static int step_line(RCore *core, int times) {
char file[512], file2[512];
int find_meta, line, line2;
int find_meta, line = -1, line2 = -1;
ut64 off = r_debug_reg_get (core->dbg, "pc");
if (off == 0LL) {
eprintf ("Cannot 'drn pc'\n");
return R_FALSE;
}
file[0] = 0;
file2[0] = 0;
if (r_bin_meta_get_line (core->bin, off, file, sizeof (file), &line)) {
eprintf ("--> 0x%08"PFMT64x" %s : %d\n", off, file, line);
eprintf ("--> %s\n", r_file_slurp_line (file, line, 0));

View File

@ -99,7 +99,7 @@ static int config_cfgdebug_callback(void *user, void *data) {
RConfigNode *node = (RConfigNode*) data;
if (core && core->io)
core->io->debug = node->i_value;
if (core->dbg && node->i_value) {
if (core && core->dbg && node->i_value) {
const char *dbgbackend = r_config_get (core->config, "dbg.backend");
r_debug_use (core->dbg, dbgbackend);
if (!strcmp (dbgbackend, "bf"))
@ -374,6 +374,7 @@ static int config_asmbits_callback(void *user, void *data) {
r_debug_set_arch (core->dbg, core->anal->cur->arch, node->i_value);
const char *asmos = r_config_get (core->config, "asm.os");
const char *asmarch = r_config_get (core->config, "asm.arch");
if (core && core->anal)
if (!r_syscall_setup (core->anal->syscall, asmarch,
asmos, node->i_value)) {
//eprintf ("asm.arch: Cannot setup syscall '%s/%s' from '%s'\n",

View File

@ -162,15 +162,15 @@ printf ("DIR(%s)\n", path);
printf ("FILE(%s)\n", p);
printf ("FILEN %d\n", n);
#endif
list = p? r_sys_dir (path):NULL;
list = p? r_sys_dir (path): NULL;
if (list) {
char buf[4096];
r_list_foreach (list, iter, str) {
if (*str == '.')
continue;
if (!*p || !memcmp (str, p, n)) {
if (!p || !*p || !memcmp (str, p, n)) {
snprintf (buf, sizeof (buf), "%s%s%s",
path, strlen(path)>1?"/":"", str);
path, strlen (path)>1?"/":"", str);
tmp_argv[i++] = strdup (buf);
if (i==TMP_ARGV_SZ)
break;
@ -708,13 +708,13 @@ reaccept:
r_socket_read_block (c, (ut8*)&bufr, 4);
r_mem_copyendian ((ut8*)&i, (ut8 *)bufr, 4, !endian);
if (i>0 && i<RMT_MAX) {
if ((cmd=malloc(i))) {
if ((cmd=malloc (i))) {
r_socket_read_block (c, (ut8*)cmd, i);
cmd[i] = '\0';
eprintf ("len: %d cmd: '%s'\n",
i, cmd); fflush(stdout);
cmd_output = r_core_cmd_str (core, cmd);
free(cmd);
free (cmd);
} else eprintf ("rap: cannot malloc\n");
} else eprintf ("rap: invalid length '%d'\n", i);
/* write */
@ -724,14 +724,14 @@ reaccept:
cmd_output = strdup("");
cmd_len = 0;
}
bufw = malloc(cmd_len + 5);
bufw = malloc (cmd_len + 5);
bufw[0] = RMT_CMD | RMT_REPLY;
r_mem_copyendian((ut8*)bufw+1, (ut8 *)&cmd_len, 4, !endian);
memcpy(bufw+5, cmd_output, cmd_len);
r_mem_copyendian ((ut8*)bufw+1, (ut8 *)&cmd_len, 4, !endian);
memcpy (bufw+5, cmd_output, cmd_len);
r_socket_write (c, bufw, cmd_len+5);
r_socket_flush (c);
free(bufw);
free(cmd_output);
free (bufw);
free (cmd_output);
break;
}
case RMT_WRITE:
@ -771,18 +771,19 @@ reaccept:
case RMT_SYSTEM:
// read
r_socket_read_block (c, buf, 4);
r_mem_copyendian((ut8*)&i, buf, 4, !endian);
r_mem_copyendian ((ut8*)&i, buf, 4, !endian);
if (i>0&&i<RMT_MAX) {
ptr = (ut8 *) malloc(i+6);
ptr = (ut8 *) malloc (i+6);
if (!ptr) return R_FALSE;
ptr[5]='!';
r_socket_read_block (c, ptr+6, i);
ptr[6+i]='\0';
//env_update();
//pipe_stdout_to_tmp_file((char*)&buf, (char*)ptr+5);
strcpy((char*)buf, "/tmp/.out");
strcpy ((char*)buf, "/tmp/.out");
pipefd = r_cons_pipe_open ((const char *)buf, 0);
//eprintf("SYSTEM(%s)\n", ptr+6);
system((const char*)ptr+6);
system ((const char*)ptr+6);
r_cons_pipe_close (pipefd);
{
FILE *fd = fopen((char*)buf, "r");
@ -791,33 +792,36 @@ reaccept:
eprintf("Cannot open tmpfile\n");
i = -1;
} else {
fseek(fd, 0, SEEK_END);
i = ftell(fd);
fseek(fd, 0, SEEK_SET);
free(ptr);
ptr = (ut8 *) malloc(i+5);
fread(ptr+5, i, 1, fd);
fseek (fd, 0, SEEK_END);
i = ftell (fd);
fseek (fd, 0, SEEK_SET);
free (ptr);
ptr = (ut8 *) malloc (i+5);
fread (ptr+5, i, 1, fd);
ptr[i+5]='\0';
fclose(fd);
fclose (fd);
}
}
{
char *out = r_file_slurp ((char*)buf, &i);
free(ptr);
free (ptr);
//eprintf("PIPE(%s)\n", out);
ptr = (ut8 *) malloc(i+5);
ptr = (ut8 *) malloc (i+5);
if (ptr) {
memcpy (ptr+5, out, i);
free(out);
free (out);
}
}
//unlink((char*)buf);
}
if (!ptr) ptr = (ut8 *) malloc (5); // malloc for 5 byets? c'mon!
if (!ptr) return R_FALSE;
// send
ptr[0] = (RMT_SYSTEM | RMT_REPLY);
r_mem_copyendian ((ut8*)ptr+1, (ut8*)&i, 4, !endian);
if (i<0)i=0;
if (i<0) i = 0;
r_socket_write (c, ptr, i+5);
r_socket_flush (c);
eprintf ("REPLY SENT (%d) (%s)\n", i, ptr+5);

View File

@ -53,7 +53,7 @@ R_API int r_core_patch (RCore *core, const char *patch) {
*q = *end = 0;
noff = r_num_math (core->num, q+2);
r_buf_append_bytes (b, (const ut8*)str, strlen (str));
snprintf (tmp, sizeof (tmp), "0x%08llx", noff);
snprintf (tmp, sizeof (tmp), "0x%08"PFMT64x, noff);
r_buf_append_bytes (b, (const ut8*)tmp, strlen (tmp));
r_buf_append_bytes (b, (const ut8*)end+1, strlen (end+1));
}

View File

@ -648,9 +648,9 @@ R_API void r_core_visual_title (RCore *core, int color) {
bar[10] = '.'; // chop cmdfmt
bar[11] = '.'; // chop cmdfmt
bar[12] = 0; // chop cmdfmt
if (curset)
if (curset)
snprintf (foo, sizeof (foo), "[0x%08"PFMT64x" %d %s(%d:%d=%d)]> %s\n", core->offset,
core->blocksize, core->file->filename, cursor, ocursor,
core->blocksize, filename, cursor, ocursor,
ocursor==-1?1:R_ABS (cursor-ocursor)+1, bar);
else
snprintf (foo, sizeof (foo), "[0x%08"PFMT64x" %d %s]> %s %s\n",

View File

@ -99,7 +99,10 @@ static char *find_include(const char *prefix, const char *file) {
else if (*prefix=='$') {
char *out = r_sys_getenv (prefix+1);
pfx = out? out: strdup ("");
} else pfx = strdup (prefix);
} else {
pfx = strdup (prefix);
if (!pfx) return NULL;
}
if (env) {
char *str, *ptr = strchr (env, ':');

View File

@ -2,6 +2,7 @@ NEXEC=egg_exec
OBJ_EXEC=${NEXEC}.o
STATIC_OBJ+=${OBJ_EXEC}
TARGET_${NEXEC}=${NEXEC}.${EXT_SO}
CFLAGS+=-DCORELIB
ALL_TARGETS+=${TARGET_${NEXEC}}

View File

@ -2,6 +2,7 @@ NXOR=egg_xor
OBJ_XOR=${NXOR}.o
STATIC_OBJ+=${OBJ_XOR}
TARGET_${NXOR}=${NXOR}.${EXT_SO}
CFLAGS+=-DCORELIB
ALL_TARGETS+=${TARGET_${NXOR}}

View File

@ -112,7 +112,7 @@ R_API RFSRoot *r_fs_mount (RFS* fs, const char *fstype, const char *path, ut64 d
return NULL;
}
r_list_append (fs->roots, root);
eprintf ("Mounted %s on %s at 0x%llx\n", fstype, str, delta);
eprintf ("Mounted %s on %s at 0x%"PFMT64x"\n", fstype, str, delta);
free (str);
return root;
}
@ -584,9 +584,10 @@ R_API int r_fs_prompt (RFS *fs, const char *root) {
input = buf+3;
while (input[0] == ' ')
input++;
if (input[0] == '/')
strncpy (str, root, sizeof (str)-1);
else strncpy (str, path, sizeof (str)-1);
if (input[0] == '/') {
if (root) strncpy (str, root, sizeof (str)-1);
else str[0] = 0;
} else strncpy (str, path, sizeof (str)-1);
strcat (str, "/");
strcat (str, input);
file = r_fs_open (fs, str);
@ -605,9 +606,11 @@ R_API int r_fs_prompt (RFS *fs, const char *root) {
input = buf+3;
while (input[0] == ' ')
input++;
if (input[0] == '/')
strncpy (str, root, sizeof (str)-1);
else strncpy (str, path, sizeof (str)-1);
if (input[0] == '/') {
if (root)
strncpy (str, root, sizeof (str)-1);
else str[0] = 0;
} else strncpy (str, path, sizeof (str)-1);
strcat (str, "/");
strcat (str, input);
file = r_fs_open (fs, str);

View File

@ -216,7 +216,7 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align)
if (extra)
extra = align - extra;
if (! p)
if (! p || !p->magic)
grub_fatal ("null in the ring");
if (p->magic != GRUB_MM_FREE_MAGIC)

View File

@ -17,6 +17,7 @@
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <r_types.h>
#include <grub/disk.h>
#include <grub/misc.h>
#include <grub/mm.h>
@ -91,9 +92,8 @@ gpt_partition_map_iterate (grub_disk_t disk,
part.index = last_offset;
part.partmap = &grub_gpt_partition_map;
grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i,
(unsigned long long) part.start,
(unsigned long long) part.len);
grub_dprintf ("gpt", "GPT entry %d: start=%"PFMT64d", length=%"PFMT64d"\n", i,
(ut64) part.start, (ut64) part.len);
if (hook (disk, &part, closure))
return grub_errno;

View File

@ -17,6 +17,7 @@
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <r_types.h>
#include <grub/partition.h>
#include <grub/msdos_partition.h>
#include <grub/disk.h>
@ -91,10 +92,10 @@ pc_partition_map_iterate (grub_disk_t disk,
p.msdostype = e->type;
grub_dprintf ("partition",
"partition %d: flag 0x%x, type 0x%x, start 0x%llx, len 0x%llx\n",
"partition %d: flag 0x%x, type 0x%x, start 0x%"PFMT64x", len 0x%"PFMT64x"\n",
p.index, e->flag, e->type,
(unsigned long long) p.start,
(unsigned long long) p.len);
(ut64) p.start,
(ut64) p.len);
/* If this is a GPT partition, this MBR is just a dummy. */
if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)

View File

@ -8,7 +8,7 @@ typedef struct r_fs_type_t {
int bytelen;
} RFSType;
RFSType fstypes[] = {
static RFSType fstypes[] = {
{ "hfsplus", 0x400, "H+", 2, 0, 0, 0x400 },
{ "fat", 0x36, "FAT12", 5, 0, 0, 0 },
{ "fat", 0x52, "FAT32", 5, 0, 0, 0 },

View File

@ -48,9 +48,9 @@ static int debug_os_read_at(int pid, ut32 *buf, int sz, ut64 addr) {
if (sz<1 || addr==UT64_MAX)
return -1;
for (x=0; x<words; x++)
buf[x] = debug_read_raw (pid, (void*)(at++));
buf[x] = (ut32)debug_read_raw (pid, (void*)(at++));
if (last) {
lr = debug_read_raw (pid, at);
lr = (ut32)debug_read_raw (pid, at);
memcpy (buf+x, &lr, last) ;
}
return sz;

View File

@ -12,7 +12,7 @@
.Op Fl o Ar file
.Op Fl dscxvh
.Sh DESCRIPTION
ragg2-cc is a frontend of CC. It allows to create tiny binaries (1KB) or shellcodes in binary or hexpairs from a C source.
ragg2-cc is a frontend of CC. It is used to creates tiny binaries (1KB) or shellcodes in binary or hexpairs from a C source.
.Pp
The compiler used is the one configured by the CC environment. This has been tested with gcc, llvm-gcc and clang.
.Pp