X Tutup
Skip to content

Commit fe96c0f

Browse files
authored
treewide: tighten variable scope in loops (systemd#18372)
Also use _cleanup_free_ in one more place.
1 parent 37baf8d commit fe96c0f

30 files changed

+74
-127
lines changed

src/basic/cgroup-util.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,8 @@ static const char *skip_slices(const char *p) {
11311131
}
11321132

11331133
int cg_path_get_unit(const char *path, char **ret) {
1134+
_cleanup_free_ char *unit = NULL;
11341135
const char *e;
1135-
char *unit;
11361136
int r;
11371137

11381138
assert(path);
@@ -1145,12 +1145,10 @@ int cg_path_get_unit(const char *path, char **ret) {
11451145
return r;
11461146

11471147
/* We skipped over the slices, don't accept any now */
1148-
if (endswith(unit, ".slice")) {
1149-
free(unit);
1148+
if (endswith(unit, ".slice"))
11501149
return -ENXIO;
1151-
}
11521150

1153-
*ret = unit;
1151+
*ret = TAKE_PTR(unit);
11541152
return 0;
11551153
}
11561154

src/basic/fd-util.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ void safe_close_pair(int p[static 2]) {
9191
}
9292

9393
void close_many(const int fds[], size_t n_fd) {
94-
size_t i;
95-
9694
assert(fds || n_fd <= 0);
9795

98-
for (i = 0; i < n_fd; i++)
96+
for (size_t i = 0; i < n_fd; i++)
9997
safe_close(fds[i]);
10098
}
10199

@@ -179,11 +177,9 @@ int fd_cloexec(int fd, bool cloexec) {
179177
}
180178

181179
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
182-
size_t i;
183-
184180
assert(n_fdset == 0 || fdset);
185181

186-
for (i = 0; i < n_fdset; i++)
182+
for (size_t i = 0; i < n_fdset; i++)
187183
if (fdset[i] == fd)
188184
return true;
189185

src/basic/format-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
4343
{ "K", UINT64_C(1000) },
4444
};
4545
const suffix_table *table;
46-
size_t n, i;
46+
size_t n;
4747

4848
assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_si));
4949

@@ -53,7 +53,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
5353
table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_si;
5454
n = ELEMENTSOF(table_iec);
5555

56-
for (i = 0; i < n; i++)
56+
for (size_t i = 0; i < n; i++)
5757
if (t >= table[i].factor) {
5858
if (flag & FORMAT_BYTES_BELOW_POINT) {
5959
snprintf(buf, l,

src/basic/io-util.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,14 @@ int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, ch
319319
}
320320

321321
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
322-
size_t i;
323-
324-
for (i = 0; i < iovw->count; i++)
322+
for (size_t i = 0; i < iovw->count; i++)
325323
iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
326324
}
327325

328326
size_t iovw_size(struct iovec_wrapper *iovw) {
329-
size_t n = 0, i;
327+
size_t n = 0;
330328

331-
for (i = 0; i < iovw->count; i++)
329+
for (size_t i = 0; i < iovw->count; i++)
332330
n += iovw->iovec[i].iov_len;
333331

334332
return n;

src/basic/parse-util.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,13 @@ int safe_atod(const char *s, double *ret_d) {
593593
}
594594

595595
int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
596-
size_t i;
597596
unsigned val = 0;
598597
const char *s;
599598

600599
s = *p;
601600

602601
/* accept any number of digits, strtoull is limited to 19 */
603-
for (i=0; i < digits; i++,s++) {
602+
for (size_t i = 0; i < digits; i++,s++) {
604603
if (*s < '0' || *s > '9') {
605604
if (i == 0)
606605
return -EINVAL;

src/basic/rlimit-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ int setrlimit_closest(int resource, const struct rlimit *rlim) {
5050
}
5151

5252
int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {
53-
int i, r;
53+
int r;
5454

5555
assert(rlim);
5656

5757
/* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
5858

59-
for (i = 0; i < _RLIMIT_MAX; i++) {
59+
for (int i = 0; i < _RLIMIT_MAX; i++) {
6060
if (!rlim[i])
6161
continue;
6262

src/basic/sigbus.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
2323
static volatile sig_atomic_t n_sigbus_queue = 0;
2424

2525
static void sigbus_push(void *addr) {
26-
unsigned u;
27-
2826
assert(addr);
2927

3028
/* Find a free place, increase the number of entries and leave, if we can */
31-
for (u = 0; u < SIGBUS_QUEUE_MAX; u++)
29+
for (size_t u = 0; u < SIGBUS_QUEUE_MAX; u++)
3230
if (__sync_bool_compare_and_swap(&sigbus_queue[u], NULL, addr)) {
3331
__sync_fetch_and_add(&n_sigbus_queue, 1);
3432
return;

src/basic/signal-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ int reset_all_signal_handlers(void) {
1515
.sa_handler = SIG_DFL,
1616
.sa_flags = SA_RESTART,
1717
};
18-
int sig, r = 0;
18+
int r = 0;
1919

20-
for (sig = 1; sig < _NSIG; sig++) {
20+
for (int sig = 1; sig < _NSIG; sig++) {
2121

2222
/* These two cannot be caught... */
2323
if (IN_SET(sig, SIGKILL, SIGSTOP))

src/basic/string-util.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,8 @@ char *strextend_with_separator_internal(char **x, const char *separator, ...) {
862862
}
863863

864864
char *strrep(const char *s, unsigned n) {
865-
size_t l;
866865
char *r, *p;
867-
unsigned i;
866+
size_t l;
868867

869868
assert(s);
870869

@@ -873,7 +872,7 @@ char *strrep(const char *s, unsigned n) {
873872
if (!r)
874873
return NULL;
875874

876-
for (i = 0; i < n; i++)
875+
for (unsigned i = 0; i < n; i++)
877876
p = stpcpy(p, s);
878877

879878
*p = 0;

src/basic/time-util.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
493493
{ "us", 1 },
494494
};
495495

496-
size_t i;
497496
char *p = buf;
498497
bool something = false;
499498

@@ -514,7 +513,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
514513

515514
/* The result of this function can be parsed with parse_sec */
516515

517-
for (i = 0; i < ELEMENTSOF(table); i++) {
516+
for (size_t i = 0; i < ELEMENTSOF(table); i++) {
518517
int k = 0;
519518
size_t n;
520519
bool done = false;
@@ -962,9 +961,8 @@ static const char* extract_multiplier(const char *p, usec_t *multiplier) {
962961
{ "us", 1ULL },
963962
{ "µs", 1ULL },
964963
};
965-
size_t i;
966964

967-
for (i = 0; i < ELEMENTSOF(table); i++) {
965+
for (size_t i = 0; i < ELEMENTSOF(table); i++) {
968966
char *e;
969967

970968
e = startswith(p, table[i].suffix);

0 commit comments

Comments
 (0)
X Tutup