#Option StringLimitCheck

Syntax:

    #option StringLimitCheck

Explanation:

Assigning a literal string, or copying one string into another, longer than a fixed-length string’s declared * Length compiles without warning (see Dim) - the extra characters overrun whatever RAM comes after the string at runtime. #option StringLimitCheck closes this: every string assignment is clamped to the destination’s declared length, so an over-long source is silently truncated instead of overwriting adjacent variables.

The check applies to the library routines every string assignment already goes through (SysReadString for a literal/table source, SysCopyString for copying one variable into another) - no change is needed to existing code beyond adding the option. The destination’s declared length is exported as an assembler constant, <name>_STRING_LENGTH, and loaded before each call; a destination with no single fixed length (a string passed by reference, an array element) is not limited (255, effectively unlimited).

Supported on PIC (12-bit, 14-bit, enhanced mid-range, PIC18) and AVR. On any other target, the option is ignored with a warning (WarningStringLimitCheck).

Example:

    #chip 16F877A, 20
    #option StringLimitCheck

    Dim Greeting As String * 5
    Greeting = "Hello, World!"     'Without this option: overruns Greeting's 5-character buffer.
                                    'With it: Greeting is truncated to "Hello" instead.
    Do
    Loop

See Also:

  • Dim — declaring a fixed-length string with As String * Length
  • UBound — for finding a string’s declared length at runtime