system/core
修訂 | c39515a5eca498d541fe4e523e705cc96196639a (tree) |
---|---|
時間 | 2017-08-08 17:13:00 |
作者 | George Burgess IV <gbiv@goog...> |
Commiter | Chih-Wei Huang |
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
@@ -590,7 +590,7 @@ int fs_mgr_mount_all(struct fstab *fstab, int mount_mode) | ||
590 | 590 | fstab->recs[top_idx].fs_type); |
591 | 591 | if (fs_mgr_is_encryptable(&fstab->recs[top_idx]) && |
592 | 592 | 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); | |
594 | 594 | if (fd >= 0) { |
595 | 595 | INFO("%s(): also wipe %s\n", __func__, fstab->recs[top_idx].key_loc); |
596 | 596 | wipe_block_device(fd, get_file_size(fd)); |
@@ -36,7 +36,7 @@ static int format_ext4(char *fs_blkdev, char *fs_mnt_point) | ||
36 | 36 | unsigned int nr_sec; |
37 | 37 | int fd, rc = 0; |
38 | 38 | |
39 | - if ((fd = open(fs_blkdev, O_WRONLY, 0644)) < 0) { | |
39 | + if ((fd = open(fs_blkdev, O_WRONLY)) < 0) { | |
40 | 40 | ERROR("Cannot open block device. %s\n", strerror(errno)); |
41 | 41 | return -1; |
42 | 42 | } |
@@ -76,7 +76,7 @@ public: | ||
76 | 76 | |
77 | 77 | void store_sid(uint32_t uid, uint64_t sid) { |
78 | 78 | char filename[21]; |
79 | - sprintf(filename, "%u", uid); | |
79 | + snprintf(filename, sizeof(filename), "%u", uid); | |
80 | 80 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
81 | 81 | if (fd < 0) { |
82 | 82 | ALOGE("could not open file: %s: %s", filename, strerror(errno)); |
@@ -102,7 +102,7 @@ public: | ||
102 | 102 | |
103 | 103 | void maybe_store_sid(uint32_t uid, uint64_t sid) { |
104 | 104 | char filename[21]; |
105 | - sprintf(filename, "%u", uid); | |
105 | + snprintf(filename, sizeof(filename), "%u", uid); | |
106 | 106 | if (access(filename, F_OK) == -1) { |
107 | 107 | store_sid(uid, sid); |
108 | 108 | } |
@@ -111,7 +111,7 @@ public: | ||
111 | 111 | uint64_t read_sid(uint32_t uid) { |
112 | 112 | char filename[21]; |
113 | 113 | uint64_t sid; |
114 | - sprintf(filename, "%u", uid); | |
114 | + snprintf(filename, sizeof(filename), "%u", uid); | |
115 | 115 | int fd = open(filename, O_RDONLY); |
116 | 116 | if (fd < 0) return 0; |
117 | 117 | read(fd, &sid, sizeof(sid)); |
@@ -121,7 +121,7 @@ public: | ||
121 | 121 | |
122 | 122 | void clear_sid(uint32_t uid) { |
123 | 123 | char filename[21]; |
124 | - sprintf(filename, "%u", uid); | |
124 | + snprintf(filename, sizeof(filename), "%u", uid); | |
125 | 125 | if (remove(filename) < 0) { |
126 | 126 | ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno)); |
127 | 127 | store_sid(uid, 0); |
@@ -12,7 +12,7 @@ void parse_error(struct parse_state *state, const char *fmt, ...) | ||
12 | 12 | char buf[128]; |
13 | 13 | int off; |
14 | 14 | |
15 | - snprintf(buf, 128, "%s: %d: ", state->filename, state->line); | |
15 | + snprintf(buf, sizeof(buf), "%s: %d: ", state->filename, state->line); | |
16 | 16 | buf[127] = 0; |
17 | 17 | off = strlen(buf); |
18 | 18 |
@@ -92,7 +92,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) | ||
92 | 92 | |
93 | 93 | for (j = 0; j < 2; j++) { |
94 | 94 | 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); | |
96 | 96 | int fd = sw_sync_fence_create(sync_timeline_fd, str, val); |
97 | 97 | if (fd < 0) { |
98 | 98 | 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))) | ||
106 | 106 | |
107 | 107 | sync_data[3].thread_no = 3; |
108 | 108 | for (j = 0; j < 2; j++) { |
109 | - sprintf(str, "merged_fence%d", j); | |
109 | + snprintf(str, sizeof(str), "merged_fence%d", j); | |
110 | 110 | sync_data[3].fd[j] = sync_merge(str, sync_data[0].fd[j], sync_data[1].fd[j]); |
111 | 111 | if (sync_data[3].fd[j] < 0) { |
112 | 112 | printf("can't merge sync pts %d and %d: %s\n", |
@@ -251,17 +251,22 @@ public: | ||
251 | 251 | { |
252 | 252 | Mutex::Autolock _l(mMutex); |
253 | 253 | 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); | |
255 | 257 | text.append(buf); |
256 | 258 | 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); | |
258 | 262 | text.append(buf); |
259 | 263 | printRefsLocked(&text, mWeakRefs); |
260 | 264 | } |
261 | 265 | |
262 | 266 | { |
263 | 267 | 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); | |
265 | 270 | int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644); |
266 | 271 | if (rc >= 0) { |
267 | 272 | write(rc, text.string(), text.length()); |
@@ -354,8 +359,8 @@ private: | ||
354 | 359 | char buf[128]; |
355 | 360 | while (refs) { |
356 | 361 | 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); | |
359 | 364 | out->append(buf); |
360 | 365 | #if DEBUG_REFS_CALLSTACK_ENABLED |
361 | 366 | out->append(refs->stack.toString("\t\t")); |
@@ -700,7 +700,7 @@ int newfs_msdos_main(int argc, char *argv[]) | ||
700 | 700 | (u_int)tm->tm_min)); |
701 | 701 | mk4(bsx->volid, x); |
702 | 702 | mklabel(bsx->label, opt_L ? opt_L : "NO NAME"); |
703 | - sprintf(buf, "FAT%u", fat); | |
703 | + snprintf(buf, sizeof(buf), "FAT%u", fat); | |
704 | 704 | setstr(bsx->type, buf, sizeof(bsx->type)); |
705 | 705 | if (!opt_B) { |
706 | 706 | x1 += sizeof(struct bsx); |
@@ -57,16 +57,16 @@ static int ps_line(int pid, int tid) | ||
57 | 57 | int prio, nice, rtprio, sched, psr; |
58 | 58 | struct passwd *pw; |
59 | 59 | |
60 | - sprintf(statline, "/proc/%d", tid ? tid : pid); | |
60 | + snprintf(statline, sizeof(statline), "/proc/%d", tid ? tid : pid); | |
61 | 61 | stat(statline, &stats); |
62 | 62 | |
63 | 63 | if(tid) { |
64 | - sprintf(statline, "/proc/%d/task/%d/stat", pid, tid); | |
64 | + snprintf(statline, sizeof(statline), "/proc/%d/task/%d/stat", pid, tid); | |
65 | 65 | cmdline[0] = 0; |
66 | 66 | snprintf(macline, sizeof(macline), "/proc/%d/task/%d/attr/current", pid, tid); |
67 | 67 | } 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); | |
70 | 70 | snprintf(macline, sizeof(macline), "/proc/%d/attr/current", pid); |
71 | 71 | int fd = open(cmdline, O_RDONLY); |
72 | 72 | if(fd == 0) { |
@@ -149,7 +149,7 @@ static int ps_line(int pid, int tid) | ||
149 | 149 | |
150 | 150 | pw = getpwuid(stats.st_uid); |
151 | 151 | 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); | |
153 | 153 | } else { |
154 | 154 | strcpy(user,pw->pw_name); |
155 | 155 | } |
@@ -208,7 +208,7 @@ static void print_exe_abi(int pid) | ||
208 | 208 | int fd, r; |
209 | 209 | char exeline[1024]; |
210 | 210 | |
211 | - sprintf(exeline, "/proc/%d/exe", pid); | |
211 | + snprintf(exeline, sizeof(exeline), "/proc/%d/exe", pid); | |
212 | 212 | fd = open(exeline, O_RDONLY); |
213 | 213 | if(fd == 0) { |
214 | 214 | printf(" "); |
@@ -243,7 +243,7 @@ void ps_threads(int pid) | ||
243 | 243 | DIR *d; |
244 | 244 | struct dirent *de; |
245 | 245 | |
246 | - sprintf(tmp,"/proc/%d/task",pid); | |
246 | + snprintf(tmp,sizeof(tmp),"/proc/%d/task",pid); | |
247 | 247 | d = opendir(tmp); |
248 | 248 | if(d == 0) return; |
249 | 249 |
@@ -265,29 +265,29 @@ static void read_procs(void) { | ||
265 | 265 | |
266 | 266 | proc->pid = proc->tid = pid; |
267 | 267 | |
268 | - sprintf(filename, "/proc/%d/stat", pid); | |
268 | + snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); | |
269 | 269 | read_stat(filename, proc); |
270 | 270 | |
271 | - sprintf(filename, "/proc/%d/cmdline", pid); | |
271 | + snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); | |
272 | 272 | read_cmdline(filename, proc); |
273 | 273 | |
274 | - sprintf(filename, "/proc/%d/status", pid); | |
274 | + snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | |
275 | 275 | read_status(filename, proc); |
276 | 276 | |
277 | 277 | read_policy(pid, proc); |
278 | 278 | |
279 | 279 | proc->num_threads = 0; |
280 | 280 | } else { |
281 | - sprintf(filename, "/proc/%d/cmdline", pid); | |
281 | + snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); | |
282 | 282 | read_cmdline(filename, &cur_proc); |
283 | 283 | |
284 | - sprintf(filename, "/proc/%d/status", pid); | |
284 | + snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | |
285 | 285 | read_status(filename, &cur_proc); |
286 | 286 | |
287 | 287 | proc = NULL; |
288 | 288 | } |
289 | 289 | |
290 | - sprintf(filename, "/proc/%d/task", pid); | |
290 | + snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | |
291 | 291 | task_dir = opendir(filename); |
292 | 292 | if (!task_dir) continue; |
293 | 293 |
@@ -302,7 +302,7 @@ static void read_procs(void) { | ||
302 | 302 | |
303 | 303 | proc->pid = pid; proc->tid = tid; |
304 | 304 | |
305 | - sprintf(filename, "/proc/%d/task/%d/stat", pid, tid); | |
305 | + snprintf(filename, sizeof(filename), "/proc/%d/task/%d/stat", pid, tid); | |
306 | 306 | read_stat(filename, proc); |
307 | 307 | |
308 | 308 | read_policy(tid, proc); |
@@ -491,7 +491,7 @@ static void print_procs(void) { | ||
491 | 491 | if (user && user->pw_name) { |
492 | 492 | user_str = user->pw_name; |
493 | 493 | } else { |
494 | - snprintf(user_buf, 20, "%d", proc->uid); | |
494 | + snprintf(user_buf, sizeof(user_buf), "%d", proc->uid); | |
495 | 495 | user_str = user_buf; |
496 | 496 | } |
497 | 497 | if (!threads) { |