diff options
| author | Rahul Yadav <yadavrah@google.com> | 2026-06-29 09:27:57 +0000 |
|---|---|---|
| committer | gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2026-06-30 04:44:38 -0700 |
| commit | a27dbcdb7bba1b8a2cc84112fa31c8f9f818604e (patch) | |
| tree | f68592217a0d606d0cdc418efa2d7c825c80df6d | |
| parent | 547dc9985ca6d401284b7ee0018721e8aa3324d3 (diff) | |
| download | git-repo-a27dbcdb7bba1b8a2cc84112fa31c8f9f818604e.tar.gz | |
git_trace2_event_log: Fix index out of range on empty config values
In git_trace2_event_log_base.py's GetDataEventName method, it parses
value to identify if it represents a JSON list. When a config key has an
empty string value, GetDataEventName evaluates value[0], which raises
IndexError: string index out of range.
This change fixes the crash by checking if the value is a string and using
startswith/endswith to check for JSON lists instead of direct indexing.
Test: PYTHONPATH=. pytest tests/test_git_trace2_event_log.py
Bug: 512518342
TAG=agy
CONV=ff5d70d7-e5b3-42b3-8f16-23b9e3070754
Change-Id: Ic40a8c6a22df57d0e97f268f6e1bc8a14a5024a4
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/602201
Reviewed-by: Gavin Mak <gavinmak@google.com>
Tested-by: Rahul Yadav <yadavrah@google.com>
Commit-Queue: Rahul Yadav <yadavrah@google.com>
| -rw-r--r-- | git_trace2_event_log_base.py | 8 | ||||
| -rw-r--r-- | tests/test_git_trace2_event_log.py | 3 |
2 files changed, 9 insertions, 2 deletions
diff --git a/git_trace2_event_log_base.py b/git_trace2_event_log_base.py index 1a59fee8a..063d101c6 100644 --- a/git_trace2_event_log_base.py +++ b/git_trace2_event_log_base.py | |||
| @@ -195,7 +195,13 @@ class BaseEventLog: | |||
| 195 | 195 | ||
| 196 | def GetDataEventName(self, value): | 196 | def GetDataEventName(self, value): |
| 197 | """Returns 'data-json' if the value is an array else returns 'data'.""" | 197 | """Returns 'data-json' if the value is an array else returns 'data'.""" |
| 198 | return "data-json" if value[0] == "[" and value[-1] == "]" else "data" | 198 | return ( |
| 199 | "data-json" | ||
| 200 | if isinstance(value, str) | ||
| 201 | and value.startswith("[") | ||
| 202 | and value.endswith("]") | ||
| 203 | else "data" | ||
| 204 | ) | ||
| 199 | 205 | ||
| 200 | def LogDataConfigEvents(self, config, prefix): | 206 | def LogDataConfigEvents(self, config, prefix): |
| 201 | """Append a 'data' event for each entry in |config| to the current log. | 207 | """Append a 'data' event for each entry in |config| to the current log. |
diff --git a/tests/test_git_trace2_event_log.py b/tests/test_git_trace2_event_log.py index 9a6ba2052..13b64df2d 100644 --- a/tests/test_git_trace2_event_log.py +++ b/tests/test_git_trace2_event_log.py | |||
| @@ -315,6 +315,7 @@ def test_data_event_config(event_log: git_trace2_event_log.EventLog) -> None: | |||
| 315 | "repo.partialclone": "false", | 315 | "repo.partialclone": "false", |
| 316 | "repo.syncstate.superproject.hassuperprojecttag": "true", | 316 | "repo.syncstate.superproject.hassuperprojecttag": "true", |
| 317 | "repo.syncstate.superproject.sys.argv": ["--", "sync", "protobuf"], | 317 | "repo.syncstate.superproject.sys.argv": ["--", "sync", "protobuf"], |
| 318 | "repo.syncstate.emptykey": "", | ||
| 318 | } | 319 | } |
| 319 | prefix_value = "prefix" | 320 | prefix_value = "prefix" |
| 320 | event_log.LogDataConfigEvents(config, prefix_value) | 321 | event_log.LogDataConfigEvents(config, prefix_value) |
| @@ -323,7 +324,7 @@ def test_data_event_config(event_log: git_trace2_event_log.EventLog) -> None: | |||
| 323 | log_path = event_log.Write(path=tempdir) | 324 | log_path = event_log.Write(path=tempdir) |
| 324 | log_data = read_log(log_path) | 325 | log_data = read_log(log_path) |
| 325 | 326 | ||
| 326 | assert len(log_data) == 5 | 327 | assert len(log_data) == 6 |
| 327 | data_events = log_data[1:] | 328 | data_events = log_data[1:] |
| 328 | verify_common_keys(log_data[0], expected_event_name="version") | 329 | verify_common_keys(log_data[0], expected_event_name="version") |
| 329 | 330 | ||
