Skip to content

[item 85] 코드 85-1에서 Set에 같은 값을 반복해서 넣는 이유 #122

@seonjuleee

Description

@seonjuleee

451pg, 이런 스트림을 역직렬화 폭탄(deserialization bomb)이라고 한다. ~

질문 내용

public static byte[] bomb() {
    Set<Object> root = new HashSet<>();
    Set<Object> s1 = root;
    Set<Object> s2 = new HashSet<>();
    for (int i = 0; i < 100; i++) {
        Set<Object> t1 = new HashSet<>();
        Set<Object> t2 = new HashSet<>();
        t1.add("foo"); // t1을 t2와 다르게 만든다.
        s1.add(t1); s1.add(t2);
        s2.add(t1); s2.add(t2);
        s1 = t1;
        s2 = t2;
    }
    return serialize(root);
}
s1 = t1;
s2 = t2;

여기서 반복문을 돌아도 s1과 s2에는 계속 같은 값이 들어갈텐데 무슨 이유로 이렇게 하는 것인지 잘 모르겠다.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions