Commit 56c43934e569ee3e5853206e9b8ee33427aa8c48

Authored by m-holger
1 parent 772cd8d5

Refactor stream filter handling for improved validation.

Simplify and reorganize parsing logic to replace explicit filter name collection with direct filter creation. Added validation for unsupported filters, ensuring filters are cleared and errors returned when necessary. This improves code clarity and robustness.
Showing 1 changed file with 12 additions and 13 deletions
libqpdf/QPDF_Stream.cc
... ... @@ -352,15 +352,17 @@ Stream::filterable(
352 352  
353 353 auto filter_obj = s->stream_dict.getKey("/Filter");
354 354  
355   - std::vector<std::string> filter_names;
356   -
357 355 if (filter_obj.isNull()) {
358 356 // No filters
359 357 return true;
360 358 }
361 359 if (filter_obj.isName()) {
362 360 // One filter
363   - filter_names.emplace_back(filter_obj.getName());
  361 + auto ff = s->filter_factory(filter_obj.getName());
  362 + if (!ff) {
  363 + return false;
  364 + }
  365 + filters.emplace_back(ff());
364 366 } else if (filter_obj.isArray()) {
365 367 // Potentially multiple filters
366 368 int n = filter_obj.getArrayNItems();
... ... @@ -370,21 +372,18 @@ Stream::filterable(
370 372 warn("stream filter type is not name or array");
371 373 return false;
372 374 }
373   - filter_names.emplace_back(item.getName());
  375 + auto ff = s->filter_factory(item.getName());
  376 + if (!ff) {
  377 + filters.clear();
  378 + return false;
  379 + }
  380 + filters.emplace_back(ff());
374 381 }
375 382 } else {
376 383 warn("stream filter type is not name or array");
377 384 return false;
378 385 }
379 386  
380   - for (auto& filter_name: filter_names) {
381   - if (auto ff = s->filter_factory(filter_name)) {
382   - filters.emplace_back(ff());
383   - continue;
384   - }
385   - return false;
386   - }
387   -
388 387 bool filterable = true;
389 388  
390 389 // filters now contains a list of filters to be applied in order. See which ones we can support.
... ... @@ -401,7 +400,7 @@ Stream::filterable(
401 400 decode_parms.push_back(decode_obj.getArrayItem(i));
402 401 }
403 402 } else {
404   - for (unsigned int i = 0; i < filter_names.size(); ++i) {
  403 + for (unsigned int i = 0; i < filters.size(); ++i) {
405 404 decode_parms.push_back(decode_obj);
406 405 }
407 406 }
... ...