Skip to content

Commit ad6d0bf

Browse files
committed
[.github/workflows/linux-Windows-macOS.yml] Show ctest output on error; [c89stringutils/c89stringutils_string_extras.h] Add different _vscprintf, vasprintf, jasprintf impl for CRT compat on MSVC; [c89stringutils_tests/test_c89stringutils/test_string_extras.h] Use ASSERT_STR_EQ for better error messages; impl x_asprintf_should_succeed
1 parent 094c28c commit ad6d0bf

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

.github/workflows/linux-Windows-macOS-sunOS.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ jobs:
2323
with:
2424
prepare: pkg install cmake wget
2525
run: |
26-
mkdir build && cd build
27-
mkdir -p c89stringutils_tests/test_downloads
26+
mkdir -p build/c89stringutils_tests/test_downloads
27+
cd build
2828
/opt/ooce/bin/wget https://raw.githubusercontent.com/silentbicycle/greatest/11a6af1/greatest.h -o c89stringutils_tests/test_downloads/greatest.h
2929
/opt/ooce/bin/cmake -DCMAKE_BUILD_TYPE="Debug" ..
3030
/opt/ooce/bin/cmake --build .
31-
/opt/ooce/bin/ctest -C Debug .
31+
/opt/ooce/bin/ctest -C Debug --rerun-failed --output-on-failure .
3232
if: matrix.os == 'macos-10.15'
3333
- name: mkdir
3434
run: mkdir build
@@ -41,5 +41,5 @@ jobs:
4141
if: matrix.os != 'macos-10.15'
4242
- name: test
4343
working-directory: ./build
44-
run: ctest -C Debug .
44+
run: ctest -C Debug --rerun-failed --output-on-failure .
4545
if: matrix.os != 'macos-10.15'

c89stringutils/c89stringutils_string_extras.h

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -297,44 +297,43 @@ extern int asprintf(char **str, const char *fmt, ...);
297297

298298
#define INIT_SZ 128
299299

300-
extern int vasprintf(char **str, const char *fmt, va_list ap) {
301-
int ret;
302-
va_list ap2;
303-
char *string, *newstr;
304-
size_t len;
305-
306-
if ((string = (char *)malloc(INIT_SZ)) == NULL)
307-
goto fail;
308-
309-
VA_COPY(ap2, ap);
310-
ret = vsnprintf(string, INIT_SZ, fmt, ap2);
311-
va_end(ap2);
312-
if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
313-
*str = string;
314-
} else if (ret == INT_MAX || ret < 0) { /* Bad length */
315-
free(string);
316-
goto fail;
317-
} else { /* bigger than initial, realloc allowing for nul */
318-
len = (size_t)ret + 1;
319-
if ((newstr = (char *)realloc(string, len)) == NULL) {
320-
free(string);
321-
goto fail;
322-
}
323-
VA_COPY(ap2, ap);
324-
ret = vsnprintf(newstr, len, fmt, ap2);
325-
va_end(ap2);
326-
if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
327-
free(newstr);
328-
goto fail;
329-
}
330-
*str = newstr;
300+
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32__) && \
301+
!defined(__NT__)
302+
int _vscprintf(const char *format, va_list pargs) {
303+
int retval;
304+
va_list argcopy;
305+
va_copy(argcopy, pargs);
306+
retval = vsnprintf(NULL, 0, format, argcopy);
307+
va_end(argcopy);
308+
return retval;
309+
}
310+
#endif /* !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32__) && \
311+
!defined(__NT__) */
312+
313+
int vasprintf(char **strp, const char *fmt, va_list ap) {
314+
const int len = _vscprintf(fmt, ap);
315+
size_t size;
316+
char *str;
317+
int r;
318+
if (len == -1)
319+
return -1;
320+
size = (size_t)len + 1;
321+
str = malloc(size);
322+
if (str == NULL)
323+
return ENOMEM;
324+
r =
325+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
326+
vsprintf_s
327+
#else
328+
vsnprintf
329+
#endif
330+
(str, len + 1, fmt, ap);
331+
if (r == -1) {
332+
free(str);
333+
return -1;
331334
}
332-
return ret;
333-
334-
fail:
335-
*str = NULL;
336-
errno = ENOMEM;
337-
return -1;
335+
*strp = str;
336+
return r;
338337
}
339338

340339
extern int asprintf(char **str, const char *fmt, ...) {
@@ -368,26 +367,18 @@ char *jasprintf(char **unto, const char *fmt, ...) {
368367
size_t base_length = unto && *unto ? strlen(*unto) : 0;
369368
int length;
370369
char *result;
371-
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
372-
int len;
373-
char *res_buf;
374-
#endif /* defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
375-
defined(__NT__) */
376370
va_start(args, fmt);
377371
/* check length for failure */
378372
length = vsnprintf(NULL, 0, fmt, args);
379373
va_end(args);
380374

381-
/* check result for failure */
375+
/* TODO: check result for failure */
382376
result = realloc(unto ? *unto : NULL, base_length + length + 1);
383377

384378
va_start(args, fmt);
385379
/* check for failure*/
386380
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
387-
len = _vscprintf(fmt, args) + 1;
388-
res_buf = (char *)malloc(len * sizeof(char));
389-
if (res_buf != NULL)
390-
vsprintf_s(res_buf, len, fmt, args);
381+
vsprintf_s(result + base_length, length + 1, fmt, args);
391382
#else
392383
vsprintf(result + base_length, fmt, args);
393384
#endif /* defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \

c89stringutils_tests/test_c89stringutils/test_string_extras.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ TEST x_strnstr_should_succeed(void) {
1212
}
1313

1414
TEST x_strnstr_should_fail(void) {
15-
ASSERT_EQ(strcmp(buffer, strnstr(buffer, target, strlen(buffer))), 0);
15+
ASSERT_STR_EQ(buffer, strnstr(buffer, target, strlen(buffer)));
16+
PASS();
17+
}
18+
19+
TEST x_asprintf_should_succeed(void) {
20+
char *s = NULL;
21+
asprintf(&s, "foo%s", "bar");
22+
ASSERT_STR_EQ("foobar", s);
23+
free(s);
1624
PASS();
1725
}
1826

1927
TEST x_jasprintf_should_succeed(void) {
2028
char *s = NULL;
2129
jasprintf(&s, "foo%s", "bar");
2230
jasprintf(&s, "can%s", "haz");
23-
ASSERT_EQ(strcmp(s, "foobarcanhaz"), 0);
31+
ASSERT_STR_EQ("foobarcanhaz", s);
2432
free(s);
2533
PASS();
2634
}
@@ -29,5 +37,6 @@ TEST x_jasprintf_should_succeed(void) {
2937
SUITE(strnstr_suite) {
3038
RUN_TEST(x_strnstr_should_succeed);
3139
RUN_TEST(x_strnstr_should_fail);
40+
RUN_TEST(x_asprintf_should_succeed);
3241
RUN_TEST(x_jasprintf_should_succeed);
3342
}

0 commit comments

Comments
 (0)