Skip to content

Commit 74857d0

Browse files
gojimmypidanielinux
authored andcommitted
Fix warnings with _MSC_VER and _MSC_VER
1 parent cb85858 commit 74857d0

File tree

2 files changed

+91
-25
lines changed

2 files changed

+91
-25
lines changed

src/delta.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <string.h>
2424
#include <delta.h>
2525

26-
2726
#define ESC 0x7f
2827

2928

@@ -174,17 +173,30 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
174173
#include <stdio.h>
175174
#include <stdlib.h>
176175
#include <errno.h>
176+
#include <limits.h> /* INT_MAX */
177+
#include <inttypes.h> /* PRIu32 */
177178

178179
static uint32_t wolfboot_sector_size = 0;
179180

180181
int wb_diff_get_sector_size(void)
181182
{
182183
uint32_t sec_sz = 0;
183184
char *env_sector_size = NULL;
185+
#if defined(_MSC_VER) /* MSVC only, not _WIN32 that includes MSYS2/MinGW */
186+
char* dup = NULL;
187+
size_t len = 0;
188+
if ((_dupenv_s(&dup, &len, "WOLFBOOT_SECTOR_SIZE") == 0) && dup) {
189+
env_sector_size = dup;
190+
}
191+
#else
184192
env_sector_size = getenv("WOLFBOOT_SECTOR_SIZE");
193+
#endif
185194
if (!env_sector_size) {
186195
fprintf(stderr, "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n"
187196
"order to sign a delta update.\n");
197+
#if defined(_MSC_VER)
198+
free(dup);
199+
#endif
188200
exit(6);
189201
} else {
190202
sec_sz = atoi(env_sector_size);
@@ -193,11 +205,28 @@ int wb_diff_get_sector_size(void)
193205
sec_sz = strtol(env_sector_size, NULL, 16);
194206
if (errno != 0) {
195207
fprintf(stderr, "Invalid WOLFBOOT_SECTOR_SIZE value\n");
208+
#if defined(_MSC_VER)
209+
free(dup);
210+
#endif
196211
exit(6);
197212
}
198213
}
199214
}
200-
return sec_sz;
215+
216+
#if defined(_MSC_VER)
217+
free(dup);
218+
#endif
219+
220+
if (sec_sz == 0) {
221+
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE cannot be 0\n");
222+
exit(6);
223+
}
224+
if (sec_sz > (uint32_t)INT_MAX) {
225+
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n",
226+
sec_sz, INT_MAX);
227+
exit(6);
228+
}
229+
return (int)sec_sz;
201230
}
202231

203232
int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_b, uint32_t len_b)

tools/bin-assemble/bin-assemble.c

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,25 @@ int binentry_address_compare(const void* a, const void* b)
7272
}
7373
}
7474

75+
#ifdef _WIN32
76+
/* 'strerror' has been explicitly marked deprecated */
77+
static const char* win_strerror(int err, char* buf, size_t bufsz) {
78+
if (strerror_s(buf, bufsz, err) != 0) {
79+
buf[0] = '\0';
80+
}
81+
return buf;
82+
}
83+
#endif
84+
7585
int main(int argc, const char* argv[]) {
86+
#ifdef _WIN32
87+
char errbuf[128] = { 0 };
88+
errno_t fe;
89+
#endif
7690
const char* outname = NULL;
91+
FILE* fo = NULL;
92+
FILE* fi = NULL;
93+
7794
size_t i = 0;
7895
size_t num_entries = 0;
7996
binentry_t* entries = NULL;
@@ -98,43 +115,48 @@ int main(int argc, const char* argv[]) {
98115
return EXIT_FAILURE;
99116
}
100117

101-
for (i=0; i<num_entries; i++) {
118+
for (i = 0; i < num_entries; i++) {
102119
char* endptr = NULL;
103120
struct stat st;
104121

105-
entries[i].address = strtol(argv[2*i + 2], &endptr, 0);
122+
entries[i].address = strtol(argv[2 * i + 2], &endptr, 0);
106123
if (*endptr) {
107124
fprintf(stderr,
108-
"Remaining characters in address field %s\n", endptr);
125+
"Remaining characters in address field %s\n", endptr);
109126
}
110-
entries[i].fname = argv[2*i + 3];
127+
entries[i].fname = argv[2 * i + 3];
111128

112129
if (stat(entries[i].fname, &st)) {
130+
#ifdef _WIN32
131+
fprintf(stderr, "unable to stat %s: %s\n",
132+
entries[i].fname, win_strerror(errno, errbuf, sizeof errbuf));
133+
#else
113134
fprintf(stderr, "unable to stat %s: %s\n", entries[i].fname,
114-
strerror(errno));
135+
strerror(errno));
136+
#endif
115137
return EXIT_FAILURE;
116138
}
117139

118140
entries[i].nbytes = st.st_size;
119141

120142
#if VERBOSE
121143
printf("%s %zu %zu\n",
122-
entries[i].fname,
123-
entries[i].address,
124-
entries[i].nbytes);
144+
entries[i].fname,
145+
entries[i].address,
146+
entries[i].nbytes);
125147
#endif
126148
}
127149

