Skip to content

Commit

Permalink
Bound integers written SQL
Browse files Browse the repository at this point in the history
Summary: Add more sanity checking of numbers we are writing to SQL to fend of strict SQL failures.

Differential Revision: D64134639

fbshipit-source-id: 9c387a48c489652904c436742968842d08495a3e
  • Loading branch information
Manuel Fahndrich authored and facebook-github-bot committed Oct 10, 2024
1 parent 44c9463 commit 45e8317
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions sapp/pipeline/model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import json
import logging
from collections import defaultdict
from typing import Dict, Iterable, List, Optional, Set, Tuple, Union
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union

from ..models import (
DBID,
Expand Down Expand Up @@ -51,6 +51,36 @@
log: logging.Logger = logging.getLogger("sapp")


# pyre-fixme[2]: Really need Any here
def bound_int(i: Any, upper_limit: int) -> Optional[int]:
if isinstance(i, int):
if i < 0:
return 0
elif i > upper_limit:
return upper_limit
return i
else:
return None


MAX_TRACE_LENGTH: int = 1000


# pyre-fixme[2]: Really need Any here
def bound_trace_length(depth: Any) -> Optional[int]:
# avoid writing bad trace_length to DB
return bound_int(depth, MAX_TRACE_LENGTH)


MAX_TYPE_INTERVAL: int = (2**31) - 1


# pyre-fixme[2]: Really need Any here
def bound_type_interval_limit(i: Any) -> Optional[int]:
# avoid writing bad interval numbers
return bound_int(i, MAX_TYPE_INTERVAL)


class ModelGenerator(PipelineStep[DictEntries, TraceGraph]):
def __init__(
self,
Expand Down Expand Up @@ -144,7 +174,8 @@ def _get_minimum_trace_length(
length = None
for entry in entries:
for _leaf, depth in entry.leaves:
if length is None or length > depth:
depth = bound_trace_length(depth)
if length is None or (depth is not None and length > depth):
length = depth
if length is not None:
return length
Expand Down Expand Up @@ -449,13 +480,7 @@ def _generate_raw_trace_frame(
leaf_mapping_ids: Set[LeafMapping] = set()
for leaf, depth in leaves:
# avoid writing bad trace_length to DB
if isinstance(depth, int):
if depth < 0:
depth = 0
elif depth > 1000:
depth = 1000
else:
depth = None
depth = bound_trace_length(depth)
leaf_record = self._get_shared_text(leaf_kind, leaf)
caller_leaf_id = self.graph.get_transform_normalized_caller_kind_id(
leaf_record
Expand Down Expand Up @@ -543,7 +568,9 @@ def _get_interval(
self, ti: Optional[ParseTypeInterval]
) -> Tuple[Optional[int], Optional[int], bool]:
if ti:
return (ti.start, ti.finish, ti.preserves_type_context)
lower = bound_type_interval_limit(ti.start)
upper = bound_type_interval_limit(ti.finish)
return (lower, upper, ti.preserves_type_context)
else:
return (None, None, False)

Expand Down

0 comments on commit 45e8317

Please sign in to comment.