{"thread":{"id":"6348","subject":"Fix up totally buggered read_or_die()","startedAt":"2007-01-12T04:37:38Z","lastAt":"2007-01-12T04:37:38Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"31542","messageId":"Pine.LNX.4.64.0701112033490.3594@woody.osdl.org","threadId":"6348","inReplyTo":null,"subject":"Fix up totally buggered read_or_die()","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2007-01-12T04:37:38Z","receivedAt":"2007-01-12T04:37:38Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nThe \"read_or_die()\" function would silently NOT die for a partial read, \nand since it was of type \"void\" it obviously couldn't even return the \npartial number of bytes read.\n\nIOW, it was totally broken. This hopefully fixes it up.\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nI'm really quite fed up with the \"cleanups\" that were just too buggy for \nwords.\n\ndiff --git a/write_or_die.c b/write_or_die.c\nindex 1224cac..4e8183e 100644\n--- a/write_or_die.c\n+++ b/write_or_die.c\n@@ -4,16 +4,11 @@ int read_in_full(int fd, void *buf, size_t count)\n {\n \tchar *p = buf;\n \tssize_t total = 0;\n-\tssize_t loaded = 0;\n \n \twhile (count > 0) {\n-\t\tloaded = xread(fd, p, count);\n-\t\tif (loaded <= 0) {\n-\t\t\tif (total)\n-\t\t\t\treturn total;\n-\t\t\telse\n-\t\t\t\treturn loaded;\n-\t\t}\n+\t\tssize_t loaded = xread(fd, p, count);\n+\t\tif (loaded <= 0)\n+\t\t\treturn total ? total : loaded;\n \t\tcount -= loaded;\n \t\tp += loaded;\n \t\ttotal += loaded;\n@@ -26,13 +21,12 @@ void read_or_die(int fd, void *buf, size_t count)\n {\n \tssize_t loaded;\n \n-\tif (!count)\n-\t\treturn;\n \tloaded = read_in_full(fd, buf, count);\n-\tif (loaded == 0)\n-\t\tdie(\"unexpected end of file\");\n-\telse if (loaded < 0)\n-\t\tdie(\"read error (%s)\", strerror(errno));\n+\tif (loaded != count) {\n+\t\tif (loaded < 0)\n+\t\t\tdie(\"read error (%s)\", strerror(errno));\n+\t\tdie(\"read error: end of file\");\n+\t}\n }\n \n int write_in_full(int fd, const void *buf, size_t count)\n"}]}