Skip to content
GitLab
    • GitLab: the DevOps platform
    • Explore GitLab
    • Install GitLab
    • How GitLab compares
    • Get started
    • GitLab docs
    • GitLab Learn
  • Pricing
  • Talk to an expert
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
    Projects Groups Snippets
  • Sign up now
  • Login
  • Sign in / Register
  • FPC Source FPC Source
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Graph
    • Compare
    • Locked Files
  • Issues 1,357
    • Issues 1,357
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 73
    • Merge requests 73
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • FPC
  • FPCFPC
  • FPC SourceFPC Source
  • Issues
  • #39944
Closed
Open
Issue created Oct 08, 2022 by ccrause@ccrauseContributor

Potential buffer overflow when using shortstring var/out parameters

Summary

A discussion regarding shortstrings revealed a shortcoming of the current implementation, where shortstring parameters are assumed to be 255 bytes in length, regardless of the actual declaration of the parameter being passed in. This can lead to buffer overflows, as illustrated below:

System Information

  • Operating system: Embedded, Linux Mint
  • Processor architecture: AVR, x86-64
  • Compiler version: main 569f83e3
  • Device: microcontroller, PC

Steps to reproduce

Pass a shortstring with size smaller than 255 bytes to subroutines that writes more data to the parameter that the original variable's size. See example project.

Example Project

program project1;

var
  s1: string[4] = 'wxyz';
  s2: string[2] = '??';

procedure setStr(out s: shortstring);
begin
  s := '1234'#09'SURPRISE!';
end;

begin
  setStr(s1);
  writeln(s1);
  writeln(s2);
end.

What is the current bug behavior?

Actual output (Linux-64 and AVR):

1234 SURPRISE!
SURPRISE!

What is the expected (correct) behavior?

Expected output:

1234
??

Relevant logs and/or screenshots

Possible fixes

Sven suggested passing the shortstring size as an extra parameter to var (and I assume out) parameters. This comment was for a specific situation (eliminating a temporary variable during string concatenation), but sounds like a general solution to the problem.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking