Browse Source

Locale and some checks

tags/v20240302
Emil 7 months ago
parent
commit
0d7605a43f
No known key found for this signature in database GPG Key ID: 5432DB986FDBCF8A
1 changed files with 28 additions and 29 deletions
  1. +28
    -29
      baked.c

+ 28
- 29
baked.c View File

@@ -11,6 +11,7 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -62,48 +63,42 @@ find_region(const char * fn)
&& s.st_size)
{
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);

for (start = addr; *start; ++start)
if (addr != MAP_FAILED)
{
if (s.st_size - (start - addr) > SLEN)
for (start = addr; *start; ++start)
{
if (!strncmp(start,START,SLEN))
if (s.st_size - (start - addr) > SLEN)
{
start += strlen(START);
for (stop = start; *stop; ++stop)
if (!strncmp(start,START,SLEN))
{
if (s.st_size - (stop - addr) > SLEN)
start += strlen(START);
for (stop = start; *stop; ++stop)
{
if (!strncmp(stop,STOP,SLEN))
if (s.st_size - (stop - addr) > SLEN)
{
size_t len = (stop - addr) - (start - addr);
buf = malloc(len + 1);
assert(buf);
if (!buf)
{ goto stop; }
strncpy(buf, start, len);
buf[len] = '\0';
goto stop;
if (!strncmp(stop,STOP,SLEN))
{
size_t len = (stop - addr) - (start - addr);
buf = malloc(len + 1);
assert(buf);
if (!buf)
{ goto stop; }
strncpy(buf, start, len);
buf[len] = '\0';
goto stop;
}
}
else goto stop;
}
else goto stop;
goto stop;
}
goto stop;
}
else goto stop;
}
else goto stop;
}
stop:
if (addr != MAP_FAILED)
{ munmap(addr, s.st_size); }
munmap(addr, s.st_size);
}
}
return buf;
}
@@ -270,6 +265,8 @@ static size_t
strip(char * buf)
{
size_t i = strlen(buf);
if (!i)
{ return 0; }
while (isspace(buf[i - 1]))
{ --i; }
buf[i] = '\0';
@@ -291,6 +288,8 @@ main(int argc, char ** argv)
int ret = 0;
char * buf;

assert(setlocale(LC_ALL, "C"));

if (argc < 2
|| !strcmp(argv[1], "-h"))
{ goto help; }


Loading…
Cancel
Save