Exclude more lines from test coverage (#1663)

These fall into a few categories:
- `_unreachable()`
- Verbose print messages
- Errors that should never practically occur (alloc/read/write failure,
  more than UINT32_MAX anonymous labels, etc)
This commit is contained in:
Rangi
2025-02-17 04:56:10 -05:00
committed by GitHub
parent 632342b254
commit 0150eb4bf3
10 changed files with 88 additions and 12 deletions

View File

@@ -698,10 +698,12 @@ static char const *mbcName(MbcType type) {
case MBC_WRONG_FEATURES:
case MBC_BAD_RANGE:
case MBC_BAD_TPP1:
// LCOV_EXCL_START
unreachable_();
}
unreachable_();
// LCOV_EXCL_STOP
}
static bool hasRAM(MbcType type) {
@@ -763,7 +765,7 @@ static bool hasRAM(MbcType type) {
break;
}
unreachable_();
unreachable_(); // LCOV_EXCL_LINE
}
static uint8_t const nintendoLogo[] = {
@@ -813,8 +815,9 @@ static ssize_t readBytes(int fd, uint8_t *buf, size_t len) {
while (len) {
ssize_t ret = read(fd, buf, len);
if (ret == -1 && errno != EINTR) { // Return errors, unless we only were interrupted
return -1;
// Return errors, unless we only were interrupted
if (ret == -1 && errno != EINTR) {
return -1; // LCOV_EXCL_LINE
}
// EOF reached
if (ret == 0) {
@@ -840,8 +843,9 @@ static ssize_t writeBytes(int fd, uint8_t *buf, size_t len) {
while (len) {
ssize_t ret = write(fd, buf, len);
if (ret == -1 && errno != EINTR) { // Return errors, unless we only were interrupted
return -1;
// Return errors, unless we only were interrupted
if (ret == -1 && errno != EINTR) {
return -1; // LCOV_EXCL_LINE
}
// If anything was written, accumulate it, and continue
if (ret != -1) {
@@ -895,8 +899,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
ssize_t headerSize = (cartridgeType & 0xFF00) == TPP1 ? 0x154 : 0x150;
if (rom0Len == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to read \"%s\"'s header: %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
} else if (rom0Len < headerSize) {
report(
"FATAL: \"%s\" too short, expected at least %jd ($%jx) bytes, got only %jd\n",
@@ -1134,8 +1140,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
// write the header
if (input == output) {
if (lseek(output, 0, SEEK_SET) == static_cast<off_t>(-1)) {
// LCOV_EXCL_START
report("FATAL: Failed to rewind \"%s\": %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
}
// If modifying the file in-place, we only need to edit the header
// However, padding may have modified ROM0 (added padding), so don't in that case
@@ -1146,9 +1154,12 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
writeLen = writeBytes(output, rom0, rom0Len);
if (writeLen == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to write \"%s\"'s ROM0: %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
} else if (writeLen < rom0Len) {
// LCOV_EXCL_START
report(
"FATAL: Could only write %jd of \"%s\"'s %jd ROM0 bytes\n",
static_cast<intmax_t>(writeLen),
@@ -1156,6 +1167,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
static_cast<intmax_t>(rom0Len)
);
return;
// LCOV_EXCL_STOP
}
// Output ROMX if it was buffered
@@ -1164,9 +1176,12 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
// so it's fine to cast to `size_t`
writeLen = writeBytes(output, romx.data(), totalRomxLen);
if (writeLen == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to write \"%s\"'s ROMX: %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
} else if (static_cast<size_t>(writeLen) < totalRomxLen) {
// LCOV_EXCL_START
report(
"FATAL: Could only write %jd of \"%s\"'s %zu ROMX bytes\n",
static_cast<intmax_t>(writeLen),
@@ -1174,6 +1189,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
totalRomxLen
);
return;
// LCOV_EXCL_STOP
}
}
@@ -1181,8 +1197,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
if (padValue != UNSPECIFIED) {
if (input == output) {
if (lseek(output, 0, SEEK_END) == static_cast<off_t>(-1)) {
// LCOV_EXCL_START
report("FATAL: Failed to seek to end of \"%s\": %s\n", name, strerror(errno));
return;
// LCOV_EXCL_STOP
}
}
memset(bank, padValue, sizeof(bank));
@@ -1196,8 +1214,10 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
// The return value is either -1, or at most `thisLen`,
// so it's fine to cast to `size_t`
if (static_cast<size_t>(ret) != thisLen) {
// LCOV_EXCL_START
report("FATAL: Failed to write \"%s\"'s padding: %s\n", name, strerror(errno));
break;
// LCOV_EXCL_STOP
}
len -= thisLen;
}
@@ -1224,12 +1244,16 @@ static bool processFilename(char const *name) {
Defer closeInput{[&] { close(input); }};
struct stat stat;
if (fstat(input, &stat) == -1) {
// LCOV_EXCL_START
report("FATAL: Failed to stat \"%s\": %s\n", name, strerror(errno));
} else if (!S_ISREG(stat.st_mode)) { // TODO: Do we want to support other types?
// LCOV_EXCL_STOP
} else if (!S_ISREG(stat.st_mode)) { // TODO: Do we want to support FIFOs or symlinks?
// LCOV_EXCL_START
report(
"FATAL: \"%s\" is not a regular file, and thus cannot be modified in-place\n",
name
);
// LCOV_EXCL_STOP
} else if (stat.st_size < 0x150) {
// This check is in theory redundant with the one in `processFile`, but it
// prevents passing a file size of 0, which usually indicates pipes
@@ -1423,16 +1447,20 @@ int main(int argc, char *argv[]) {
}
case 'V':
// LCOV_EXCL_START
printf("rgbfix %s\n", get_package_version_string());
exit(0);
// LCOV_EXCL_STOP
case 'v':
fixSpec = FIX_LOGO | FIX_HEADER_SUM | FIX_GLOBAL_SUM;
break;
default:
// LCOV_EXCL_START
printUsage();
exit(1);
// LCOV_EXCL_STOP
}
}

View File

@@ -103,6 +103,7 @@ void fatal(char const *fmt, ...) {
}
void Options::verbosePrint(uint8_t level, char const *fmt, ...) const {
// LCOV_EXCL_START
if (verbosity >= level) {
va_list ap;
@@ -110,6 +111,7 @@ void Options::verbosePrint(uint8_t level, char const *fmt, ...) const {
vfprintf(stderr, fmt, ap);
va_end(ap);
}
// LCOV_EXCL_STOP
}
// Short options
@@ -586,13 +588,17 @@ static char *parseArgv(int argc, char *argv[]) {
options.tilemap = musl_optarg;
break;
case 'V':
// LCOV_EXCL_START
printf("rgbgfx %s\n", get_package_version_string());
exit(0);
// LCOV_EXCL_STOP
case 'v':
// LCOV_EXCL_START
if (options.verbosity < Options::VERB_VVVVVV) {
++options.verbosity;
}
break;
// LCOV_EXCL_STOP
case 'x':
options.trim = parseNumber(arg, "Number of tiles to trim", 0);
if (*arg != '\0') {
@@ -750,6 +756,7 @@ int main(int argc, char *argv[]) {
parseExternalPalSpec(localOptions.externalPalSpec);
}
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_CFG) {
fprintf(stderr, "rgbgfx %s\n", get_package_version_string());
@@ -862,6 +869,7 @@ int main(int argc, char *argv[]) {
printPath("Output palettes", options.palettes);
fputs("Ready.\n", stderr);
}
// LCOV_EXCL_STOP
// Do not do anything if option parsing went wrong.
requireZeroErrors();

View File

@@ -559,6 +559,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
}
}
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&assignment : assignments) {
fprintf(stderr, "{ ");
@@ -571,11 +572,13 @@ std::tuple<DefaultInitVec<size_t>, size_t>
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
}
// LCOV_EXCL_STOP
// "Decant" the result
decant(assignments, protoPalettes);
// Note that the result does not contain any empty palettes
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&assignment : assignments) {
fprintf(stderr, "{ ");
@@ -588,6 +591,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
}
}
// LCOV_EXCL_STOP
DefaultInitVec<size_t> mappings(protoPalettes.size());
for (size_t i = 0; i < assignments.size(); ++i) {

View File

@@ -40,7 +40,7 @@ void sortIndexed(
return true;
}
}
unreachable_(); // This should not be possible
unreachable_(); // LCOV_EXCL_LINE
});
}
}

View File

@@ -193,13 +193,15 @@ public:
PNG_LIBPNG_VER_STRING, static_cast<png_voidp>(this), handleError, handleWarning
);
if (!png) {
fatal("Failed to allocate PNG structure: %s", strerror(errno));
fatal("Failed to create PNG read structure: %s", strerror(errno)); // LCOV_EXCL_LINE
}
info = png_create_info_struct(png);
if (!info) {
// LCOV_EXCL_START
png_destroy_read_struct(&png, nullptr, nullptr);
fatal("Failed to allocate PNG info structure: %s", strerror(errno));
fatal("Failed to create PNG info structure: %s", strerror(errno));
// LCOV_EXCL_STOP
}
png_set_read_fn(png, this, readData);
@@ -555,6 +557,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
auto [mappings, nbPalettes] = overloadAndRemove(protoPalettes);
assume(mappings.size() == protoPalettes.size());
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
fprintf(
stderr,
@@ -566,6 +569,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
fprintf(stderr, "%zu -> %zu\n", i, mappings[i]);
}
}
// LCOV_EXCL_STOP
std::vector<Palette> palettes(nbPalettes);
// If the image contains at least one transparent pixel, force transparency in the first slot of
@@ -653,6 +657,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
}
static void outputPalettes(std::vector<Palette> const &palettes) {
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto &&palette : palettes) {
fputs("{ ", stderr);
@@ -662,6 +667,7 @@ static void outputPalettes(std::vector<Palette> const &palettes) {
fputs("}\n", stderr);
}
}
// LCOV_EXCL_STOP
if (palettes.size() > options.nbPalettes) {
// If the palette generation is wrong, other (dependee) operations are likely to be
@@ -676,7 +682,9 @@ static void outputPalettes(std::vector<Palette> const &palettes) {
if (!options.palettes.empty()) {
File output;
if (!output.open(options.palettes, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", output.c_str(options.palettes), strerror(errno));
// LCOV_EXCL_STOP
}
for (Palette const &palette : palettes) {
@@ -829,7 +837,9 @@ static void outputUnoptimizedTileData(
) {
File output;
if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", output.c_str(options.output), strerror(errno));
// LCOV_EXCL_STOP
}
uint16_t widthTiles = options.inputSlice.width ? options.inputSlice.width : png.getWidth() / 8;
@@ -868,7 +878,9 @@ static void outputUnoptimizedMaps(
if (!path.empty()) {
file.emplace();
if (!file->open(path, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", file->c_str(options.tilemap), strerror(errno));
// LCOV_EXCL_STOP
}
}
};
@@ -1013,7 +1025,9 @@ static UniqueTiles dedupTiles(
static void outputTileData(UniqueTiles const &tiles) {
File output;
if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", output.c_str(options.output), strerror(errno));
// LCOV_EXCL_STOP
}
uint16_t tileID = 0;
@@ -1035,7 +1049,9 @@ static void outputTileData(UniqueTiles const &tiles) {
static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) {
File output;
if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", output.c_str(options.tilemap), strerror(errno));
// LCOV_EXCL_STOP
}
for (AttrmapEntry const &entry : attrmap) {
@@ -1048,7 +1064,9 @@ static void outputAttrmap(
) {
File output;
if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", output.c_str(options.attrmap), strerror(errno));
// LCOV_EXCL_STOP
}
for (AttrmapEntry const &entry : attrmap) {
@@ -1064,7 +1082,9 @@ static void outputPalmap(
) {
File output;
if (!output.open(options.palmap, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", output.c_str(options.palmap), strerror(errno));
// LCOV_EXCL_STOP
}
for (AttrmapEntry const &entry : attrmap) {
@@ -1092,6 +1112,7 @@ void process() {
// Now, we have all the image's colors in `colors`
// The next step is to order the palette
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
fputs("Image colors: [ ", stderr);
for (auto const &slot : colors) {
@@ -1102,6 +1123,7 @@ void process() {
}
fputs("]\n", stderr);
}
// LCOV_EXCL_STOP
// Now, iterate through the tiles, generating proto-palettes as we go
// We do this unconditionally because this performs the image validation (which we want to
@@ -1191,6 +1213,7 @@ continue_visiting_tiles:;
protoPalettes.size(),
protoPalettes.size() != 1 ? "s" : ""
);
// LCOV_EXCL_START
if (options.verbosity >= Options::VERB_INTERM) {
for (auto const &protoPal : protoPalettes) {
fputs("[ ", stderr);
@@ -1200,6 +1223,7 @@ continue_visiting_tiles:;
fputs("]\n", stderr);
}
}
// LCOV_EXCL_STOP
if (options.palSpecType == Options::EMBEDDED) {
generatePalSpec(png);

View File

@@ -401,7 +401,9 @@ void reverse() {
options.verbosePrint(Options::VERB_LOG_ACT, "Writing image...\n");
File pngFile;
if (!pngFile.open(options.input, std::ios::out | std::ios::binary)) {
// LCOV_EXCL_START
fatal("Failed to create \"%s\": %s", pngFile.c_str(options.input), strerror(errno));
// LCOV_EXCL_STOP
}
png_structp png = png_create_write_struct(
PNG_LIBPNG_VER_STRING,
@@ -410,11 +412,15 @@ void reverse() {
pngWarning
);
if (!png) {
// LCOV_EXCL_START
fatal("Failed to create PNG write struct: %s", strerror(errno));
// LCOV_EXCL_STOP
}
png_infop pngInfo = png_create_info_struct(png);
if (!pngInfo) {
fatal("Failed to create PNG info struct: %s", strerror(errno));
// LCOV_EXCL_START
fatal("Failed to create PNG info structure: %s", strerror(errno));
// LCOV_EXCL_STOP
}
png_set_write_fn(png, &pngFile, writePng, flushPng);

View File

@@ -424,5 +424,5 @@ max_out:
}
}
unreachable_();
unreachable_(); // LCOV_EXCL_LINE
}

View File

@@ -397,11 +397,15 @@ int main(int argc, char *argv[]) {
is32kMode = true;
break;
case 'V':
// LCOV_EXCL_START
printf("rgblink %s\n", get_package_version_string());
exit(0);
// LCOV_EXCL_STOP
case 'v':
// LCOV_EXCL_START
beVerbose = true;
break;
// LCOV_EXCL_STOP
case 'w':
isWRAM0Mode = true;
break;

View File

@@ -228,7 +228,7 @@ static uint8_t parseHexDigit(int c) {
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else {
unreachable_();
unreachable_(); // LCOV_EXCL_LINE
}
}

View File

@@ -208,8 +208,10 @@ static void mergeSections(Section &target, std::unique_ptr<Section> &&other) {
break;
case SECTION_NORMAL:
// LCOV_EXCL_START
unreachable_();
}
// LCOV_EXCL_STOP
// Note that the order in which fragments are stored in the `nextu` list does not
// really matter, only that offsets were properly computed above