commit: 47364622bed691918f9ad10aaf65de7410847ea4
parent 2ef29f397a6de64a926151fb24ecdfa9a4a3dd44
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 28 Jul 2025 07:10:47 +0200
cmd/strings: improve logic, fix off-by-one insert of NULL terminator
Diffstat:
1 file changed, 13 insertions(+), 18 deletions(-)
diff --git a/cmd/strings.c b/cmd/strings.c
@@ -17,7 +17,7 @@
const char *argv0 = "strings";
size_t opt_min_strlen = 4;
const char *opt_offset_format = NULL;
-int delim = '\n';
+char delim = '\n';
static int
print_string(char *buffer, size_t offset)
@@ -57,32 +57,27 @@ concat(int fd, const char *fdname)
while((c = read(fd, read_buf, sizeof(read_buf))) > 0)
{
int read_pos = 0;
- char b = 0;
for(; read_pos < c; read_pos++)
{
- b = read_buf[read_pos];
+ char b = read_buf[read_pos];
- if(isprint(b) && write_pos < 4096)
+ if(isprint(b))
{
write_buf[write_pos++] = b;
+
+ if(write_pos < (sizeof(write_buf) - 1)) continue;
}
- else
+
+ if(write_pos >= opt_min_strlen)
{
- if(write_pos >= opt_min_strlen)
- {
- write_buf[write_pos + 1] = 0;
-
- if(print_string(write_buf, offset) != 0)
- {
- return 1;
- }
- }
-
- offset += write_pos;
- offset++;
- write_pos = 0;
+ write_buf[write_pos] = '\0';
+ if(print_string(write_buf, offset) != 0) return 1;
}
+
+ offset += write_pos;
+ offset++;
+ write_pos = 0;
}
}