Skip to content

Commit acfa4f5

Browse files
committed
Improve type safety in runtime
- Add explicit casts in libc for type clarity - Enhance ELF generation with proper type handling - Ensure consistent type usage throughout runtime - Code formatted with clang-format-18
1 parent 5965012 commit acfa4f5

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

lib/c.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,17 @@ char *memcpy(char *dest, char *src, int count)
146146
void *memset(void *s, int c, int n)
147147
{
148148
int i = 0;
149-
char *ptr = s;
149+
char *ptr = (char *) s;
150+
char byte_val = (char) c;
150151
for (; i + 4 <= n; i += 4) {
151-
ptr[i] = c;
152-
ptr[i + 1] = c;
153-
ptr[i + 2] = c;
154-
ptr[i + 3] = c;
152+
ptr[i] = byte_val;
153+
ptr[i + 1] = byte_val;
154+
ptr[i + 2] = byte_val;
155+
ptr[i + 3] = byte_val;
155156
}
156157

157158
for (; i < n; i++)
158-
ptr[i] = c;
159+
ptr[i] = byte_val;
159160

160161
return s;
161162
}
@@ -282,7 +283,7 @@ void __fmtbuf_write_char(fmtbuf_t *fmtbuf, int val)
282283
if (fmtbuf->n <= 1)
283284
return;
284285

285-
char ch = val & 0xFF;
286+
char ch = (char) (val & 0xFF);
286287
fmtbuf->buf[0] = ch;
287288
fmtbuf->buf += 1;
288289
fmtbuf->n -= 1;
@@ -418,12 +419,12 @@ void __format_to_buf(fmtbuf_t *fmtbuf, char *format, int *var_args)
418419
switch (format[si]) {
419420
case 's':
420421
/* append param pi as string */
421-
l = strlen(v);
422-
__fmtbuf_write_str(fmtbuf, v, l);
422+
l = strlen((char *) v);
423+
__fmtbuf_write_str(fmtbuf, (char *) v, l);
423424
break;
424425
case 'c':
425426
/* append param pi as char */
426-
__fmtbuf_write_char(fmtbuf, v);
427+
__fmtbuf_write_char(fmtbuf, (char) v);
427428
break;
428429
case 'o':
429430
/* append param as octal */
@@ -551,8 +552,8 @@ char *fgets(char *str, int n, FILE *stream)
551552
str[i] = 0;
552553
return str;
553554
}
554-
/* Not support casting yet. Simply assign it. */
555-
str[i] = c;
555+
/* Use explicit cast for clarity */
556+
str[i] = (char) c;
556557

557558
if (c == '\n') {
558559
str[i + 1] = 0;
@@ -683,7 +684,7 @@ void *malloc(int size)
683684
__alloc_tail->size = allocated->size;
684685
chunk_clear_freed(__alloc_tail);
685686

686-
void *ptr = __alloc_tail + 1;
687+
void *ptr = (void *) (__alloc_tail + 1);
687688
return ptr;
688689
}
689690

@@ -750,8 +751,8 @@ void free(void *ptr)
750751
if (!ptr)
751752
return;
752753

753-
char *__ptr = ptr;
754-
chunk_t *cur = __ptr - sizeof(chunk_t);
754+
char *__ptr = (char *) ptr;
755+
chunk_t *cur = (chunk_t *) (__ptr - sizeof(chunk_t));
755756
if (IS_CHUNK_GET_FREED(cur->size)) {
756757
printf("free(): double free detected\n");
757758
abort();

src/elf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void elf_write_byte(strbuf_t *elf_array, int val)
3131

3232
char e_extract_byte(int v, int b)
3333
{
34-
return (v >> (b << 3)) & 0xFF;
34+
return (char) ((v >> (b << 3)) & 0xFF);
3535
}
3636

3737
void elf_write_int(strbuf_t *elf_array, int val)
@@ -97,7 +97,7 @@ void elf_generate_header(void)
9797
* 34 | | |
9898
*/
9999
/* ELF file header */
100-
hdr.e_ident[0] = 0x7F; /* ELF magic number */
100+
hdr.e_ident[0] = (char) 0x7F; /* ELF magic number */
101101
hdr.e_ident[1] = 'E';
102102
hdr.e_ident[2] = 'L';
103103
hdr.e_ident[3] = 'F';
@@ -124,13 +124,13 @@ void elf_generate_header(void)
124124
elf_symtab->size +
125125
elf_strtab->size; /* section header offset */
126126
hdr.e_flags = ELF_FLAGS; /* flags */
127-
hdr.e_ehsize[0] = 0x34; /* header size */
127+
hdr.e_ehsize[0] = (char) 0x34; /* header size */
128128
hdr.e_ehsize[1] = 0;
129-
hdr.e_phentsize[0] = 0x20; /* program header size */
129+
hdr.e_phentsize[0] = (char) 0x20; /* program header size */
130130
hdr.e_phentsize[1] = 0;
131131
hdr.e_phnum[0] = 1; /* number of program headers */
132132
hdr.e_phnum[1] = 0;
133-
hdr.e_shentsize[0] = 0x28; /* section header size */
133+
hdr.e_shentsize[0] = (char) 0x28; /* section header size */
134134
hdr.e_shentsize[1] = 0;
135135
hdr.e_shnum[0] = 6; /* number of section headers */
136136
hdr.e_shnum[1] = 0;

0 commit comments

Comments
 (0)