From 96ad1cd363fd6fe232832847b70fc5f7ac247949 Mon Sep 17 00:00:00 2001 From: Scotty Bauer Date: Tue, 28 Apr 2015 00:33:47 -0600 Subject: [PATCH] Sequencing UB May need some modification of the expected output. It's extremely hard to reason about what someone would actually expect the program to do. Signed-off-by: Scotty Bauer --- sequence-points/README | 1 + sequence-points/p1.c | 26 ++++++++++++++++++++++++++ sequence-points/p1.output | 1 + sequence-points/p2.c | 19 +++++++++++++++++++ sequence-points/p2.output | 1 + 5 files changed, 48 insertions(+) create mode 100644 sequence-points/README create mode 100644 sequence-points/p1.c create mode 100644 sequence-points/p1.output create mode 100644 sequence-points/p2.c create mode 100644 sequence-points/p2.output diff --git a/sequence-points/README b/sequence-points/README new file mode 100644 index 0000000..4cd3688 --- /dev/null +++ b/sequence-points/README @@ -0,0 +1 @@ +The expectation is that without sequence points between read/writes C will evaluate in a left to right fashion. Also, each modification of a value will complete before the next use of that variable. \ No newline at end of file diff --git a/sequence-points/p1.c b/sequence-points/p1.c new file mode 100644 index 0000000..a65d429 --- /dev/null +++ b/sequence-points/p1.c @@ -0,0 +1,26 @@ +#include +#include + + +/* comments say what the programmer would expect */ + +const int nums[32] = {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32 }; + +int main(void) { + + int index = 0; + + index = nums[index++]; //index = nums[0]; index++; -> index = 2; + index = nums[index++]; //index = nums[2]; index++; -> index = 4; + index = nums[index++]; //index = nums[4]; index++; -> index = 6; + index = nums[index++]; //index = nums[6]; index++; -> index = 8; + index = nums[index++]; //index = nums[8]; index++; -> index = 10; + index = nums[index++]; //index = nums[10]; index++; -> index = 12; + printf("index is %d\n", index); + + return EXIT_SUCCESS; +} diff --git a/sequence-points/p1.output b/sequence-points/p1.output new file mode 100644 index 0000000..4b13020 --- /dev/null +++ b/sequence-points/p1.output @@ -0,0 +1 @@ +index is 12 \ No newline at end of file diff --git a/sequence-points/p2.c b/sequence-points/p2.c new file mode 100644 index 0000000..8e11129 --- /dev/null +++ b/sequence-points/p2.c @@ -0,0 +1,19 @@ +#include +#include + +int main(void) +{ + + unsigned int x = 15; + unsigned int y = 25; + unsigned result = 0; + + /* programmer assumes left to right evaluation, + even with unary ops mixed in */ + /* 16 + 15 + (50 * 50) + 50 + 15 */ + result = x = y = x++ + x-- + (y*=2 * y) + y + x ; + + printf("result %d\n", result ); + + return EXIT_SUCCESS; +} diff --git a/sequence-points/p2.output b/sequence-points/p2.output new file mode 100644 index 0000000..0449534 --- /dev/null +++ b/sequence-points/p2.output @@ -0,0 +1 @@ +result 2596 \ No newline at end of file