Add splint checking to Makefile. Rewrite programs to pass lint.

Add an error handling mechanism to common, and rename it support.h.
This commit is contained in:
Mid Favila 2022-09-07 16:45:37 -04:00
parent a152301109
commit b7ac76440a
8 changed files with 113 additions and 61 deletions

3
README
View File

@ -10,3 +10,6 @@
than Busybox.
I recommend compiling these statically.
If SPLint is available, 'make check' can perform static analysis of
all available programs.

View File

@ -4,7 +4,7 @@ CC = cc
CFLAGS = -Wall -O2
LDFLAGS = -static
LDLIBS =
PREFIX = /usr/local/
PREFIX = usr/local
DESTDIR =
BIN = echo yes true false cat
@ -18,14 +18,15 @@ false:
cat: cat.c
install: all
install -Dm0775 ${BIN} ${DESTDIR}${PREFIX}bin
install -Dm0755 ${BIN} ${DESTDIR}/${PREFIX}/bin
clean:
rm echo true yes false cat
clean: all
rm ${BIN} *.o
check:
splint +checks +posix-strict-lib +showsummary +forcehints +showscan +showsourceloc +toctou +its4low +its4moderate +its4risky +its4veryrisky +its4mostrisky +bufferoverflow *.c *.h
.SUFFIXES: .c .o
.c.o:
${CC} ${CFLAGS} ${LDFLAGS} -c $<
:
echo hello

View File

@ -1,28 +1,22 @@
/* cat.c -- provides a routine to output the contents of the named files in either a buffered or unbuffered fashion. */
/* Accepts the following parameters: */
/* -u: Output should be unbuffered. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* Returns the following values: */
/* 0: All files were printed successfully. */
/* 1: One or more files could not be opened. */
#include "support.h"
#include "common.h"
/* If mode is set to zero, return a null pointer and print characters directly. Otherwise, return a pointer to a buffer containing */
/* the contents of the file pointed to by fp. The size of the new buffer is stored in the int pointed to by bufsize. */
char *cat(FILE *fp, const int mode, long unsigned *bufsize)
{
char c;
static long unsigned int i, finalbuflen, tmpbuflen;
char *arr, *tmparr, *finalarr;
tmpbuflen = 4096;
if(!mode)
{
static long unsigned int i, finalbuflen, tmpbuflen;
tmpbuflen = 4096;
char *arr = malloc(tmpbuflen);
char *tmparr = 0;
char *finalarr = 0;
arr = malloc(tmpbuflen);
tmparr = finalarr = 0;
for(i = 0, finalbuflen = 0; (c = fgetc(fp)) != EOF; i++, finalbuflen++)
{
if(i >= tmpbuflen)
@ -57,14 +51,14 @@ char *cat(FILE *fp, const int mode, long unsigned *bufsize)
int main(int argc, char **argv)
{
long unsigned int i, upresence, fppresence, buflen, newbuflen;
char *buf, *newbuf, *tmpbuf;
long unsigned int i, upresence, fppresence;
i = upresence = fppresence = 0;
long unsigned int buflen, newbuflen;
buflen = newbuflen = 0;
FILE *fp;
fp = 0;
i = upresence = fppresence = buflen = newbuflen = 0;
buf = newbuf = tmpbuf = 0;
fp = 0;
if((upresence = (getopt(argc, argv, ":u") == 'u')))
{

View File

@ -1,30 +0,0 @@
#include <iso646.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
/* arrccat -- function to copy arr2len bytes from arr2 to the arr1len'th position of arr1. returns *newarr, and stores the length */
/* of newarr[] in arr1len (that's why it needs to be a pointer to arr1len and not just a long unsigned integer). */
char *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t arr2len)
{
char *newarr;
unsigned int i, i2;
newarr = malloc(*arr1len+arr2len);
i = i2 = 0;
for(i = 0; i < *arr1len; i++)
{
newarr[i] = arr1[i];
}
for(i2 = 0; i2 < arr2len; i2++, i++)
{
newarr[i] = arr2[i2];
}
*arr1len = *arr1len+arr2len;
return newarr;
}

View File

@ -1,4 +1,4 @@
#include "common.h"
#include "support.h"
int main(int argc, char **argv)
{

84
src/support.h Normal file
View File

@ -0,0 +1,84 @@
#include <iso646.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
enum {
UNKNOWN_FAIL = 1,
MALLOC_FAIL,
NEEDARG_FAIL,
FOPEN_FAIL,
WRITE_FAIL
};
/* arrccat -- function to copy arr2len bytes from arr2 to the arr1len'th position of arr1. returns *newarr, and stores the length */
/* of newarr[] in arr1len (that's why it needs to be a pointer to arr1len and not just a long unsigned integer). */
char *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t arr2len)
{
char *newarr;
unsigned int i, i2;
newarr = malloc(*arr1len+arr2len);
i = i2 = 0;
for(i = 0; i < *arr1len; i++)
{
newarr[i] = arr1[i];
}
for(i2 = 0; i2 < arr2len; i2++, i++)
{
newarr[i] = arr2[i2];
}
*arr1len = *arr1len+arr2len;
return newarr;
}
/* Condition should be one of the above enum'd strings. Info is optional; it can just be NULL when irrelevant. */
void throw(long unsigned int condition, void *info)
{
switch(condition)
{
case UNKNOWN_FAIL: printf("unknown failure\n");
exit(UNKNOWN_FAIL);
case MALLOC_FAIL: printf("malloc failure\n");
exit(MALLOC_FAIL);
case NEEDARG_FAIL: printf("usage: %s\n", (char *) info);
exit(NEEDARG_FAIL);
case FOPEN_FAIL: printf("failed to open: %s\n", (char *) info);
exit(FOPEN_FAIL);
case WRITE_FAIL: printf("failed to write: %s\n", (char *) info);
exit(WRITE_FAIL);
default: printf("You shouldn't be seeing this.\nSomething is very wrong.\n");
exit(-256);
}
}
/* mastrcat -- improved string concat function. returns a pointer to the first element in a buffer containing the strings str1 */
/* and str2 joined end-to-end. */
char *mastrcat(char *str1, char *str2)
{
unsigned long int nbi, stri, nbsize;
char *nbuf;
nbi = stri = 0;
nbsize = (strlen(str1) + strlen(str2));
nbuf = malloc(nbsize);
for(stri = 0; str1[stri] != '\0'; nbi++, stri++)
{
nbuf[nbi] = str1[stri];
}
for(stri = 0; str2[stri] != '\0'; nbi++, stri++)
{
nbuf[nbi] = str2[stri];
}
return nbuf;
}

View File

@ -1,4 +1,4 @@
#include "common.h"
#include "support.h"
int main(int argc, char **argv)
{

View File

@ -1,4 +1,4 @@
#include "common.h"
#include "support.h"
int main(int argc, char **argv)
{