other/password.py: alternative_password_generator ignores the requested length when the required characters exceed it
维护者通常 2 天内回复
评估
调研方向
打开 other/password.py 并阅读 alternative_password_generator;bug 位于 i -= len(chars_incl) / quotient = i // 3 / remainder = i % 3 这一代码块中,当 i 变为负数时行为异常。用 issue 中的代码片段复现问题,然后决定当 len(chars_incl) > i 时是抛出 ValueError 还是进行钳制,并检查 main() 是否需要报告该错误。完成的含义是两个复现场景都返回所请求的长度,且对有效输入的现有行为保持不变。
由索引模型根据 Issue 内容生成。
描述
Repository commit
6e03a48579ca212a08b36be00378f887dc41a003
Python version (python --version)
Python 3.13.5
Dependencies version (pip freeze)
None needed — other/password.py only uses the standard library (secrets, random, string).
Expected behavior
alternative_password_generator(chars_incl, i) should return a password whose length matches the requested length i (the main() prompt calls it "the max length of your password", and the comment above the function says i = how many letters or characters the password length will be).
Actual behavior
When chars_incl is longer than the requested length i, the subtraction i -= len(chars_incl) makes i negative, and the generator silently returns a password longer than what was asked for:
>>> from other.password import alternative_password_generator
>>> len(alternative_password_generator("abcdefgh", 5)) # asked for 5
8
>>> len(alternative_password_generator("abcd", 3)) # asked for 3
5
In the second case i becomes -1, so quotient = i // 3 is -1 and remainder = i % 3 is 2 (Python's modulo of a negative number is positive), which adds one extra random letter on top of all the required characters.
A user who types a max length smaller than their required-characters string in main() gets a password that silently exceeds the max length they asked for. If the requested length is at least len(chars_incl) everything works as expected.
Steps to reproduce
python - <<'EOF'
from other.password import alternative_password_generator
print(len(alternative_password_generator("abcdefgh", 5))) # expected 5, prints 8
print(len(alternative_password_generator("abcd", 3))) # expected 3, prints 5
EOF
Suspected cause
The negative remainder path in alternative_password_generator:
i -= len(chars_incl)
quotient = i // 3
remainder = i % 3
When len(chars_incl) > i, quotient goes negative (the random() calls then contribute nothing because range() of a negative is empty) while remainder stays positive, so quotient + remainder adds extra characters. Guarding the input (if i < len(chars_incl): raise ValueError(...) or clamping) would keep the generator honest about the requested length.
- 主要语言
- Python
- 星标
- 225k
- 派生
- 51.2k
- 平均合并
- 1 天 13 小时
- 30 天内合并 PR
- 32
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TheAlgorithms/Python 的其他 Issue
-
binomial_distribution overflows at 171 trials even when the result is representable可能已有人在做 @landfvit 于 1 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 85/100
TheAlgorithms/Python#15656 · 1 条评论 ·
维护者通常 2 天内回复
-
continued_fraction uses float division, producing invalid terms for large integers and raising OverflowError可能已有人在做 @trleo 于 1 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 88/100
TheAlgorithms/Python#15655 · 2 条评论 ·
维护者通常 2 天内回复
-
ciphers/base32.py and ciphers/base85.py encode Unicode code points instead of bytes: output diverges from the spec for non-ASCII, round-trips corrupt chars >= U+0400, arbitrary bytes crash可能已有人在做 @Nate-br 于 3 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
TheAlgorithms/Python#15636 · 1 条评论 ·
维护者通常 2 天内回复
-
ciphers/des_ecb.py: encrypt/decrypt silently corrupts any character above U+00FF (round-trip fails)可能已有人在做 @mohammedanasnakhuda 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
TheAlgorithms/Python#15634 ·
维护者通常 2 天内回复
-
ciphers/base58.py is a copy of base64_cipher.py — no Base58 implementation exists in the file可能已有人在做 @SakshiYeole 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 45/100
TheAlgorithms/Python#15637 ·
维护者通常 2 天内回复
查看 TheAlgorithms/Python 的全部 Issue
相似的 Issue
-
enhancement help wanted
难度 2/5 1-3 小时 新手友好度 76/100
Git-Hospital/githealthcheck#61 ·
维护者通常 1 天内回复
-
changelog investigate
难度 2/5 1-3 小时 新手友好度 67/100
ramnes/notion-sdk-py#433 ·
-
Partial method descriptors raise on class access可能已有人在做 @Gambit-Checkmate 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 19 天内回复
-
docs pkg:core
难度 1/5 1 小时以内 新手友好度 84/100
pydantic/pydantic-ai#11929 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
intake/intake-esm#789 ·