128150
qsort(entries, num_entries, sizeof(binentry_t), binentry_address_compare);
129151

130152
// check for overlap
131-
for (i=1; i<num_entries; i++) {
132-
size_t endaddr = entries[i-1].address + entries[i-1].nbytes;
153+
for (i = 1; i < num_entries; i++) {
154+
size_t endaddr = entries[i - 1].address + entries[i - 1].nbytes;
133155
if (endaddr > entries[i].address) {
134156
fprintf(stderr,
135-
"overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n",
136-
entries[i-1].fname, endaddr,
137-
entries[i].fname, entries[i].address);
157+
"overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n",
158+
entries[i - 1].fname, endaddr,
159+
entries[i].fname, entries[i].address);
138160
err = 1;
139161
}
140162
if (err) {
@@ -144,23 +166,39 @@ int main(int argc, const char* argv[]) {
144166
}
145167

146168
// TODO: consider handling stdout "-"
147-
148-
FILE* fo = fopen(outname, "wb");
149-
if (fo == NULL) {
169+
#ifdef _WIN32
170+
fe = fopen_s(&fo, outname, "wb");
171+
if (fo == NULL || fe != 0) {
150172
fprintf(stderr, "opening %s failed %s\n",
151-
outname, strerror(errno));
173+
outname, win_strerror((int)fe, errbuf, sizeof errbuf));
152174
return EXIT_FAILURE;
153175
}
176+
#else
177+
fo = fopen(outname, "wb");
178+
if (fo == NULL) {
179+
fprintf(stderr, "opening %s failed %s\n", outname, strerror(errno));
180+
return EXIT_FAILURE;
181+
}
182+
#endif
154183

155184
cur_add = entries[0].address;
156185
for (i=0; i<num_entries; i++) {
157186
size_t fillSz = entries[i].address - cur_add;
158-
FILE* fi = fopen(entries[i].fname, "rb");
159-
if (fi == NULL){
187+
#ifdef _WIN32
188+
fe = fopen_s(&fi, entries[i].fname, "rb");
189+
if (fi == NULL || fe != 0) {
190+
fprintf(stderr, "opening %s failed %s\n",
191+
entries[i].fname, win_strerror((int)fe, errbuf, sizeof errbuf));
192+
return EXIT_FAILURE;
193+
}
194+
#else
195+
fi = fopen(entries[i].fname, "rb");
196+
if (fi == NULL) {
160197
fprintf(stderr, "opening %s failed %s\n",
161-
entries[i].fname, strerror(errno));
198+
entries[i].fname, strerror(errno));
162199
return EXIT_FAILURE;
163200
}
201+
#endif
164202

165203
if (fillSz > 0 && fillSz < INT_MAX) {
166204
/* fill until address - blocks then bytes */
@@ -194,7 +232,7 @@ int main(int argc, const char* argv[]) {
194232
cur_add += nw;
195233
if (nr != nw) {
196234
fprintf(stderr,
197-
"Failed to wrote %zu bytes of the %zu bytes read from %s\n",
235+
"Failed to write %zu bytes of the %zu bytes read from %s\n",
198236
nw, nr, entries[i].fname);
199237
return EXIT_FAILURE;
200238
}
@@ -209,10 +247,9 @@ int main(int argc, const char* argv[]) {
209247
if (ferror(fo)) {
210248
fprintf(stderr,
211249
"error writing to %s\n",
212-
entries[i].fname);
250+
outname);
213251
return EXIT_FAILURE;
214252
}
215-
216253
}
217254

218255
fprintf(stderr, "\tAdded %12zu bytes at 0x%08zx from %s\n",

0 commit comments

Comments
 (0)