Repository: pydicom/pydicom

Issue:
OverflowError "VR of 'DS' must be <= 16 characters long" triggered when element is 16 characters long
**Describe the bug**

`OverflowError` triggered while accessing `PixelData`, which the values compliant with the standard. In the sample referenced in the example below, we have this, which satisfies DS VR:

```
(0028,0030) DS [.002006091181818\.002006091181818]      #  34, 2 PixelSpacing
```

But nevertheless the error is triggered while trying to access `PixelData`:

```
OverflowError: Values for elements with a VR of 'DS' must be <= 16 characters long, 
but the float provided requires > 16 characters to be accurately represented. Use a 
smaller string, set 'config.settings.reading_validation_mode' to 'WARN' to override 
the length check, or explicitly construct a DS object with 'auto_format' set to True
```

**Expected behavior**

`OverflowError` does not get triggered.

**Steps To Reproduce**

Follow the steps of this Colab notebook: https://colab.research.google.com/drive/1FcSgjBKazh0YN-jlJYdID0YUTh90CAvZ?usp=sharing

**Your environment**

```
module       | version
------       | -------
platform     | Linux-5.4.144+-x86_64-with-Ubuntu-18.04-bionic
Python       | 3.7.13 (default, Mar 16 2022, 17:37:17)  [GCC 7.5.0]
pydicom      | 2.3.0
gdcm         | _module not found_
jpeg_ls      | _module not found_
numpy        | 1.21.5
PIL          | 9.1.0
pylibjpeg    | _module not found_
openjpeg     | _module not found_
libjpeg      | _module not found_
```

Related issue: https://github.com/imi-bigpicture/wsidicom/issues/49

cc: @DanielaSchacherer @dclunie @hackermd 


Discussion:
For reference, a possibly similar issue came up in dcmjs: https://github.com/dcmjs-org/dcmjs/issues/175
I had a quick look, and the problem seems to be that the length is not taken from the original string, but from the string representation, which in this case adds a leading zero... This check has been introduced in pydicom 2.2.0.

Patch:
diff --git a/pydicom/valuerep.py b/pydicom/valuerep.py
--- a/pydicom/valuerep.py
+++ b/pydicom/valuerep.py
@@ -874,7 +874,7 @@ def __init__(
 
         if (validation_mode == config.RAISE and
                 not self.auto_format):
-            if len(repr(self)[1:-1]) > 16:
+            if len(str(self)) > 16:
                 raise OverflowError(
                     "Values for elements with a VR of 'DS' must be <= 16 "
                     "characters long, but the float provided requires > 16 "
@@ -884,7 +884,7 @@ def __init__(
                     "explicitly construct a DS object with 'auto_format' "
                     "set to True"
                 )
-            if not is_valid_ds(repr(self)[1:-1]):
+            if not is_valid_ds(str(self)):
                 # This will catch nan and inf
                 raise ValueError(
                     f'Value "{str(self)}" is not valid for elements with a VR '


Test patch:
diff --git a/pydicom/tests/test_valuerep.py b/pydicom/tests/test_valuerep.py
--- a/pydicom/tests/test_valuerep.py
+++ b/pydicom/tests/test_valuerep.py
@@ -603,6 +603,13 @@ def test_enforce_valid_values_length(self):
             valuerep.DSfloat('3.141592653589793',
                              validation_mode=config.RAISE)
 
+    def test_handle_missing_leading_zero(self):
+        """Test that no error is raised with maximum length DS string
+        without leading zero."""
+        # Regression test for #1632
+        valuerep.DSfloat(".002006091181818",
+                         validation_mode=config.RAISE)
+
     def test_DSfloat_auto_format(self):
         """Test creating a value using DSfloat copies auto_format"""
         x = DSfloat(math.pi, auto_format=True)