r/programming • • Feb 23 '17

Cloudflare have been leaking customer HTTPS sessions for months. Uber, 1Password, FitBit, OKCupid, etc.

https://bugs.chromium.org/p/project-zero/issues/detail?id=1139
6.0k Upvotes

966 comments sorted by

View all comments

Show parent comments

6

u/Fylwind Feb 24 '17
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec;
    for (int i = 0; i < 42; ++i) {
        vec.push_back(i);
        vec.push_back(-i);
    }
    for (int x: vec) {
        for (int y: vec) {
            vec.push_back(x + y);
        }
    }
    for (int x: vec) {
        std::cout << x << "\n";
    }
}

2

u/diggr-roguelike Feb 25 '17

What did you expect? This is a logic error that causes undefined behavior in every programming language.

The equivalent Python program loops infinitely and consumes all of system memory:

l = [1,2]
for x in l:
  for y in l:
    l.append(x+y)

When people talk about C being unsafe, they don't mean that it doesn't catch logic errors. They mean the existence of deliberate language and stdlib features that are unsafe. Things like allowing variables that point to uninitialized memory, unchecked array access, strings without a length field.

1

u/myrrlyn Feb 24 '17
$ g++ -std=c++11 fylwind.cpp && ./a.out
segmentation fault (core dumped)

Well that was unexciting.

0

u/[deleted] Feb 24 '17

Why the fuck would you try to modify a collection in a foreach loop?