<commit_before>#include "openrasp.h"
extern "C" {
#include "php_ini.h"
#include "ext/standard/file.h"
#include "ext/standard/php_string.h"
#include "Zend/zend_builtin_functions.h"
}
#include <string>

void format_debug_backtrace_str(zval *backtrace_str TSRMLS_DC)
{
    zval trace_arr;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION <= 3)
    zend_fetch_debug_backtrace(&trace_arr, 0, 0 TSRMLS_CC);
#else
    zend_fetch_debug_backtrace(&trace_arr, 0, 0, 0 TSRMLS_CC);
#endif
    if (Z_TYPE(trace_arr) == IS_ARRAY)
    {
        std::string buffer;
        HashTable *hash_arr = Z_ARRVAL(trace_arr);
        for (zend_hash_internal_pointer_reset(hash_arr);
             zend_hash_has_more_elements(hash_arr) == SUCCESS;
             zend_hash_move_forward(hash_arr))
        {
            zval **ele_value;
            if (zend_hash_get_current_data(hash_arr, (void **)&ele_value) != SUCCESS ||
                Z_TYPE_PP(ele_value) != IS_ARRAY)
            {
                continue;
            }
            zval **trace_ele;
            if (zend_hash_find(Z_ARRVAL_PP(ele_value), ZEND_STRS("file"), (void **)&trace_ele) == SUCCESS &&
                Z_TYPE_PP(trace_ele) == IS_STRING)
            {
                buffer.append(Z_STRVAL_PP(trace_ele), Z_STRLEN_PP(trace_ele));
            }
            if (zend_hash_find(Z_ARRVAL_PP(ele_value), ZEND_STRS("function"), (void **)&trace_ele) == SUCCESS &&
                Z_TYPE_PP(trace_ele) == IS_STRING)
            {
                buffer.push_back('(');
                buffer.append(Z_STRVAL_PP(trace_ele), Z_STRLEN_PP(trace_ele));
            }
            //line number
            if (zend_hash_find(Z_ARRVAL_PP(ele_value), ZEND_STRS("line"), (void **)&trace_ele) == SUCCESS &&
                Z_TYPE_PP(trace_ele) == IS_LONG)
            {
                buffer.push_back(':');
                buffer.append(std::to_string(Z_LVAL_PP(trace_ele)));
                buffer.push_back(')');
            }
            buffer.push_back('\n');
        }
        ZVAL_STRINGL(backtrace_str, buffer.c_str(), buffer.length(), 1);
    }
    zval_dtor(&trace_arr);
}<commit_msg>[PHP]stack trace format<commit_after>#include "openrasp.h"
extern "C" {
#include "php_ini.h"
#include "ext/standard/file.h"
#include "ext/standard/php_string.h"
#include "Zend/zend_builtin_functions.h"
}
#include <string>

void format_debug_backtrace_str(zval *backtrace_str TSRMLS_DC)
{
    zval trace_arr;
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION <= 3)
    zend_fetch_debug_backtrace(&trace_arr, 0, 0 TSRMLS_CC);
#else
    zend_fetch_debug_backtrace(&trace_arr, 0, 0, 0 TSRMLS_CC);
#endif
    if (Z_TYPE(trace_arr) == IS_ARRAY)
    {
        std::string buffer;
        HashTable *hash_arr = Z_ARRVAL(trace_arr);
        for (zend_hash_internal_pointer_reset(hash_arr);
             zend_hash_has_more_elements(hash_arr) == SUCCESS;
             zend_hash_move_forward(hash_arr))
        {
            zval **ele_value;
            if (zend_hash_get_current_data(hash_arr, (void **)&ele_value) != SUCCESS ||
                Z_TYPE_PP(ele_value) != IS_ARRAY)
            {
                continue;
            }
            zval **trace_ele;
            if (zend_hash_find(Z_ARRVAL_PP(ele_value), ZEND_STRS("file"), (void **)&trace_ele) == SUCCESS &&
                Z_TYPE_PP(trace_ele) == IS_STRING)
            {
                buffer.append(Z_STRVAL_PP(trace_ele), Z_STRLEN_PP(trace_ele));
            }
            buffer.push_back('(');
            if (zend_hash_find(Z_ARRVAL_PP(ele_value), ZEND_STRS("function"), (void **)&trace_ele) == SUCCESS &&
                Z_TYPE_PP(trace_ele) == IS_STRING)
            {
                buffer.append(Z_STRVAL_PP(trace_ele), Z_STRLEN_PP(trace_ele));
            }
            buffer.push_back(':');
            //line number
            if (zend_hash_find(Z_ARRVAL_PP(ele_value), ZEND_STRS("line"), (void **)&trace_ele) == SUCCESS &&
                Z_TYPE_PP(trace_ele) == IS_LONG)
            {
                buffer.append(std::to_string(Z_LVAL_PP(trace_ele)));
            }
            else
            {
                buffer.append("-1");
            }
            buffer.append(")\n");
        }
        ZVAL_STRINGL(backtrace_str, buffer.c_str(), buffer.length(), 1);
    }
    zval_dtor(&trace_arr);
}
{'fraction_non_alphanumeric': 0.13065676491065212, 'fraction_numerical': 0.003945230912044558, 'mean_word_length': 2.343677269200931, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7457344', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1511, 'n_words': 205}