Skip to content
This repository was archived by the owner on May 24, 2025. It is now read-only.

Commit bf75d3f

Browse files
author
SamyAB
committed
Merge branch 'fix-crash-dimacs-reader-when-it-should'
2 parents 4a5fad2 + d2b7b6e commit bf75d3f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.2] - 2020-06-30
8+
### Fixed
9+
- Ensure the DIMACS file reader crashes when it reads a literal number higher than the number of literals
10+
711
## [1.0.1] - 2020-06-27
812
### Fixed
913
- Reader of DIMACS CNF formula format to handle toughsat generated formulae

cugen/sat_problem_modelling/dimacs_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def transform_dimacs_clause_to_cugen_clause(dimacs_clause: str, number_of_litera
1616

1717
for literal in signed_literals:
1818
literal_as_integer = int(literal)
19-
literal_as_index = cupy.absolute(literal_as_integer) - 1
19+
literal_as_index = int(cupy.absolute(literal_as_integer) - 1)
2020

2121
if cupy.isnan(cugen_clause[literal_as_index]):
2222
cugen_clause[literal_as_index] = 0 if literal_as_integer < 0 else 1

tests/test_sat_problem_modelling/test_dimacs_reader.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import patch, call
22

33
import cupy
4+
import pytest
45

56
from cugen.sat_problem_modelling.dimacs_reader import transform_dimacs_clause_to_cugen_clause, read_dimacs_file
67

@@ -49,6 +50,17 @@ def test_transform_dimacs_clause_to_cugen_clause_transforms_contradicting_litera
4950
cupy.testing.assert_array_equal(cugen_clause_with_nans, expected_cugen_clause_with_nans)
5051

5152

53+
def test_transform_dimacs_clause_to_cugen_clause_raises_exception_when_literal_is_out_of_bounds():
54+
# Given
55+
clause_with_literal_out_of_bounds = '42 -246 528 0\n'
56+
number_of_literals = 527
57+
58+
# Then
59+
with pytest.raises(IndexError):
60+
# When
61+
_ = transform_dimacs_clause_to_cugen_clause(clause_with_literal_out_of_bounds, number_of_literals)
62+
63+
5264
@patch(f'{TESTED_MODULE}.transform_dimacs_clause_to_cugen_clause', return_value=cupy.array([1, 2, 3]))
5365
def test_read_dimacs_file_finds_the_right_number_of_literals_and_loops_over_clauses_to_transform_them(
5466
mock_transform_clause, tmp_path

0 commit comments

Comments
 (0)