Locale and some checks

This commit is contained in:
Emil 2023-10-07 02:17:40 +00:00
parent 1707803ff7
commit 0d7605a43f
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A

19
baked.c
View File

@ -11,6 +11,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <locale.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -62,15 +63,9 @@ find_region(const char * fn)
&& s.st_size) && s.st_size)
{ {
char * start, * stop, * addr; char * start, * stop, * addr;
/* hypothetically mmap can return -1 (MAP_FAILED) on failure,
this will not be due to permission, noexist, or ifreg.
Checks ensures that all these possibilities are impossible.
However, other issues may occur, such as a unexpected
change to permission after the fstat call had succeeded.
Then, this call may fail, Under such condition the for loop
will safely lead to a bail with a possible bad call of munmap. */
addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0); addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr != MAP_FAILED)
{
for (start = addr; *start; ++start) for (start = addr; *start; ++start)
{ {
if (s.st_size - (start - addr) > SLEN) if (s.st_size - (start - addr) > SLEN)
@ -102,8 +97,8 @@ find_region(const char * fn)
else goto stop; else goto stop;
} }
stop: stop:
if (addr != MAP_FAILED) munmap(addr, s.st_size);
{ munmap(addr, s.st_size); } }
} }
return buf; return buf;
} }
@ -270,6 +265,8 @@ static size_t
strip(char * buf) strip(char * buf)
{ {
size_t i = strlen(buf); size_t i = strlen(buf);
if (!i)
{ return 0; }
while (isspace(buf[i - 1])) while (isspace(buf[i - 1]))
{ --i; } { --i; }
buf[i] = '\0'; buf[i] = '\0';
@ -291,6 +288,8 @@ main(int argc, char ** argv)
int ret = 0; int ret = 0;
char * buf; char * buf;
assert(setlocale(LC_ALL, "C"));
if (argc < 2 if (argc < 2
|| !strcmp(argv[1], "-h")) || !strcmp(argv[1], "-h"))
{ goto help; } { goto help; }