Skip to content

Potential memory leak in addslashes/stripslashes #12508

@oleg-andreyev

Description

@oleg-andreyev

Description

The following code:

No leak

<?php

function encode() {
    return addslashes('stdClass');
}

function decode(string $str) {
    return stripslashes($str);
}

for ($ctr = 0; $ctr < 1_000_000; $ctr++) {
    $php = encode();
    decode($php);
}

Resulted in this output:
https://gist.github.com/oleg-andreyev/0cb275e5945061f00560f0fe07a7e068

Potential memory leak

function encode() {
    return addslashes('\stdClass');
}

function decode(string $str) {
    return stripslashes($str);
}

for ($ctr = 0; $ctr < 1_000_000; $ctr++) {
    $php = encode();
    decode($php);
}

Resulted in this output:
https://gist.github.com/oleg-andreyev/bd85f9441e1b12e603a8592553d6ec69

image

As you can see (first example):
encode allocated 0 and free 0
decode allocated 38.1Mb and free 38.1Mb

second example:
encode allocated 53.4Mb and free 0
decode allocated 38.1Mb and free 38.1Mb

PHP Version

8.1.22

Operating System

Alpine Linux v3.18


UPD
same but with extra flag SPX_BUILTINS=1
image
image

it looks like both addslashes/stripslashes are not freeing memory.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions