• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/core


Commit MetaInfo

修訂c39515a5eca498d541fe4e523e705cc96196639a (tree)
時間2017-08-08 17:13:00
作者George Burgess IV <gbiv@goog...>
CommiterChih-Wei Huang

Log Message

Cleanup uses of sprintf so we can deprecate it.

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

Change Summary

差異

--- a/fs_mgr/fs_mgr.c
+++ b/fs_mgr/fs_mgr.c
@@ -590,7 +590,7 @@ int fs_mgr_mount_all(struct fstab *fstab, int mount_mode)
590590 fstab->recs[top_idx].fs_type);
591591 if (fs_mgr_is_encryptable(&fstab->recs[top_idx]) &&
592592 strcmp(fstab->recs[top_idx].key_loc, KEY_IN_FOOTER)) {
593- int fd = open(fstab->recs[top_idx].key_loc, O_WRONLY, 0644);
593+ int fd = open(fstab->recs[top_idx].key_loc, O_WRONLY);
594594 if (fd >= 0) {
595595 INFO("%s(): also wipe %s\n", __func__, fstab->recs[top_idx].key_loc);
596596 wipe_block_device(fd, get_file_size(fd));
--- a/fs_mgr/fs_mgr_format.c
+++ b/fs_mgr/fs_mgr_format.c
@@ -36,7 +36,7 @@ static int format_ext4(char *fs_blkdev, char *fs_mnt_point)
3636 unsigned int nr_sec;
3737 int fd, rc = 0;
3838
39- if ((fd = open(fs_blkdev, O_WRONLY, 0644)) < 0) {
39+ if ((fd = open(fs_blkdev, O_WRONLY)) < 0) {
4040 ERROR("Cannot open block device. %s\n", strerror(errno));
4141 return -1;
4242 }
--- a/gatekeeperd/gatekeeperd.cpp
+++ b/gatekeeperd/gatekeeperd.cpp
@@ -76,7 +76,7 @@ public:
7676
7777 void store_sid(uint32_t uid, uint64_t sid) {
7878 char filename[21];
79- sprintf(filename, "%u", uid);
79+ snprintf(filename, sizeof(filename), "%u", uid);
8080 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
8181 if (fd < 0) {
8282 ALOGE("could not open file: %s: %s", filename, strerror(errno));
@@ -102,7 +102,7 @@ public:
102102
103103 void maybe_store_sid(uint32_t uid, uint64_t sid) {
104104 char filename[21];
105- sprintf(filename, "%u", uid);
105+ snprintf(filename, sizeof(filename), "%u", uid);
106106 if (access(filename, F_OK) == -1) {
107107 store_sid(uid, sid);
108108 }
@@ -111,7 +111,7 @@ public:
111111 uint64_t read_sid(uint32_t uid) {
112112 char filename[21];
113113 uint64_t sid;
114- sprintf(filename, "%u", uid);
114+ snprintf(filename, sizeof(filename), "%u", uid);
115115 int fd = open(filename, O_RDONLY);
116116 if (fd < 0) return 0;
117117 read(fd, &sid, sizeof(sid));
@@ -121,7 +121,7 @@ public:
121121
122122 void clear_sid(uint32_t uid) {
123123 char filename[21];
124- sprintf(filename, "%u", uid);
124+ snprintf(filename, sizeof(filename), "%u", uid);
125125 if (remove(filename) < 0) {
126126 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
127127 store_sid(uid, 0);
--- a/init/parser.cpp
+++ b/init/parser.cpp
@@ -12,7 +12,7 @@ void parse_error(struct parse_state *state, const char *fmt, ...)
1212 char buf[128];
1313 int off;
1414
15- snprintf(buf, 128, "%s: %d: ", state->filename, state->line);
15+ snprintf(buf, sizeof(buf), "%s: %d: ", state->filename, state->line);
1616 buf[127] = 0;
1717 off = strlen(buf);
1818
--- a/libsync/sync_test.c
+++ b/libsync/sync_test.c
@@ -92,7 +92,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
9292
9393 for (j = 0; j < 2; j++) {
9494 unsigned val = i + j * 3 + 1;
95- sprintf(str, "test_fence%d-%d", i, j);
95+ snprintf(str, sizeof(str), "test_fence%d-%d", i, j);
9696 int fd = sw_sync_fence_create(sync_timeline_fd, str, val);
9797 if (fd < 0) {
9898 printf("can't create sync pt %d: %s", val, strerror(errno));
@@ -106,7 +106,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
106106
107107 sync_data[3].thread_no = 3;
108108 for (j = 0; j < 2; j++) {
109- sprintf(str, "merged_fence%d", j);
109+ snprintf(str, sizeof(str), "merged_fence%d", j);
110110 sync_data[3].fd[j] = sync_merge(str, sync_data[0].fd[j], sync_data[1].fd[j]);
111111 if (sync_data[3].fd[j] < 0) {
112112 printf("can't merge sync pts %d and %d: %s\n",
--- a/libutils/RefBase.cpp
+++ b/libutils/RefBase.cpp
@@ -251,17 +251,22 @@ public:
251251 {
252252 Mutex::Autolock _l(mMutex);
253253 char buf[128];
254- sprintf(buf, "Strong references on RefBase %p (weakref_type %p):\n", mBase, this);
254+ snprintf(buf, sizeof(buf),
255+ "Strong references on RefBase %p (weakref_type %p):\n",
256+ mBase, this);
255257 text.append(buf);
256258 printRefsLocked(&text, mStrongRefs);
257- sprintf(buf, "Weak references on RefBase %p (weakref_type %p):\n", mBase, this);
259+ snprintf(buf, sizeof(buf),
260+ "Weak references on RefBase %p (weakref_type %p):\n",
261+ mBase, this);
258262 text.append(buf);
259263 printRefsLocked(&text, mWeakRefs);
260264 }
261265
262266 {
263267 char name[100];
264- snprintf(name, 100, DEBUG_REFS_CALLSTACK_PATH "/%p.stack", this);
268+ snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack",
269+ this);
265270 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
266271 if (rc >= 0) {
267272 write(rc, text.string(), text.length());
@@ -354,8 +359,8 @@ private:
354359 char buf[128];
355360 while (refs) {
356361 char inc = refs->ref >= 0 ? '+' : '-';
357- sprintf(buf, "\t%c ID %p (ref %d):\n",
358- inc, refs->id, refs->ref);
362+ snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n",
363+ inc, refs->id, refs->ref);
359364 out->append(buf);
360365 #if DEBUG_REFS_CALLSTACK_ENABLED
361366 out->append(refs->stack.toString("\t\t"));
--- a/toolbox/newfs_msdos.c
+++ b/toolbox/newfs_msdos.c
@@ -700,7 +700,7 @@ int newfs_msdos_main(int argc, char *argv[])
700700 (u_int)tm->tm_min));
701701 mk4(bsx->volid, x);
702702 mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
703- sprintf(buf, "FAT%u", fat);
703+ snprintf(buf, sizeof(buf), "FAT%u", fat);
704704 setstr(bsx->type, buf, sizeof(bsx->type));
705705 if (!opt_B) {
706706 x1 += sizeof(struct bsx);
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -57,16 +57,16 @@ static int ps_line(int pid, int tid)
5757 int prio, nice, rtprio, sched, psr;
5858 struct passwd *pw;
5959
60- sprintf(statline, "/proc/%d", tid ? tid : pid);
60+ snprintf(statline, sizeof(statline), "/proc/%d", tid ? tid : pid);
6161 stat(statline, &stats);
6262
6363 if(tid) {
64- sprintf(statline, "/proc/%d/task/%d/stat", pid, tid);
64+ snprintf(statline, sizeof(statline), "/proc/%d/task/%d/stat", pid, tid);
6565 cmdline[0] = 0;
6666 snprintf(macline, sizeof(macline), "/proc/%d/task/%d/attr/current", pid, tid);
6767 } else {
68- sprintf(statline, "/proc/%d/stat", pid);
69- sprintf(cmdline, "/proc/%d/cmdline", pid);
68+ snprintf(statline, sizeof(statline), "/proc/%d/stat", pid);
69+ snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid);
7070 snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid);
7171 int fd = open(cmdline, O_RDONLY);
7272 if(fd == 0) {
@@ -149,7 +149,7 @@ static int ps_line(int pid, int tid)
149149
150150 pw = getpwuid(stats.st_uid);
151151 if(pw == 0 || (display_flags & SHOW_NUMERIC_UID)) {
152- sprintf(user,"%d",(int)stats.st_uid);
152+ snprintf(user,sizeof(user),"%d",(int)stats.st_uid);
153153 } else {
154154 strcpy(user,pw->pw_name);
155155 }
@@ -208,7 +208,7 @@ static void print_exe_abi(int pid)
208208 int fd, r;
209209 char exeline[1024];
210210
211- sprintf(exeline, "/proc/%d/exe", pid);
211+ snprintf(exeline, sizeof(exeline), "/proc/%d/exe", pid);
212212 fd = open(exeline, O_RDONLY);
213213 if(fd == 0) {
214214 printf(" ");
@@ -243,7 +243,7 @@ void ps_threads(int pid)
243243 DIR *d;
244244 struct dirent *de;
245245
246- sprintf(tmp,"/proc/%d/task",pid);
246+ snprintf(tmp,sizeof(tmp),"/proc/%d/task",pid);
247247 d = opendir(tmp);
248248 if(d == 0) return;
249249
--- a/toolbox/top.c
+++ b/toolbox/top.c
@@ -265,29 +265,29 @@ static void read_procs(void) {
265265
266266 proc->pid = proc->tid = pid;
267267
268- sprintf(filename, "/proc/%d/stat", pid);
268+ snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
269269 read_stat(filename, proc);
270270
271- sprintf(filename, "/proc/%d/cmdline", pid);
271+ snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
272272 read_cmdline(filename, proc);
273273
274- sprintf(filename, "/proc/%d/status", pid);
274+ snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
275275 read_status(filename, proc);
276276
277277 read_policy(pid, proc);
278278
279279 proc->num_threads = 0;
280280 } else {
281- sprintf(filename, "/proc/%d/cmdline", pid);
281+ snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
282282 read_cmdline(filename, &cur_proc);
283283
284- sprintf(filename, "/proc/%d/status", pid);
284+ snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
285285 read_status(filename, &cur_proc);
286286
287287 proc = NULL;
288288 }
289289
290- sprintf(filename, "/proc/%d/task", pid);
290+ snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
291291 task_dir = opendir(filename);
292292 if (!task_dir) continue;
293293
@@ -302,7 +302,7 @@ static void read_procs(void) {
302302
303303 proc->pid = pid; proc->tid = tid;
304304
305- sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);
305+ snprintf(filename, sizeof(filename), "/proc/%d/task/%d/stat", pid, tid);
306306 read_stat(filename, proc);
307307
308308 read_policy(tid, proc);
@@ -491,7 +491,7 @@ static void print_procs(void) {
491491 if (user && user->pw_name) {
492492 user_str = user->pw_name;
493493 } else {
494- snprintf(user_buf, 20, "%d", proc->uid);
494+ snprintf(user_buf, sizeof(user_buf), "%d", proc->uid);
495495 user_str = user_buf;
496496 }
497497 if (!threads) {