diff options
| author | Ryan <fauxpark@gmail.com> | 2023-03-01 12:51:18 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-01 01:51:18 +0000 |
| commit | 9b09e7c6d71b3f8eab71cdc9d1c30a6873d08dc9 (patch) | |
| tree | 5a52243cd84172f0d035d0fa01279447e64067a9 /lib/python/qmk | |
| parent | f1894e4bac7d61d3074484d87f872edc81c6d475 (diff) | |
Check all rows have the correct number of columns when parsing `g_led_config` (#19954)
Diffstat (limited to 'lib/python/qmk')
| -rw-r--r-- | lib/python/qmk/c_parse.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index 3d73e66091..560e5d3264 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py | |||
| @@ -211,10 +211,13 @@ def _coerce_led_token(_type, value): | |||
| 211 | return value_map[value] | 211 | return value_map[value] |
| 212 | 212 | ||
| 213 | 213 | ||
| 214 | def _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position_raw, flags): | 214 | def _validate_led_config(matrix, matrix_rows, matrix_cols, matrix_indexes, position, position_raw, flags): |
| 215 | # TODO: Improve crude parsing/validation | 215 | # TODO: Improve crude parsing/validation |
| 216 | if len(matrix) != matrix_rows and len(matrix) != (matrix_rows / 2): | 216 | if len(matrix) != matrix_rows and len(matrix) != (matrix_rows / 2): |
| 217 | raise ValueError("Unable to parse g_led_config matrix data") | 217 | raise ValueError("Unable to parse g_led_config matrix data") |
| 218 | for index, row in enumerate(matrix): | ||
| 219 | if len(row) != matrix_cols: | ||
| 220 | raise ValueError(f"Number of columns in row {index} ({len(row)}) does not match matrix ({matrix_cols})") | ||
| 218 | if len(position) != len(flags): | 221 | if len(position) != len(flags): |
| 219 | raise ValueError(f"Number of g_led_config physical positions ({len(position)}) does not match number of flags ({len(flags)})") | 222 | raise ValueError(f"Number of g_led_config physical positions ({len(position)}) does not match number of flags ({len(flags)})") |
| 220 | if len(matrix_indexes) and (max(matrix_indexes) >= len(flags)): | 223 | if len(matrix_indexes) and (max(matrix_indexes) >= len(flags)): |
| @@ -228,13 +231,16 @@ def _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position | |||
| 228 | def _parse_led_config(file, matrix_cols, matrix_rows): | 231 | def _parse_led_config(file, matrix_cols, matrix_rows): |
| 229 | """Return any 'raw' led/rgb matrix config | 232 | """Return any 'raw' led/rgb matrix config |
| 230 | """ | 233 | """ |
| 231 | matrix_raw = [] | 234 | matrix = [] |
| 232 | position_raw = [] | 235 | position_raw = [] |
| 233 | flags = [] | 236 | flags = [] |
| 234 | 237 | ||
| 235 | found_led_config = False | 238 | found_led_config = False |
| 236 | bracket_count = 0 | 239 | bracket_count = 0 |
| 237 | section = 0 | 240 | section = 0 |
| 241 | current_row_index = 0 | ||
| 242 | current_row = [] | ||
| 243 | |||
| 238 | for _type, value in lex(_preprocess_c_file(file), CLexer()): | 244 | for _type, value in lex(_preprocess_c_file(file), CLexer()): |
| 239 | # Assume g_led_config..stuff..; | 245 | # Assume g_led_config..stuff..; |
| 240 | if value == 'g_led_config': | 246 | if value == 'g_led_config': |
| @@ -248,12 +254,16 @@ def _parse_led_config(file, matrix_cols, matrix_rows): | |||
| 248 | if bracket_count == 2: | 254 | if bracket_count == 2: |
| 249 | section += 1 | 255 | section += 1 |
| 250 | elif value == '}': | 256 | elif value == '}': |
| 257 | if section == 1 and bracket_count == 3: | ||
| 258 | matrix.append(current_row) | ||
| 259 | current_row = [] | ||
| 260 | current_row_index += 1 | ||
| 251 | bracket_count -= 1 | 261 | bracket_count -= 1 |
| 252 | else: | 262 | else: |
| 253 | # Assume any non whitespace value here is important enough to stash | 263 | # Assume any non whitespace value here is important enough to stash |
| 254 | if _type in [Token.Literal.Number.Integer, Token.Literal.Number.Float, Token.Literal.Number.Hex, Token.Name]: | 264 | if _type in [Token.Literal.Number.Integer, Token.Literal.Number.Float, Token.Literal.Number.Hex, Token.Name]: |
| 255 | if section == 1 and bracket_count == 3: | 265 | if section == 1 and bracket_count == 3: |
| 256 | matrix_raw.append(_coerce_led_token(_type, value)) | 266 | current_row.append(_coerce_led_token(_type, value)) |
| 257 | if section == 2 and bracket_count == 3: | 267 | if section == 2 and bracket_count == 3: |
| 258 | position_raw.append(_coerce_led_token(_type, value)) | 268 | position_raw.append(_coerce_led_token(_type, value)) |
| 259 | if section == 3 and bracket_count == 2: | 269 | if section == 3 and bracket_count == 2: |
| @@ -263,16 +273,15 @@ def _parse_led_config(file, matrix_cols, matrix_rows): | |||
| 263 | return None | 273 | return None |
| 264 | 274 | ||
| 265 | # Slightly better intrim format | 275 | # Slightly better intrim format |
| 266 | matrix = list(_get_chunks(matrix_raw, matrix_cols)) | ||
| 267 | position = list(_get_chunks(position_raw, 2)) | 276 | position = list(_get_chunks(position_raw, 2)) |
| 268 | matrix_indexes = list(filter(lambda x: x is not None, matrix_raw)) | 277 | matrix_indexes = list(filter(lambda x: x is not None, sum(matrix, []))) |
| 269 | 278 | ||
| 270 | # If we have not found anything - bail with no error | 279 | # If we have not found anything - bail with no error |
| 271 | if not section: | 280 | if not section: |
| 272 | return None | 281 | return None |
| 273 | 282 | ||
| 274 | # Throw any validation errors | 283 | # Throw any validation errors |
| 275 | _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position_raw, flags) | 284 | _validate_led_config(matrix, matrix_rows, matrix_cols, matrix_indexes, position, position_raw, flags) |
| 276 | 285 | ||
| 277 | return (matrix, position, flags) | 286 | return (matrix, position, flags) |
| 278 | 287 | ||
