Package: opentyrian / 2.1.20130907+dfsg-3

check-return-values.patch Patch series | download
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Subject: Check return values
Author: Etienne Millon <me@emillon.org>
Last-Update: 2015-01-05
Forwarded: https://bitbucket.org/opentyrian/opentyrian/pull-request/3

  - Make efread/efwrite check their return values.
    This can be done with an assert() since the actual return value is not used.

  - Patch a call to fread.

--- a/src/file.c
+++ b/src/file.c
@@ -21,6 +21,7 @@
 
 #include "SDL.h"
 #include <errno.h>
+#include <assert.h>
 
 const char *custom_data_dir = ".";
 
@@ -123,12 +124,12 @@
 	return size;
 }
 
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 // endian-swapping fread
-size_t efread( void *buffer, size_t size, size_t num, FILE *stream )
+void efread( void *buffer, size_t size, size_t num, FILE *stream )
 {
 	size_t f = fread(buffer, size, num, stream);
 	
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 	switch (size)
 	{
 		case 2:
@@ -146,15 +147,17 @@
 		default:
 			break;
 	}
-	
-	return f;
+#endif
+
+	assert(f == num);
 }
 
 // endian-swapping fwrite
-size_t efwrite( void *buffer, size_t size, size_t num, FILE *stream )
+void efwrite( void *buffer, size_t size, size_t num, FILE *stream )
 {
 	void *swap_buffer;
 	
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 	switch (size)
 	{
 		case 2:
@@ -176,12 +179,16 @@
 			swap_buffer = buffer;
 			break;
 	}
+#else
+	swap_buffer = buffer;
+#endif
 	
 	size_t f = fwrite(swap_buffer, size, num, stream);
 	
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
 	if (swap_buffer != buffer)
 		free(swap_buffer);
+#endif
 	
-	return f;
+	assert(f == num);
 }
-#endif
--- a/src/file.h
+++ b/src/file.h
@@ -40,13 +40,8 @@
 long ftell_eof( FILE *f );
 
 // endian-swapping fread/fwrite
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-size_t efread( void *buffer, size_t size, size_t num, FILE *stream );
-size_t efwrite( void *buffer, size_t size, size_t num, FILE *stream );
-#else
-#define efread fread
-#define efwrite fwrite
-#endif
+void efread( void *buffer, size_t size, size_t num, FILE *stream );
+void efwrite( void *buffer, size_t size, size_t num, FILE *stream );
 
 #endif // FILE_H
 
--- a/src/config.c
+++ b/src/config.c
@@ -223,7 +223,8 @@
 	size_t buffer_len = ftell_eof(f);
 	char *buffer = malloc(buffer_len + 1);
 	
-	fread(buffer, 1, buffer_len, f);
+	size_t res = fread(buffer, 1, buffer_len, f);
+	assert(res == buffer_len);
 	buffer[buffer_len] = '\0';
 	
 	fclose(f);