1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-10 00:58:20 +03:00

boot: Tighten scope of variables used in loops

This commit is contained in:
Daan De Meyer 2021-02-01 22:31:47 +00:00
parent 2a7c16753b
commit 258f0970f9
9 changed files with 70 additions and 101 deletions

View File

@ -118,17 +118,17 @@ static BOOLEAN line_edit(
while (!exit) {
EFI_STATUS err;
UINT64 key;
UINTN i;
UINTN j;
i = len - first;
if (i >= x_max-1)
i = x_max-1;
CopyMem(print, line + first, i * sizeof(CHAR16));
while (clear > 0 && i < x_max-1) {
j = len - first;
if (j >= x_max-1)
j = x_max-1;
CopyMem(print, line + first, j * sizeof(CHAR16));
while (clear > 0 && j < x_max-1) {
clear--;
print[i++] = ' ';
print[j++] = ' ';
}
print[i] = '\0';
print[j] = '\0';
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
@ -212,12 +212,14 @@ static BOOLEAN line_edit(
case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'):
/* kill-word */
clear = 0;
for (i = first + cursor; i < len && line[i] == ' '; i++)
UINTN k;
for (k = first + cursor; k < len && line[k] == ' '; k++)
clear++;
for (; i < len && line[i] != ' '; i++)
for (; k < len && line[k] != ' '; k++)
clear++;
for (i = first + cursor; i + clear < len; i++)
for (UINTN i = first + cursor; i + clear < len; i++)
line[i] = line[i + clear];
len -= clear;
line[len] = '\0';
@ -242,7 +244,7 @@ static BOOLEAN line_edit(
}
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
for (i = first + cursor; i + clear < len; i++)
for (UINTN i = first + cursor; i + clear < len; i++)
line[i] = line[i + clear];
len -= clear;
line[len] = '\0';
@ -255,7 +257,7 @@ static BOOLEAN line_edit(
continue;
if (first + cursor == len)
continue;
for (i = first + cursor; i < len; i++)
for (UINTN i = first + cursor; i < len; i++)
line[i] = line[i+1];
clear = 1;
len--;
@ -284,7 +286,7 @@ static BOOLEAN line_edit(
continue;
if (first == 0 && cursor == 0)
continue;
for (i = first + cursor-1; i < len; i++)
for (UINTN i = first + cursor-1; i < len; i++)
line[i] = line[i+1];
clear = 1;
len--;
@ -312,7 +314,7 @@ static BOOLEAN line_edit(
case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
if (len+1 == size)
continue;
for (i = len; i > first + cursor; i--)
for (UINTN i = len; i > first + cursor; i--)
line[i] = line[i-1];
line[first + cursor] = KEYCHAR(key);
len++;
@ -330,25 +332,23 @@ static BOOLEAN line_edit(
}
static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
UINTN i;
if (key == 0)
return -1;
/* select entry by number key */
if (key >= '1' && key <= '9') {
i = key - '0';
UINTN i = key - '0';
if (i > config->entry_count)
i = config->entry_count;
return i-1;
}
/* find matching key in config entries */
for (i = start; i < config->entry_count; i++)
for (UINTN i = start; i < config->entry_count; i++)
if (config->entries[i]->key == key)
return i;
for (i = 0; i < start; i++)
for (UINTN i = 0; i < start; i++)
if (config->entries[i]->key == key)
return i;
@ -357,7 +357,7 @@ static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
UINT64 key;
UINTN i;
UINTN timeout;
_cleanup_freepool_ CHAR8 *modevar = NULL, *indvar = NULL;
_cleanup_freepool_ CHAR16 *partstr = NULL, *defaultstr = NULL;
UINTN x, y, size;
@ -421,8 +421,8 @@ static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
Print(L"\n");
if (efivar_get_int(LOADER_GUID, L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
Print(L"LoaderConfigTimeout: %u\n", i);
if (efivar_get_int(LOADER_GUID, L"LoaderConfigTimeout", &timeout) == EFI_SUCCESS)
Print(L"LoaderConfigTimeout: %u\n", timeout);
if (config->entry_oneshot)
Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
@ -434,7 +434,7 @@ static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
Print(L"\n--- press key ---\n\n");
console_key_read(&key, TRUE);
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
ConfigEntry *entry;
if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q'))
@ -501,7 +501,6 @@ static BOOLEAN menu_run(
UINTN idx_last;
BOOLEAN refresh;
BOOLEAN highlight;
UINTN i;
UINTN line_width;
CHAR16 **lines;
UINTN x_start;
@ -562,7 +561,7 @@ static BOOLEAN menu_run(
/* length of the longest entry */
line_width = 5;
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
UINTN entry_len;
entry_len = StrLen(config->entries[i]->title_show);
@ -581,14 +580,14 @@ static BOOLEAN menu_run(
/* menu entries title lines */
lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
for (i = 0; i < config->entry_count; i++) {
UINTN j, k;
for (UINTN i = 0; i < config->entry_count; i++) {
UINTN j;
lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
for (j = 0; j < x_start; j++)
lines[i][j] = ' ';
for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
for (UINTN k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
lines[i][j] = config->entries[i]->title_show[k];
for (; j < x_max; j++)
@ -598,15 +597,15 @@ static BOOLEAN menu_run(
status = NULL;
clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
for (i = 0; i < x_max; i++)
for (UINTN i = 0; i < x_max; i++)
clearline[i] = ' ';
clearline[i] = 0;
clearline[x_max] = 0;
while (!exit) {
UINT64 key;
if (refresh) {
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
if (i < idx_first || i > idx_last)
continue;
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
@ -866,7 +865,7 @@ static BOOLEAN menu_run(
*chosen_entry = config->entries[idx_highlight];
for (i = 0; i < config->entry_count; i++)
for (UINTN i = 0; i < config->entry_count; i++)
FreePool(lines[i]);
FreePool(lines);
FreePool(clearline);
@ -1556,14 +1555,11 @@ static INTN config_entry_compare(ConfigEntry *a, ConfigEntry *b) {
}
static VOID config_sort_entries(Config *config) {
UINTN i;
for (i = 1; i < config->entry_count; i++) {
for (UINTN i = 1; i < config->entry_count; i++) {
BOOLEAN more;
UINTN k;
more = FALSE;
for (k = 0; k < config->entry_count - i; k++) {
for (UINTN k = 0; k < config->entry_count - i; k++) {
ConfigEntry *entry;
if (config_entry_compare(config->entries[k], config->entries[k+1]) <= 0)
@ -1580,9 +1576,7 @@ static VOID config_sort_entries(Config *config) {
}
static INTN config_entry_find(Config *config, CHAR16 *id) {
UINTN i;
for (i = 0; i < config->entry_count; i++)
for (UINTN i = 0; i < config->entry_count; i++)
if (StrCmp(config->entries[i]->id, id) == 0)
return (INTN) i;
@ -1663,13 +1657,12 @@ static VOID config_default_entry_select(Config *config) {
static BOOLEAN find_nonunique(ConfigEntry **entries, UINTN entry_count) {
BOOLEAN non_unique = FALSE;
UINTN i, k;
for (i = 0; i < entry_count; i++)
for (UINTN i = 0; i < entry_count; i++)
entries[i]->non_unique = FALSE;
for (i = 0; i < entry_count; i++)
for (k = 0; k < entry_count; k++) {
for (UINTN i = 0; i < entry_count; i++)
for (UINTN k = 0; k < entry_count; k++) {
if (i == k)
continue;
if (StrCmp(entries[i]->title_show, entries[k]->title_show) != 0)
@ -1683,10 +1676,8 @@ static BOOLEAN find_nonunique(ConfigEntry **entries, UINTN entry_count) {
/* generate a unique title, avoiding non-distinguishable menu entries */
static VOID config_title_generate(Config *config) {
UINTN i;
/* set title */
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
CHAR16 *title;
FreePool(config->entries[i]->title_show);
@ -1700,7 +1691,7 @@ static VOID config_title_generate(Config *config) {
return;
/* add version to non-unique titles */
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
CHAR16 *s;
if (!config->entries[i]->non_unique)
@ -1717,7 +1708,7 @@ static VOID config_title_generate(Config *config) {
return;
/* add machine-id to non-unique titles */
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
CHAR16 *s;
_cleanup_freepool_ CHAR16 *m = NULL;
@ -1737,7 +1728,7 @@ static VOID config_title_generate(Config *config) {
return;
/* add file name to non-unique titles */
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
CHAR16 *s;
if (!config->entries[i]->non_unique)
@ -1830,10 +1821,7 @@ static BOOLEAN config_entry_add_loader_auto(
/* look for systemd-boot magic string */
err = file_read(root_dir, loader, 0, 100*1024, &content, &len);
if (!EFI_ERROR(err)) {
CHAR8 *start = content;
CHAR8 *last = content + len - sizeof(magic) - 1;
for (; start <= last; start++)
for (CHAR8 *start = content; start <= content + len - sizeof(magic) - 1; start++)
if (start[0] == magic[0] && CompareMem(start, magic, sizeof(magic) - 1) == 0)
return FALSE;
}
@ -1865,9 +1853,7 @@ static VOID config_entry_add_osx(Config *config) {
err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
if (!EFI_ERROR(err)) {
UINTN i;
for (i = 0; i < handle_count; i++) {
for (UINTN i = 0; i < handle_count; i++) {
EFI_FILE *root;
BOOLEAN found;
@ -2042,7 +2028,7 @@ static VOID config_load_xbootldr(
Config *config,
EFI_HANDLE *device) {
EFI_DEVICE_PATH *partition_path, *node, *disk_path, *copy;
EFI_DEVICE_PATH *partition_path, *disk_path, *copy;
UINT32 found_partition_number = (UINT32) -1;
UINT64 found_partition_start = (UINT64) -1;
UINT64 found_partition_size = (UINT64) -1;
@ -2055,11 +2041,10 @@ static VOID config_load_xbootldr(
if (!partition_path)
return;
for (node = partition_path; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) {
for (EFI_DEVICE_PATH *node = partition_path; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) {
EFI_HANDLE disk_handle;
EFI_BLOCK_IO *block_io;
EFI_DEVICE_PATH *p;
UINTN nr;
/* First, Let's look for the SCSI/SATA/USB/… device path node, i.e. one above the media
* devices */
@ -2086,7 +2071,7 @@ static VOID config_load_xbootldr(
continue;
/* Try both copies of the GPT header, in case one is corrupted */
for (nr = 0; nr < 2; nr++) {
for (UINTN nr = 0; nr < 2; nr++) {
_cleanup_freepool_ EFI_PARTITION_ENTRY* entries = NULL;
union {
EFI_PARTITION_TABLE_HEADER gpt_header;
@ -2094,7 +2079,7 @@ static VOID config_load_xbootldr(
} gpt_header_buffer;
const EFI_PARTITION_TABLE_HEADER *h = &gpt_header_buffer.gpt_header;
UINT64 where;
UINTN i, sz;
UINTN sz;
UINT32 c;
if (nr == 0)
@ -2164,7 +2149,7 @@ static VOID config_load_xbootldr(
if (c != h->PartitionEntryArrayCRC32)
continue;
for (i = 0; i < h->NumberOfPartitionEntries; i++) {
for (UINTN i = 0; i < h->NumberOfPartitionEntries; i++) {
EFI_PARTITION_ENTRY *entry;
entry = (EFI_PARTITION_ENTRY*) ((UINT8*) entries + h->SizeOfPartitionEntry * i);
@ -2198,7 +2183,7 @@ found:
copy = DuplicateDevicePath(partition_path);
/* Patch in the data we found */
for (node = copy; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) {
for (EFI_DEVICE_PATH *node = copy; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) {
HARDDRIVE_DEVICE_PATH *hd;
if (DevicePathType(node) != MEDIA_DEVICE_PATH)
@ -2313,9 +2298,7 @@ static EFI_STATUS reboot_into_firmware(VOID) {
}
static VOID config_free(Config *config) {
UINTN i;
for (i = 0; i < config->entry_count; i++)
for (UINTN i = 0; i < config->entry_count; i++)
config_entry_free(config->entries[i]);
FreePool(config->entries);
FreePool(config->entry_default_pattern);
@ -2325,15 +2308,15 @@ static VOID config_free(Config *config) {
static VOID config_write_entries_to_variable(Config *config) {
_cleanup_freepool_ CHAR16 *buffer = NULL;
UINTN i, sz = 0;
UINTN sz = 0;
CHAR16 *p;
for (i = 0; i < config->entry_count; i++)
for (UINTN i = 0; i < config->entry_count; i++)
sz += StrLen(config->entries[i]->id) + 1;
p = buffer = AllocatePool(sz * sizeof(CHAR16));
for (i = 0; i < config->entry_count; i++) {
for (UINTN i = 0; i < config->entry_count; i++) {
UINTN l;
l = StrLen(config->entries[i]->id) + 1;

View File

@ -128,9 +128,8 @@ UINT32 crc32_exclude_offset(
const UINT8 *p = buf;
UINT32 crc = seed;
UINTN i;
for (i = 0; i < len; i++) {
for (UINTN i = 0; i < len; i++) {
UINT8 x = *p++;
if (i >= exclude_off && i < exclude_off + exclude_len)

View File

@ -13,10 +13,9 @@ EFI_STATUS disk_get_part_uuid(EFI_HANDLE *handle, CHAR16 uuid[static 37]) {
device_path = DevicePathFromHandle(handle);
if (device_path) {
_cleanup_freepool_ EFI_DEVICE_PATH *paths = NULL;
EFI_DEVICE_PATH *path;
paths = UnpackDevicePath(device_path);
for (path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) {
for (EFI_DEVICE_PATH *path = paths; !IsDevicePathEnd(path); path = NextDevicePathNode(path)) {
HARDDRIVE_DEVICE_PATH *drive;
if (DevicePathType(path) != MEDIA_DEVICE_PATH)

View File

@ -62,7 +62,6 @@ struct PeSectionHeader {
EFI_STATUS pe_memory_locate_sections(CHAR8 *base, CHAR8 **sections, UINTN *addrs, UINTN *offsets, UINTN *sizes) {
struct DosFileHeader *dos;
struct PeHeader *pe;
UINTN i;
UINTN offset;
dos = (struct DosFileHeader *)base;
@ -85,12 +84,11 @@ EFI_STATUS pe_memory_locate_sections(CHAR8 *base, CHAR8 **sections, UINTN *addrs
offset = dos->ExeHeader + sizeof(*pe) + pe->FileHeader.SizeOfOptionalHeader;
for (i = 0; i < pe->FileHeader.NumberOfSections; i++) {
for (UINTN i = 0; i < pe->FileHeader.NumberOfSections; i++) {
struct PeSectionHeader *sect;
UINTN j;
sect = (struct PeSectionHeader *)&base[offset];
for (j = 0; sections[j]; j++) {
for (UINTN j = 0; sections[j]; j++) {
if (CompareMem(sect->Name, sections[j], strlena(sections[j])) != 0)
continue;

View File

@ -86,7 +86,6 @@ static EFI_STATUS hash_many(
VOID **ret) {
_cleanup_freepool_ VOID *output = NULL;
UINTN i;
/* Hashes the specified parameters in counter mode, generating n hash values, with the counter in the
* range counter_startcounter_start+n-1. */
@ -95,7 +94,7 @@ static EFI_STATUS hash_many(
if (!output)
return log_oom();
for (i = 0; i < n; i++)
for (UINTN i = 0; i < n; i++)
hash_once(old_seed, rng, size,
system_token, system_token_size,
counter_start + i,
@ -201,9 +200,7 @@ static VOID validate_sha256(void) {
0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1 }},
};
UINTN i;
for (i = 0; i < ELEMENTSOF(array); i++) {
for (UINTN i = 0; i < ELEMENTSOF(array); i++) {
struct sha256_ctx hash;
uint8_t result[HASH_VALUE_SIZE];

View File

@ -94,7 +94,7 @@ void sha256_init_ctx(struct sha256_ctx *ctx) {
void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
/* Take yet unprocessed bytes into account. */
UINT32 bytes = ctx->buflen;
UINTN pad, i;
UINTN pad;
/* Now count remaining bytes. */
ctx->total64 += bytes;
@ -111,7 +111,7 @@ void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
sha256_process_block (ctx->buffer, bytes + pad + 8, ctx);
/* Put result from CTX in first 32 bytes following RESBUF. */
for (i = 0; i < 8; ++i)
for (UINTN i = 0; i < 8; ++i)
((UINT32 *) resbuf)[i] = SWAP (ctx->H[i]);
return resbuf;
@ -214,7 +214,6 @@ static void sha256_process_block(const void *buffer, UINTN len, struct sha256_ct
UINT32 f_save = f;
UINT32 g_save = g;
UINT32 h_save = h;
UINTN t;
/* Operators defined in FIPS 180-2:4.1.2. */
#define Ch(x, y, z) ((x & y) ^ (~x & z))
@ -229,15 +228,15 @@ static void sha256_process_block(const void *buffer, UINTN len, struct sha256_ct
#define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
/* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
for (t = 0; t < 16; ++t) {
for (UINTN t = 0; t < 16; ++t) {
W[t] = SWAP (*words);
++words;
}
for (t = 16; t < 64; ++t)
for (UINTN t = 16; t < 64; ++t)
W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
/* The actual computation according to FIPS 180-2:6.2.2 step 3. */
for (t = 0; t < 64; ++t) {
for (UINTN t = 0; t < 64; ++t) {
UINT32 T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
UINT32 T2 = S0 (a) + Maj (a, b, c);
h = g;

View File

@ -151,22 +151,18 @@ static EFI_STATUS bmp_to_blt(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf,
struct bmp_dib *dib, struct bmp_map *map,
UINT8 *pixmap) {
UINT8 *in;
UINTN y;
/* transform and copy pixels */
in = pixmap;
for (y = 0; y < dib->y; y++) {
for (UINTN y = 0; y < dib->y; y++) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out;
UINTN row_size;
UINTN x;
out = &buf[(dib->y - y - 1) * dib->x];
for (x = 0; x < dib->x; x++, in++, out++) {
for (UINTN x = 0; x < dib->x; x++, in++, out++) {
switch (dib->depth) {
case 1: {
UINTN i;
for (i = 0; i < 8 && x < dib->x; i++) {
for (UINTN i = 0; i < 8 && x < dib->x; i++) {
out->Red = map[((*in) >> (7 - i)) & 1].red;
out->Green = map[((*in) >> (7 - i)) & 1].green;
out->Blue = map[((*in) >> (7 - i)) & 1].blue;

View File

@ -59,12 +59,11 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
*(CHAR16 *) loaded_image->LoadOptions > 0x1F) {
CHAR16 *options;
CHAR8 *line;
UINTN i;
options = (CHAR16 *)loaded_image->LoadOptions;
cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
line = AllocatePool(cmdline_len);
for (i = 0; i < cmdline_len; i++)
for (UINTN i = 0; i < cmdline_len; i++)
line[i] = options[i];
cmdline = line;

View File

@ -193,7 +193,6 @@ VOID efivar_set_time_usec(const EFI_GUID *vendor, CHAR16 *name, UINT64 usec) {
static INTN utf8_to_16(CHAR8 *stra, CHAR16 *c) {
CHAR16 unichar;
UINTN len;
UINTN i;
if (!(stra[0] & 0x80))
len = 1;
@ -231,7 +230,7 @@ static INTN utf8_to_16(CHAR8 *stra, CHAR16 *c) {
break;
}
for (i = 1; i < len; i++) {
for (UINTN i = 1; i < len; i++) {
if ((stra[i] & 0xc0) != 0x80)
return -1;
unichar <<= 6;