复制代码

为懒人提供无限可能,生命不息,code不止

人类感性的情绪,让我们知难行难
我思故我在
日拱一卒,功不唐捐
  • 首页
  • 前端
  • 后台
  • 数据库
  • 运维
  • 资源下载
  • 实用工具
  • 接口文档工具
  • 登录
  • 注册

其它

【原创】java 堆栈模拟溢出

作者: whooyun发表于: 2025-03-04 23:55

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class PerformanceTrapApplication {
    public static void main(String[] args) {
        SpringApplication.run(PerformanceTrapApplication.class, args);
    }
}

@RestController
class TrapController {
    private final Map<String, String> cache = new HashMap<>();

    @GetMapping("/trap")
    public String trap(@RequestParam String input) {
        try {
            String result = process(input, new HashMap<>());
            return "Processed: " + result;
        } catch (StackOverflowError e) {
            return "Application Crashed: Stack Overflow";
        }
    }

    private String process(String input, Map<String, String> context) {
        if (input.length() > 10) {
            if (!cache.containsKey(input)) {
                cache.put(input, process(input.substring(1), context));
            }
            return cache.get(input);
        }
        return input;
    }
}