Repository navigation
Accept negative numbers as option arguments in apps - #3425
Open
StevenLi-phoenix wants to merge 1 commit into
Open
StevenLi-phoenix wants to merge 1 commit into
StevenLi-phoenix wants to merge 1 commit into
Conversation
NEXTARG() in avifenc and avifdec treated any argument that starts with '-' as the next option, so "avifenc --jobs -10" failed with "--jobs requires an argument." An argument where '-' is followed by a digit is now passed to the option, which validates it. Also end the "requires an argument." message with a newline. Reject the negative values that options would otherwise turn into large unsigned values: - --jobs in avifenc and avifdec, and avifdec --index (-1 would mean all frames). - The avifenc options parsed by parseU32List(), such as --cicp and --pasp. --clap still accepts negative values because its horizontal and vertical offsets are signed. - avifenc --irot and --imir. The value is now range-checked before the cast to uint8_t, so --irot 259 is no longer read as 3. - avifdec --size-limit and --dimension-limit, where strtoul() returns UINT32_MAX for -1 if unsigned long is 32 bits. Fixes AOMediaCodec#1750. Co-authored-by: Claude Opus-5-5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1750
Problem
NEXTARG()inapps/avifenc.candapps/avifdec.ctreated every argument that starts with-as the next option. Soavifenc --jobs -10 in.png out.aviffailed with--jobs requires an argument.(with no trailing newline), and no option ever got to check a negative value.Fix
NEXTARG()now passes the next argument to the current option when the-is followed by a digit, as in-10. Option names (-o,--jobs), a bare-and a missing argument still give<option> requires an argument., which now ends with\n. A side effect: an option can now take a file name such as-1.avif(-o -1.avif).Some options turned a negative value into a large unsigned value. They now reject it:
--jobs(avifenc and avifdec) and avifdec--index.--index -1would otherwise mean all frames.--jobs 0is still clamped to 1.parseU32List():--depth,--scaling-mode,--grid,--cicp,--pasp,--cropand--clli. Without this,--cicp -1/-1/-1wrote color primaries 65535 and--pasp -1,1wrote an hSpacing of 4294967295.--clapstill takes negative values, because its horizontal and vertical offsets are signed (ISO/IEC 14496-12:2022, 12.1.4.1). It callsparseIntList()withallowNegative, andavifCropRectFromCleanApertureBox()still rejects a negative width, height or denominator.--irotand--imir. The value is range-checked as anintbefore the cast touint8_t, so--irot -253is not read as 3.--size-limitand--dimension-limit, which now reject a leading-.strtoul("-1")returnsULONG_MAX, which equalsUINT32_MAXwhereunsigned longis 32 bits (Windows), so-1would have been accepted there as the largest limit.Two kinds of command lines that work on main now fail. Both wrote wrapped values:
parseU32List()option, for example--cicp 1/-1/1(transfer characteristics 65535) or--pasp 1,-1;--irot 256to259and--imir 256or257, which were read modulo 256 (--irot 259as 3).Not changed:
-q,--speed,--min/--max,--tilerowslog2, avifdec-q/--png-compress, ...) clamp negative values too. avifenc-k -3behaves like-k 0, which means no maximum keyframe interval.intrange still overflow inatoi(), as on main, for positive and negative input alike.tests/avifyuv.ckeeps its own copy of the oldNEXTARG(), for its-moption, because it is a test tool.For the maintainer: a negative
--jobscould be clamped to 1 instead, as 0 is and as avifgainmaputil'sJobsConverterdoes. I made it an error because the issue asks for a meaningful error message. I can switch it.Testing
New assertions in
tests/test_cmd.sh:ERROR:message: avifenc--jobs -10,--cicp -1/-1/-1,--pasp -1,1,--irot -253and--imir -255; avifdec--jobs -10,--index -1and--size-limit -1.--jobs -oand--jobs -imust still report--jobs requires an argument.--clap 100,1,100,1,-10,1,-20,1must still encode, andavifdec --infomust showhOff: -10/1, vOff: -20/1.Build (macOS arm64, Apple clang, system aom 3.15.2 and dav1d 1.5.4):
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DAVIF_CODEC_AOM=SYSTEM -DAVIF_CODEC_DAV1D=SYSTEM \ -DAVIF_LIBYUV=OFF -DAVIF_BUILD_APPS=ON -DAVIF_BUILD_TESTS=ON -DAVIF_GTEST=LOCAL -DAVIF_ENABLE_WERROR=ON ninja -C buildtest_cmd.shfails atgrep 'ERROR: Invalid number of jobs: -10', because avifenc prints--jobs requires an argument.NEXTARG(),--jobsand--indexchanges, it fails becauseavifenc --cicp -1/-1/-1exits 0.ctest --test-dir build -R '^test_cmd'passes 11 of 11 tests, and the fullctest --test-dir build -j 8passes 62 of 62.test_cmd_stdinis not built in this configuration becauseAVIF_ENABLE_JPEG_GAIN_MAP_CONVERSIONis off.clang-format19.1.7 (-style=file --dry-run --Werror) reports nothing forapps/avifenc.candapps/avifdec.c.I did not test on Windows or Linux. On macOS,
unsigned longis 64 bits, so--size-limit -1also fails without the new leading--check.