Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snprintf implementation #170

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

icgmilk
Copy link

@icgmilk icgmilk commented Dec 3, 2024

  • Add snprintf in c.c
  • Add tests for snprintf

}
}
if (bi < n) buffer[bi] = '\0';
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using git for version control, it is recommended to add a newline at the end of files. This is because git compares files line by line, and the end of each line is identified by a newline character.

For more details, refer to the following link:
https://medium.com/@alexey.inkin/how-to-force-newline-at-end-of-files-and-why-you-should-do-it-fdf76d1d090e

@visitorckw
Copy link
Contributor

I noticed the PR includes a merge commit, which doesn't seem necessary. Could you remove it?

@visitorckw
Copy link
Contributor

Currently, shecc doesn't have any direct users of snprintf(). I'm unsure if adding it without a user is acceptable, so I'll leave that for the maintainer to decide. If this change is welcomed, should we aim to support as many functions from the C library standard as possible?

Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the file CONTRIBUTING.md carefully.

@jserv jserv changed the title Add snprintf in c.c Add snprintf implementation Dec 3, 2024
lib/c.c Outdated
@@ -724,3 +724,68 @@ void free(void *ptr)
__freelist_head->prev = cur;
__freelist_head = cur;
}

void snprintf(char *buffer, size_t n, char *str, ...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently shecc doesn't support unsigned types, size_t is unsigned type. Instead, use int as a workaround at this moment.

Comment on lines +1089 to +1105
try_output "Hello World 1123" << EOF
int main() {
char buffer[50];
snprintf(buffer, sizeof(buffer), "Hello %s %d", "World", 1123);
printf("%s", buffer);
return 0;
}
EOF

try_output "Number: -37" << EOF
int main() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "Number: %d", -37);
printf("%s", buffer);
return 0;
}
EOF
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't to see any edge case here, please add an edge case that covers when given formatted string's length exceeds n.

lib/c.c Outdated
int si = 0, bi = 0, pi = 0;

while (str[si]) {
if (bi >= n-1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the coding convention.

int *var_args = &str + 4;
int si = 0, bi = 0, pi = 0;

while (str[si]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reuse the code in sprintf() ?

return 0;
}
EOF

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide more tests such as string truncation?

@ChAoSUnItY
Copy link
Collaborator

Additionally, I failed to see why this build keeps failed on stage1. I've used gdb to debug the cause, but I've found that by applying the following diff in lib/c.c to fix some syntactic errors:

diff --git a/lib/c.c b/lib/c.c
index c76bde9..412c485 100644
--- a/lib/c.c
+++ b/lib/c.c
@@ -725,7 +725,7 @@ void free(void *ptr)
     __freelist_head = cur;
 }
 
-void snprintf(char *buffer, size_t n, char *str, ...)
+void snprintf(char *buffer, int n, char *str, ...)
 {
     int *var_args = &str + 4;
     int si = 0, bi = 0, pi = 0;
@@ -787,5 +787,5 @@ void snprintf(char *buffer, size_t n, char *str, ...)
             si++;
         }
     }
-    if (bi < n) buffer[bi] = '\0';
+    if (bi < n) buffer[bi] = 0;
 }
\ No newline at end of file

The program would failed at src/ssa.c:
image

@ChAoSUnItY
Copy link
Collaborator

@vacantron do you have any suspicion for the above ssa error?

@vacantron
Copy link
Collaborator

@vacantron do you have any suspicion for the above ssa error?

I haven't checked it yet. But I think there is an array index out of bound somewhere.

Jim Hsu and others added 2 commits December 5, 2024 16:22
Replaced 'size_t' type with 'int' in the function due to shecc limitations that do not support the 'unsigned' type.
Refactor function 'snprintf(size_t n)' to replace 'size_t' with 'int'
Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use git rebase -i to rework the commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants