Skip to content

add as oct to describe base fields#1290

Merged
antonvw merged 2 commits into
developfrom
1289-add-as-oct-to-describe_basefields
Jul 26, 2026
Merged

add as oct to describe base fields#1290
antonvw merged 2 commits into
developfrom
1289-add-as-oct-to-describe_basefields

Conversation

@antonvw

@antonvw antonvw commented Jul 26, 2026

Copy link
Copy Markdown
Owner

No description provided.

@antonvw antonvw linked an issue Jul 26, 2026 that may be closed by this pull request
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Show octal value when describing numeric base fields

✨ Enhancement 🕐 Less than 10 minutes

Grey Divider

AI Description

• Extend base field description output to include octal formatting alongside hex.
• Improve clipboard-friendly diagnostics for numeric parsing/representation.
Diagram

graph TD
  A["Input string"] --> B["describe_basefields()"] --> C["base_t::to_stream()"] --> D["Formatted output"] --> E["clipboard_add()"] --> F["Returned description"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Save/restore iostream format flags around hex/oct output
  • ➕ Prevents future formatting side effects if more numbers are appended after this output
  • ➕ Makes base_t::to_stream safer if reused in other contexts
  • ➖ Slightly more code (saving flags/precision) for a currently low-risk, end-of-stream usage
2. Use explicit base prefixes (showbase) or custom formatting helpers
  • ➕ More readable output (e.g., 0xFF, 077) and clearer diagnostics
  • ➕ Avoids reliance on stream state
  • ➖ Changes output format more broadly (may break expectations/tests)
  • ➖ More implementation churn than needed for adding octal output

Recommendation: The current approach is fine given the oct manipulator is applied at the end of the output path. If this formatting function is likely to grow or be reused, consider saving/restoring stream flags to avoid subtle future regressions caused by lingering std::oct/std::hex state.

Files changed (1) +2 / -1

Enhancement (1) +2 / -1
util.cppInclude octal representation in base field description output +2/-1

Include octal representation in base field description output

• Updates base_t::to_stream() to print the value in octal in addition to hex when emitting the non-decimal representation line. This enhances describe_basefields output that is copied to the clipboard for inspection.

src/stc/util.cpp

@qodo-code-review

qodo-code-review Bot commented Jul 26, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Informational

1. Stream flags not restored ✓ Resolved 🐞 Bug ⚙ Maintainability
Description
base_t::to_stream() leaves the caller-provided stream in std::oct mode after writing the octal
value, so any later integer output to the same std::stringstream will be octal unless reset (a
subtle formatting/correctness hazard). This is especially risky because other branches print labels
like "as dec" without explicitly forcing std::dec, relying on prior stream state.
Code

src/stc/util.cpp[R53-55]

+        str << name() << "as hex: " << std::hex << m_val
+            << " as oct: " << std::oct << m_val << "\n";
      }
Relevance

⭐⭐⭐ High

Team often accepts fixes for leaked/incorrect state; restoring stream flags prevents subtle
downstream formatting bugs.

PR-#1233
PR-#1254
PR-#1150

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The updated hex branch sets std::hex and then std::oct on the shared std::stringstream and
never resets it. The same std::stringstream is reused across multiple conversions in
describe_basefields(), so any later numeric insertions to that stream would inherit the octal
formatting state.

src/stc/util.cpp[43-56]
src/stc/util.cpp[88-119]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`base_t::to_stream()` writes using `std::hex` and now `std::oct`, but does not restore the original stream formatting flags. Because iostream manipulators are sticky, this can unexpectedly change formatting of later numeric writes to the same `std::stringstream`.

### Issue Context
`describe_basefields()` reuses one `std::stringstream` across multiple `to_stream()` calls; while the current call order happens to invoke the hex/oct path last, future edits (or additional stream writes after the call) can silently start emitting octal numbers.

### Fix Focus Areas
- src/stc/util.cpp[43-56]
- src/stc/util.cpp[88-119]

### Suggested fix
Inside `base_t::to_stream()`, save and restore the stream’s formatting state:
- Save: `auto old_flags = str.flags();`
- Optionally also save `old_fill` / `old_precision` if changed.
- Ensure decimal branch uses `std::dec` when printing "as dec".
- Restore at end of function: `str.flags(old_flags);`

Alternative: explicitly apply `std::dec` after the hex/oct write to avoid leaking octal mode.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Comment thread src/stc/util.cpp
@antonvw
antonvw merged commit 0ce145a into develop Jul 26, 2026
16 checks passed
@antonvw
antonvw deleted the 1289-add-as-oct-to-describe_basefields branch July 26, 2026 14:46
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add as oct to describe_basefields

1 participant