Skip to content

Commit 3f2baa8

Browse files
olafheringgregkh
authored andcommitted
Tools: hv: update buffer handling in hv_fcopy_daemon
Currently this warning is triggered when compiling hv_fcopy_daemon: hv_fcopy_daemon.c:216:4: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] kernel_modver = *(__u32 *)buffer; Convert the send/receive buffer to a union and pass individual members as needed. This also gives the correct size for the buffer. Signed-off-by: Olaf Hering <olaf@aepfle.de> Signed-off-by: K. Y. Srinivasan <kys@microsoft.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3619350 commit 3f2baa8

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

‎tools/hv/hv_fcopy_daemon.c‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,17 @@ void print_usage(char *argv[])
138138

139139
int main(int argc, char *argv[])
140140
{
141-
int fcopy_fd, len;
141+
int fcopy_fd;
142142
int error;
143143
int daemonize = 1, long_index = 0, opt;
144144
int version = FCOPY_CURRENT_VERSION;
145-
char *buffer[4096 * 2];
146-
struct hv_fcopy_hdr *in_msg;
145+
union {
146+
struct hv_fcopy_hdr hdr;
147+
struct hv_start_fcopy start;
148+
struct hv_do_fcopy copy;
149+
__u32 kernel_modver;
150+
} buffer = { };
147151
int in_handshake = 1;
148-
__u32 kernel_modver;
149152

150153
static struct option long_options[] = {
151154
{"help", no_argument, 0, 'h' },
@@ -195,32 +198,31 @@ int main(int argc, char *argv[])
195198
* In this loop we process fcopy messages after the
196199
* handshake is complete.
197200
*/
198-
len = pread(fcopy_fd, buffer, (4096 * 2), 0);
201+
ssize_t len;
202+
203+
len = pread(fcopy_fd, &buffer, sizeof(buffer), 0);
199204
if (len < 0) {
200205
syslog(LOG_ERR, "pread failed: %s", strerror(errno));
201206
exit(EXIT_FAILURE);
202207
}
203208

204209
if (in_handshake) {
205-
if (len != sizeof(kernel_modver)) {
210+
if (len != sizeof(buffer.kernel_modver)) {
206211
syslog(LOG_ERR, "invalid version negotiation");
207212
exit(EXIT_FAILURE);
208213
}
209-
kernel_modver = *(__u32 *)buffer;
210214
in_handshake = 0;
211-
syslog(LOG_INFO, "kernel module version: %d",
212-
kernel_modver);
215+
syslog(LOG_INFO, "kernel module version: %u",
216+
buffer.kernel_modver);
213217
continue;
214218
}
215219

216-
in_msg = (struct hv_fcopy_hdr *)buffer;
217-
218-
switch (in_msg->operation) {
220+
switch (buffer.hdr.operation) {
219221
case START_FILE_COPY:
220-
error = hv_start_fcopy((struct hv_start_fcopy *)in_msg);
222+
error = hv_start_fcopy(&buffer.start);
221223
break;
222224
case WRITE_TO_FILE:
223-
error = hv_copy_data((struct hv_do_fcopy *)in_msg);
225+
error = hv_copy_data(&buffer.copy);
224226
break;
225227
case COMPLETE_FCOPY:
226228
error = hv_copy_finished();
@@ -231,7 +233,7 @@ int main(int argc, char *argv[])
231233

232234
default:
233235
syslog(LOG_ERR, "Unknown operation: %d",
234-
in_msg->operation);
236+
buffer.hdr.operation);
235237

236238
}
237239

0 commit comments

Comments
 (0)