Skip to content

Commit

Permalink
Cleanup uses of sprintf so we can deprecate it.
Browse files Browse the repository at this point in the history
Also cleans up two instances of open() with useless mode params, and
changes a few uses of snprintf to use sizeof(buffer) instead of
hardcoded buffer sizes.

Change-Id: If11591003d910c995e72ad8f75afd072c255a3c5
  • Loading branch information
gburgessiv committed Mar 8, 2016
1 parent d1ec9c4 commit e7aa2b2
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion fs_mgr/fs_mgr.c
Expand Up @@ -600,7 +600,7 @@ int fs_mgr_mount_all(struct fstab *fstab)
fstab->recs[top_idx].fs_type);
if (fs_mgr_is_encryptable(&fstab->recs[top_idx]) &&
strcmp(fstab->recs[top_idx].key_loc, KEY_IN_FOOTER)) {
int fd = open(fstab->recs[top_idx].key_loc, O_WRONLY, 0644);
int fd = open(fstab->recs[top_idx].key_loc, O_WRONLY);
if (fd >= 0) {
INFO("%s(): also wipe %s\n", __func__, fstab->recs[top_idx].key_loc);
wipe_block_device(fd, get_file_size(fd));
Expand Down
2 changes: 1 addition & 1 deletion fs_mgr/fs_mgr_format.c
Expand Up @@ -36,7 +36,7 @@ static int format_ext4(char *fs_blkdev, char *fs_mnt_point)
uint64_t dev_sz;
int fd, rc = 0;

if ((fd = open(fs_blkdev, O_WRONLY, 0644)) < 0) {
if ((fd = open(fs_blkdev, O_WRONLY)) < 0) {
ERROR("Cannot open block device. %s\n", strerror(errno));
return -1;
}
Expand Down
8 changes: 4 additions & 4 deletions gatekeeperd/gatekeeperd.cpp
Expand Up @@ -76,7 +76,7 @@ class GateKeeperProxy : public BnGateKeeperService {

void store_sid(uint32_t uid, uint64_t sid) {
char filename[21];
sprintf(filename, "%u", uid);
snprintf(filename, sizeof(filename), "%u", uid);
int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0) {
ALOGE("could not open file: %s: %s", filename, strerror(errno));
Expand All @@ -102,7 +102,7 @@ class GateKeeperProxy : public BnGateKeeperService {

void maybe_store_sid(uint32_t uid, uint64_t sid) {
char filename[21];
sprintf(filename, "%u", uid);
snprintf(filename, sizeof(filename), "%u", uid);
if (access(filename, F_OK) == -1) {
store_sid(uid, sid);
}
Expand All @@ -111,7 +111,7 @@ class GateKeeperProxy : public BnGateKeeperService {
uint64_t read_sid(uint32_t uid) {
char filename[21];
uint64_t sid;
sprintf(filename, "%u", uid);
snprintf(filename, sizeof(filename), "%u", uid);
int fd = open(filename, O_RDONLY);
if (fd < 0) return 0;
read(fd, &sid, sizeof(sid));
Expand All @@ -121,7 +121,7 @@ class GateKeeperProxy : public BnGateKeeperService {

void clear_sid(uint32_t uid) {
char filename[21];
sprintf(filename, "%u", uid);
snprintf(filename, sizeof(filename), "%u", uid);
if (remove(filename) < 0) {
ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
store_sid(uid, 0);
Expand Down
2 changes: 1 addition & 1 deletion init/parser.cpp
Expand Up @@ -12,7 +12,7 @@ void parse_error(struct parse_state *state, const char *fmt, ...)
char buf[128];
int off;

snprintf(buf, 128, "%s: %d: ", state->filename, state->line);
snprintf(buf, sizeof(buf), "%s: %d: ", state->filename, state->line);
buf[127] = 0;
off = strlen(buf);

Expand Down
4 changes: 2 additions & 2 deletions libsync/sync_test.c
Expand Up @@ -92,7 +92,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))

for (j = 0; j < 2; j++) {
unsigned val = i + j * 3 + 1;
sprintf(str, "test_fence%d-%d", i, j);
snprintf(str, sizeof(str), "test_fence%d-%d", i, j);
int fd = sw_sync_fence_create(sync_timeline_fd, str, val);
if (fd < 0) {
printf("can't create sync pt %d: %s", val, strerror(errno));
Expand All @@ -106,7 +106,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))

sync_data[3].thread_no = 3;
for (j = 0; j < 2; j++) {
sprintf(str, "merged_fence%d", j);
snprintf(str, sizeof(str), "merged_fence%d", j);
sync_data[3].fd[j] = sync_merge(str, sync_data[0].fd[j], sync_data[1].fd[j]);
if (sync_data[3].fd[j] < 0) {
printf("can't merge sync pts %d and %d: %s\n",
Expand Down
15 changes: 10 additions & 5 deletions libutils/RefBase.cpp
Expand Up @@ -190,17 +190,22 @@ class RefBase::weakref_impl : public RefBase::weakref_type
{
Mutex::Autolock _l(mMutex);
char buf[128];
sprintf(buf, "Strong references on RefBase %p (weakref_type %p):\n", mBase, this);
snprintf(buf, sizeof(buf),
"Strong references on RefBase %p (weakref_type %p):\n",
mBase, this);
text.append(buf);
printRefsLocked(&text, mStrongRefs);
sprintf(buf, "Weak references on RefBase %p (weakref_type %p):\n", mBase, this);
snprintf(buf, sizeof(buf),
"Weak references on RefBase %p (weakref_type %p):\n",
mBase, this);
text.append(buf);
printRefsLocked(&text, mWeakRefs);
}

{
char name[100];
snprintf(name, 100, DEBUG_REFS_CALLSTACK_PATH "/%p.stack", this);
snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack",
this);
int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
if (rc >= 0) {
write(rc, text.string(), text.length());
Expand Down Expand Up @@ -293,8 +298,8 @@ class RefBase::weakref_impl : public RefBase::weakref_type
char buf[128];
while (refs) {
char inc = refs->ref >= 0 ? '+' : '-';
sprintf(buf, "\t%c ID %p (ref %d):\n",
inc, refs->id, refs->ref);
snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n",
inc, refs->id, refs->ref);
out->append(buf);
#if DEBUG_REFS_CALLSTACK_ENABLED
out->append(refs->stack.toString("\t\t"));
Expand Down
2 changes: 1 addition & 1 deletion toolbox/newfs_msdos.c
Expand Up @@ -695,7 +695,7 @@ int newfs_msdos_main(int argc, char *argv[])
(u_int)tm->tm_min));
mk4(bsx->volid, x);
mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
sprintf(buf, "FAT%u", fat);
snprintf(buf, sizeof(buf), "FAT%u", fat);
setstr(bsx->type, buf, sizeof(bsx->type));
if (!opt_B) {
x1 += sizeof(struct bsx);
Expand Down
14 changes: 7 additions & 7 deletions toolbox/ps.c
Expand Up @@ -57,16 +57,16 @@ static int ps_line(int pid, int tid)
int prio, nice, rtprio, sched, psr;
struct passwd *pw;

sprintf(statline, "/proc/%d", tid ? tid : pid);
snprintf(statline, sizeof(statline), "/proc/%d", tid ? tid : pid);
stat(statline, &stats);

if(tid) {
sprintf(statline, "/proc/%d/task/%d/stat", pid, tid);
snprintf(statline, sizeof(statline), "/proc/%d/task/%d/stat", pid, tid);
cmdline[0] = 0;
snprintf(macline, sizeof(macline), "/proc/%d/task/%d/attr/current", pid, tid);
} else {
sprintf(statline, "/proc/%d/stat", pid);
sprintf(cmdline, "/proc/%d/cmdline", pid);
snprintf(statline, sizeof(statline), "/proc/%d/stat", pid);
snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid);
snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid);
int fd = open(cmdline, O_RDONLY);
if(fd == 0) {
Expand Down Expand Up @@ -149,7 +149,7 @@ static int ps_line(int pid, int tid)

pw = getpwuid(stats.st_uid);
if(pw == 0 || (display_flags & SHOW_NUMERIC_UID)) {
sprintf(user,"%d",(int)stats.st_uid);
snprintf(user,sizeof(user),"%d",(int)stats.st_uid);
} else {
strcpy(user,pw->pw_name);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ static void print_exe_abi(int pid)
int fd, r;
char exeline[1024];

sprintf(exeline, "/proc/%d/exe", pid);
snprintf(exeline, sizeof(exeline), "/proc/%d/exe", pid);
fd = open(exeline, O_RDONLY);
if(fd == 0) {
printf(" ");
Expand Down Expand Up @@ -243,7 +243,7 @@ void ps_threads(int pid)
DIR *d;
struct dirent *de;

sprintf(tmp,"/proc/%d/task",pid);
snprintf(tmp,sizeof(tmp),"/proc/%d/task",pid);
d = opendir(tmp);
if(d == 0) return;

Expand Down
16 changes: 8 additions & 8 deletions toolbox/top.c
Expand Up @@ -258,29 +258,29 @@ static void read_procs(void) {

proc->pid = proc->tid = pid;

sprintf(filename, "/proc/%d/stat", pid);
snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
read_stat(filename, proc);

sprintf(filename, "/proc/%d/cmdline", pid);
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
read_cmdline(filename, proc);

sprintf(filename, "/proc/%d/status", pid);
snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
read_status(filename, proc);

read_policy(pid, proc);

proc->num_threads = 0;
} else {
sprintf(filename, "/proc/%d/cmdline", pid);
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
read_cmdline(filename, &cur_proc);

sprintf(filename, "/proc/%d/status", pid);
snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
read_status(filename, &cur_proc);

proc = NULL;
}

sprintf(filename, "/proc/%d/task", pid);
snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
task_dir = opendir(filename);
if (!task_dir) continue;

Expand All @@ -295,7 +295,7 @@ static void read_procs(void) {

proc->pid = pid; proc->tid = tid;

sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);
snprintf(filename, sizeof(filename), "/proc/%d/task/%d/stat", pid, tid);
read_stat(filename, proc);

read_policy(tid, proc);
Expand Down Expand Up @@ -484,7 +484,7 @@ static void print_procs(void) {
if (user && user->pw_name) {
user_str = user->pw_name;
} else {
snprintf(user_buf, 20, "%d", proc->uid);
snprintf(user_buf, sizeof(user_buf), "%d", proc->uid);
user_str = user_buf;
}
if (!threads) {
Expand Down

0 comments on commit e7aa2b2

Please sign in to comment.