From: Sergey Matveev Date: Fri, 25 Oct 2024 11:58:42 +0000 (+0300) Subject: Satisfy linter X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=afeae6e62cb52b43ab3a3baa642be088b16f10470a7beeae820b2b95b6060498;p=keks.git Satisfy linter --- diff --git a/cyac/.clang-tidy b/cyac/.clang-tidy new file mode 100644 index 0000000..486254d --- /dev/null +++ b/cyac/.clang-tidy @@ -0,0 +1,13 @@ +Checks: " + *, + -altera-id-dependent-backward-branch, + -altera-struct-pack-align, + -altera-unroll-loops, + -bugprone-easily-swappable-parameters, + -cert-err33-c, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, + -cppcoreguidelines-avoid-magic-numbers, + -llvm-header-guard, + -llvmlibc-restrict-system-libc-headers, + -readability-*, +" diff --git a/cyac/cmd/cer-verify/cer-verify.c b/cyac/cmd/cer-verify/cer-verify.c index 50c9cc1..86b1201 100644 --- a/cyac/cmd/cer-verify/cer-verify.c +++ b/cyac/cmd/cer-verify/cer-verify.c @@ -44,8 +44,12 @@ main(int argc, char **argv) unsigned char *buf = NULL; size_t len = 0; if (!Mmap(&buf, &len, argv[i])) { - fprintf(stderr, "%s: %s\n", argv[i], strerror(errno)); - exit(EXIT_FAILURE); + fprintf( + stderr, + "%s: %s\n", + argv[i], + strerror(errno)); // NOLINT(concurrency-mt-unsafe) + exit(EXIT_FAILURE); // NOLINT(concurrency-mt-unsafe) } fputs(argv[i], stdout); fputs(":\t", stdout); @@ -67,7 +71,7 @@ main(int argc, char **argv) struct timespec now; errno = 0; if (clock_gettime(CLOCK_REALTIME, &now) != 0) { - fprintf(stderr, "%s\n", strerror(errno)); + fprintf(stderr, "%s\n", strerror(errno)); // NOLINT(concurrency-mt-unsafe) return EXIT_FAILURE; } @@ -84,8 +88,7 @@ main(int argc, char **argv) fputs("verifying ", stdout); UUIDPrint(toVerify->cid); fputs(": ", stdout); - if (!YACCerVerify( - &failReason, &verifier, toVerify, cers, (size_t)cersLen, opts)) { + if (!YACCerVerify(&failReason, &verifier, toVerify, cers, cersLen, opts)) { fputs(failReason, stdout); fputs("\n", stdout); return EXIT_FAILURE; diff --git a/cyac/cmd/lib/hex.c b/cyac/cmd/lib/hex.c index 25c1a3c..e6e6907 100644 --- a/cyac/cmd/lib/hex.c +++ b/cyac/cmd/lib/hex.c @@ -1,3 +1,4 @@ +#include #include #include "hex.h" @@ -14,8 +15,9 @@ HexEnc(const unsigned char *src, const size_t srcLen) } size_t i = 0; for (; i < srcLen; i++) { - dst[(i * 2) + 0] = hexdigits[(src[i] >> 4) & 0x0F]; - dst[(i * 2) + 1] = hexdigits[src[i] & 0x0F]; + dst[(i * 2) + 0] = hexdigits + [(src[i] >> (uint8_t)4) & (uint8_t)0x0F]; // NOLINT(hicpp-signed-bitwise) + dst[(i * 2) + 1] = hexdigits[src[i] & (uint8_t)0x0F]; } dst[srcLen * 2] = 0; return dst; diff --git a/cyac/cmd/lib/mmap.c b/cyac/cmd/lib/mmap.c index 45f4b93..d3d3a2f 100644 --- a/cyac/cmd/lib/mmap.c +++ b/cyac/cmd/lib/mmap.c @@ -12,7 +12,7 @@ bool Mmap(unsigned char **buf, size_t *len, const char *path) { - int fd = open(path, O_RDONLY | O_CLOEXEC); + int fd = open(path, O_RDONLY | O_CLOEXEC); // NOLINT(hicpp-signed-bitwise) if (fd == -1) { return false; } @@ -24,8 +24,8 @@ Mmap(unsigned char **buf, size_t *len, const char *path) (*len) = (size_t)sb.st_size; errno = 0; (*buf) = mmap(NULL, *len, PROT_READ, MAP_SHARED, fd, 0); - if ((*buf) == MAP_FAILED) { - fprintf(stderr, "mmap: %s\n", strerror(errno)); + if ((*buf) == MAP_FAILED) { // NOLINT(performance-no-int-to-ptr) + fprintf(stderr, "mmap: %s\n", strerror(errno)); // NOLINT(concurrency-mt-unsafe) return false; } return true; diff --git a/cyac/cmd/lib/printai.c b/cyac/cmd/lib/printai.c index afc4e93..8e2ffd2 100644 --- a/cyac/cmd/lib/printai.c +++ b/cyac/cmd/lib/printai.c @@ -26,6 +26,8 @@ PrintTAI64(const unsigned char *buf, const size_t len) case 12: fputs("TAI64N(", stdout); break; + default: + break; } struct timespec tv; enum YACErr err = YACTAI64ToTimespec(&tv, buf, len); @@ -59,7 +61,7 @@ PrintTAI64(const unsigned char *buf, const size_t len) return err; } time_t t = tv.tv_sec; - struct tm *tm = localtime(&t); + struct tm *tm = localtime(&t); // NOLINT(concurrency-mt-unsafe) if (tm == NULL) { hex = HexEnc(buf, len); fprintf(stdout, "unrepresentable: %s)\n", hex); diff --git a/cyac/cmd/print-items/print-items.c b/cyac/cmd/print-items/print-items.c index b314cb3..db2ffc0 100644 --- a/cyac/cmd/print-items/print-items.c +++ b/cyac/cmd/print-items/print-items.c @@ -31,14 +31,30 @@ static const size_t maxStrLen = 40; -static const char *ColourRed = "\x1b[31m"; -static const char *ColourGreen = "\x1b[32m"; -static const char *ColourYellow = "\x1b[33m"; -static const char *ColourBlue = "\x1b[34m"; -static const char *ColourMagenta = "\x1b[35m"; -static const char *ColourCyan = "\x1b[36m"; -static const char *ColourWhite = "\x1b[37m"; -static const char *ColourReset = "\x1b[0m"; +static const char + *ColourRed = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[31m"; +static const char + *ColourGreen = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[32m"; +static const char + *ColourYellow = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[33m"; +static const char + *ColourBlue = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[34m"; +static const char + *ColourMagenta = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[35m"; +static const char + *ColourCyan = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[36m"; +static const char + *ColourWhite = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[37m"; +static const char + *ColourReset = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + "\x1b[0m"; static void printIndent(const size_t indent) @@ -48,13 +64,17 @@ printIndent(const size_t indent) } } -static bool NoColour = false; -static bool NoOffsets = false; -static int OffDigits = 0; -static char OffFmt[16] = {0}; +static bool NoColour = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + false; +static bool NoOffsets = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + false; +static int OffDigits = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + 0; +static char OffFmt[16] = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + {0}; static enum YACErr -printer( +printer( // NOLINT(misc-no-recursion) const struct YACItems *items, size_t idx, size_t indent, @@ -261,10 +281,10 @@ main(int argc, char **argv) size_t len = 0; unsigned char *buf = NULL; if (!Mmap(&buf, &len, argv[1])) { - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); // NOLINT(concurrency-mt-unsafe) } - NoColour = getenv("NO_COLOR") != NULL; - NoOffsets = getenv("NO_OFFSETS") != NULL; + NoColour = getenv("NO_COLOR") != NULL; // NOLINT(concurrency-mt-unsafe) + NoOffsets = getenv("NO_OFFSETS") != NULL; // NOLINT(concurrency-mt-unsafe) OffDigits = (int)(1 + floor(log10((double)len))); snprintf(OffFmt, sizeof OffFmt, "%%s%%0%dzd%%s ", OffDigits); @@ -293,7 +313,7 @@ main(int argc, char **argv) "items: %zu size: %zu\n", items.len, items.len * (sizeof(struct YACItem) + (NoOffsets ? 0 : sizeof(size_t)))); - if (getenv("DO_ENCODE") != NULL) { + if (getenv("DO_ENCODE") != NULL) { // NOLINT(concurrency-mt-unsafe) unsigned char *dst = malloc(len); assert(dst != NULL); off = 0; diff --git a/cyac/cmd/print-itered/print-itered.c b/cyac/cmd/print-itered/print-itered.c index c9923e7..6636221 100644 --- a/cyac/cmd/print-itered/print-itered.c +++ b/cyac/cmd/print-itered/print-itered.c @@ -163,7 +163,7 @@ main(int argc, char **argv) size_t len = 0; unsigned char *buf = NULL; if (!Mmap(&buf, &len, argv[1])) { - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); // NOLINT(concurrency-mt-unsafe) } struct YACAtom atom; memset(&atom, 0, sizeof(struct YACAtom)); diff --git a/cyac/cmd/test-vector/test-vector.c b/cyac/cmd/test-vector/test-vector.c index 8230e36..b682604 100644 --- a/cyac/cmd/test-vector/test-vector.c +++ b/cyac/cmd/test-vector/test-vector.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -9,8 +10,8 @@ #include #include -static size_t Off = 0; -static size_t Got = 0; +static size_t Off = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +static size_t Got = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static void adder(const bool ok) @@ -20,12 +21,12 @@ adder(const bool ok) } int -main(int argc, char **argv) +main(int argc, char **argv) // NOLINT(misc-unused-parameters) { - const size_t len = 68 * 1024; + const size_t len = (size_t)68 * (uint16_t)1024; unsigned char *buf = malloc(len); assert(buf != NULL); - unsigned char *bin = malloc(1 << 17); + unsigned char *bin = malloc((uint32_t)1 << (uint8_t)17); assert(bin != NULL); adder(YACAtomMapEncode(&Got, buf + Off, len - Off)); // . diff --git a/cyac/lib/dec.c b/cyac/lib/dec.c index 2b3b19c..abd04ec 100644 --- a/cyac/lib/dec.c +++ b/cyac/lib/dec.c @@ -37,9 +37,10 @@ YACAtomDecode( } atom->tag = buf[0]; - if ((atom->tag & YACAtomStrings) > 0) { - atom->typ = ((atom->tag & YACAtomIsUTF8) == 0) ? YACItemBin : YACItemStr; - uint64_t l = atom->tag & 63; + if ((atom->tag & (uint8_t)YACAtomStrings) > 0) { + atom->typ = + ((atom->tag & (uint8_t)YACAtomIsUTF8) == 0) ? YACItemBin : YACItemStr; + uint64_t l = atom->tag & (uint8_t)63; size_t ll = 0; switch (l) { case 61: @@ -53,6 +54,8 @@ YACAtomDecode( ll = 8; l += 255 + 65535; break; + default: + break; } if (ll != 0) { (*got) += ll; @@ -81,12 +84,11 @@ YACAtomDecode( } size_t cpl = 0; uint32_t cp = YACUTF8InvalidCp; - for (size_t n = 0; n < l;) { + for (size_t n = 0; n < l; n += cpl) { cpl = YACUTF8CpDecode(&cp, atom->v.str.ptr + (ptrdiff_t)n, l - n); if (cp == YACUTF8InvalidCp) { return YACErrBadUTF8; } - n += cpl; } } return YACErrNo; @@ -102,12 +104,13 @@ YACAtomDecode( atom->v.nint = -1 - (int64_t)(atom->tag - YACAtomIntN1); return YACErrNo; } - switch (atom->tag & 0xF0) { + switch (atom->tag & (uint8_t)0xF0) { case YACAtomIntPos1: case YACAtomIntNeg1: { - atom->typ = ((atom->tag & YACAtomIntNeg1) == YACAtomIntNeg1) ? YACItemNint : - YACItemPint; - const size_t l = (atom->tag & 0x0F) + 1; + atom->typ = ((atom->tag & (uint8_t)YACAtomIntNeg1) == YACAtomIntNeg1) ? + YACItemNint : + YACItemPint; + const size_t l = (atom->tag & (uint8_t)0x0F) + 1; (*got) += l; if (len < (*got)) { return YACErrNotEnough; @@ -125,7 +128,7 @@ YACAtomDecode( return YACErrIntNonMinimal; } if (atom->typ == YACItemNint) { - if (atom->v.pint >= ((uint64_t)1 << 63)) { + if (atom->v.pint >= ((uint64_t)1 << (uint8_t)63)) { atom->typ = YACItemRaw; atom->v.str.len = l; atom->v.str.ptr = buf + 1; @@ -237,7 +240,7 @@ YACAtomDecode( atom->v.str.len = l; atom->v.str.ptr = buf + 1; uint64_t v = yacFromBE(buf + 1, 8); - if (v > ((uint64_t)1 << 63)) { + if (v > ((uint64_t)1 << (uint8_t)63)) { return YACErrTAI64TooBig; } switch (l) { @@ -253,6 +256,8 @@ YACAtomDecode( return YACErrTAI64BadAsec; } break; + default: + break; } break; } diff --git a/cyac/lib/dectai.c b/cyac/lib/dectai.c index 642dc8b..e0d0b9d 100644 --- a/cyac/lib/dectai.c +++ b/cyac/lib/dectai.c @@ -50,9 +50,9 @@ YACTimespecToUTC(struct timespec *ts) int64_t v = (int64_t)(ts->tv_sec); { int64_t diff = 0; - for (size_t i = 0; i < YACLeapsecsN; i++) { - if (v > (YACLeapsecs[i] + (int64_t)YACLeapsecsN - (int64_t)i)) { - diff = 10 + (int64_t)YACLeapsecsN - (int64_t)i; + for (int64_t i = 0; i < YACLeapsecsN; i++) { + if (v > (YACLeapsecs[i] + YACLeapsecsN - i)) { + diff = 10 + YACLeapsecsN - i; break; } } diff --git a/cyac/lib/enc.c b/cyac/lib/enc.c index 737a654..81df522 100644 --- a/cyac/lib/enc.c +++ b/cyac/lib/enc.c @@ -106,7 +106,7 @@ YACAtomUintEncode(size_t *len, unsigned char *buf, const size_t cap, const uint6 { bool ok = yacAtomIntEncode(len, buf, cap, v); if (ok) { - buf[0] |= (v < 32) ? YACAtomInt0 : YACAtomIntPos1; + buf[0] |= (unsigned char)((v < 32) ? YACAtomInt0 : YACAtomIntPos1); } return ok; } @@ -120,7 +120,7 @@ YACAtomSintEncode(size_t *len, unsigned char *buf, const size_t cap, const int64 const uint64_t vp = (uint64_t)(-(v + 1)); bool ok = yacAtomIntEncode(len, buf, cap, vp); if (ok) { - buf[0] |= (vp < 32) ? YACAtomIntN1 : YACAtomIntNeg1; + buf[0] |= (unsigned char)((vp < 32) ? YACAtomIntN1 : YACAtomIntNeg1); } return ok; } @@ -186,7 +186,7 @@ yacAtomStrEncode( } else if (srcLen >= 61) { lVal = 61; lLen = 1; - lBuf[0] = (unsigned char)((srcLen - 61) & 0xFF); + lBuf[0] = (unsigned char)((srcLen - 61) & (uint8_t)0xFF); } else { lVal = (unsigned char)srcLen; } @@ -198,9 +198,9 @@ yacAtomStrEncode( if (cap < (*len)) { return false; } - buf[0] = YACAtomStrings | lVal; + buf[0] = (uint8_t)YACAtomStrings | lVal; if (utf8) { - buf[0] |= YACAtomIsUTF8; + buf[0] |= (unsigned char)YACAtomIsUTF8; } memcpy(buf + 1, lBuf, lLen); memcpy(buf + 1 + lLen, src, srcLen); diff --git a/cyac/lib/enctai.c b/cyac/lib/enctai.c index 2538c0b..f9956f8 100644 --- a/cyac/lib/enctai.c +++ b/cyac/lib/enctai.c @@ -40,7 +40,7 @@ YACTimespecToTAI(struct timespec *ts) { int64_t v = (int64_t)(ts->tv_sec); int64_t diff = 10; - for (size_t i = 0; i < YACLeapsecsN; i++) { + for (int64_t i = 0; i < YACLeapsecsN; i++) { if (v > YACLeapsecs[i]) { diff += YACLeapsecsN - i; break; diff --git a/cyac/lib/items.c b/cyac/lib/items.c index c9182ca..d85b9a2 100644 --- a/cyac/lib/items.c +++ b/cyac/lib/items.c @@ -114,7 +114,7 @@ yacItemsAdd( } enum YACErr -YACItemsParse( +YACItemsParse( // NOLINT(misc-no-recursion) struct YACItems *items, size_t *off, const unsigned char *buf, @@ -270,7 +270,7 @@ YACItemsParse( } bool -YACItemsEncode( +YACItemsEncode( // NOLINT(misc-no-recursion) const struct YACItems *items, size_t idx, size_t *off, diff --git a/cyac/lib/leapsecs.c b/cyac/lib/leapsecs.c index b1d86ae..7f703d4 100644 --- a/cyac/lib/leapsecs.c +++ b/cyac/lib/leapsecs.c @@ -1,9 +1,8 @@ -#include #include #include "leapsecs.h" -const size_t YACLeapsecsN = 27; +const int64_t YACLeapsecsN = 27; const int64_t YACLeapsecs[] = { 1483228800, // 2017-01 1435708800, // 2015-07 diff --git a/cyac/lib/leapsecs.h b/cyac/lib/leapsecs.h index 206c6f3..71c0efe 100644 --- a/cyac/lib/leapsecs.h +++ b/cyac/lib/leapsecs.h @@ -10,7 +10,7 @@ // second was added. @var{YACLeapsecsN} variable holds the length of // that list. // @end deftypevar -extern const size_t YACLeapsecsN; +extern const int64_t YACLeapsecsN; extern const int64_t YACLeapsecs[]; #endif // YAC_LEAPSECS_H diff --git a/cyac/lib/pki/cer.c b/cyac/lib/pki/cer.c index 0807e83..8b3dce0 100644 --- a/cyac/lib/pki/cer.c +++ b/cyac/lib/pki/cer.c @@ -1,4 +1,5 @@ #include +#include #include #include "../dec.h" @@ -265,8 +266,8 @@ YACCerVerify( return false; } } - const size_t cap = 1 << 10; - unsigned char buf[1 << 10] = {0}; + const size_t cap = (size_t)1 << (uint8_t)10; + unsigned char buf[(uint16_t)1 << (uint8_t)10] = {0}; size_t off = 0; { const size_t items = 5; diff --git a/cyac/lib/tobe.c b/cyac/lib/tobe.c index 530a798..a66fb1c 100644 --- a/cyac/lib/tobe.c +++ b/cyac/lib/tobe.c @@ -8,7 +8,7 @@ yacToBE(unsigned char *buf, const size_t len, const uint64_t v) { for (size_t i = 0; i < len; i++) { buf[i] = - (unsigned char)(((v & ((uint64_t)0xFF << ((len - i - 1) * 8))) >> ((len - i - 1) * 8)) & 0xFF); + (unsigned char)(((v & ((uint64_t)0xFF << ((len - i - 1) * 8))) >> ((len - i - 1) * 8)) & (uint8_t)0xFF); } // this can be replaced by a switch with hard-coded decoding // sequence without any loops for each of eight possible lengths diff --git a/cyac/lib/utf8.c b/cyac/lib/utf8.c index 9d6c4a8..865ec08 100644 --- a/cyac/lib/utf8.c +++ b/cyac/lib/utf8.c @@ -10,7 +10,7 @@ #define BETWEEN(c, l, u) (((c) >= (l)) && ((c) <= (u))) -uint32_t YACUTF8InvalidCp = 0xFFFD; +const uint32_t YACUTF8InvalidCp = 0xFFFD; static const struct { uint32_t mincp; @@ -25,31 +25,31 @@ static const struct { .lower = 0x00, /* 00000000 */ .upper = 0x7F, /* 01111111 */ .mincp = (uint32_t)0, - .maxcp = ((uint32_t)1 << 7) - 1, /* 7 bits capacity */ + .maxcp = ((uint32_t)1 << (uint8_t)7) - 1, /* 7 bits capacity */ }, [1] = { /* 110xxxxx */ .lower = 0xC0, /* 11000000 */ .upper = 0xDF, /* 11011111 */ - .mincp = (uint32_t)1 << 7, - .maxcp = ((uint32_t)1 << 11) - 1, /* 5+6=11 bits capacity */ + .mincp = (uint32_t)1 << (uint8_t)7, + .maxcp = ((uint32_t)1 << (uint8_t)11) - 1, /* 5+6=11 bits capacity */ }, [2] = { /* 1110xxxx */ .lower = 0xE0, /* 11100000 */ .upper = 0xEF, /* 11101111 */ - .mincp = (uint32_t)1 << 11, - .maxcp = ((uint32_t)1 << 16) - 1, /* 4+6+6=16 bits capacity */ + .mincp = (uint32_t)1 << (uint8_t)11, + .maxcp = ((uint32_t)1 << (uint8_t)16) - 1, /* 4+6+6=16 bits capacity */ }, [3] = { /* 11110xxx */ .lower = 0xF0, /* 11110000 */ .upper = 0xF7, /* 11110111 */ - .mincp = (uint32_t)1 << 16, - .maxcp = ((uint32_t)1 << 21) - 1, /* 3+6+6+6=21 bits capacity */ + .mincp = (uint32_t)1 << (uint8_t)16, + .maxcp = ((uint32_t)1 << (uint8_t)21) - 1, /* 3+6+6+6=21 bits capacity */ }, }; @@ -91,7 +91,8 @@ YACUTF8CpDecode(uint32_t *cp, const unsigned char *str, const size_t len) (*cp) = YACUTF8InvalidCp; return 1 + (i - 1); } - (*cp) = (*cp << 6) | (str[i] & 0x3F); + (*cp) = (*cp << (uint8_t)6) | // NOLINT(hicpp-signed-bitwise) + (str[i] & (uint8_t)0x3F); } if ((*cp < lut[off].mincp) || BETWEEN(*cp, 0xD800, 0xDFFF) || (*cp > 0x10FFFF)) { (*cp) = YACUTF8InvalidCp; diff --git a/cyac/lib/utf8.h b/cyac/lib/utf8.h index 4a3dd64..6c455e0 100644 --- a/cyac/lib/utf8.h +++ b/cyac/lib/utf8.h @@ -4,7 +4,7 @@ #include #include -extern uint32_t YACUTF8InvalidCp; +extern const uint32_t YACUTF8InvalidCp; size_t YACUTF8CpDecode(uint32_t *cp, const unsigned char *str, const size_t len);