Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Fix oacr build warnings #68

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Fix oacr build warnings #68

wants to merge 6 commits into from

Conversation

kasontey
Copy link

Fixed TVS oacr warnings that appear when built for console code.
Windows wants us to check buffer size, while this is not required on Linux, so two new defines added for memcpy_s and vsnprintf_s.
Also Windows suggest we use GetAddrInfoW which takes a windows wide string, this method does not work with Linux. So we will suppress the warning.

libftl/logging.c Outdated
@@ -32,7 +32,7 @@ void ftl_log_msg(ftl_stream_configuration_private_t *ftl, ftl_log_severity_t log

m.msg.log.log_level = log_level;
va_start(args, fmt);
vsnprintf(m.msg.log.string, sizeof(m.msg.log.string), fmt, args);
vsnprintf_s(m.msg.log.string, sizeof(m.msg.log.string), sizeof(m.msg.log.string), fmt, args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you want something like strlen here, sizeof will return the size of the type which I think is char*, so 4 bytes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call strlen on a char[] ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, [] and * is the same in C anyway. (Both are a pointer, it is just syntactic sugar, really).
Also sizeof(char*) is 8 bytes on 64-bit :p

Copy link
Author

@kasontey kasontey Jun 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, but there should be a nice way to get the count

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, strlen will look through the array until it finds a '/0'

Copy link

@TomyLobo TomyLobo Jun 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused about your page numbers there for a while.
Apparently, pdf page 92 is standard page 80 :)

Anyway, I actually have no idea what type the "string" field has. I assumed that, being a field it was not char[], but char[x] (which behave differently in sizeof, at least in C++)

So, what is it? char[] or char[x]?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry about the page confusion.
The "string" variable is a char[1024] in a struct which is in the msg union, which is in the m struct.

typedef struct {
  int log_level;
  char string[1024];
}ftl_status_log_msg_t;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://ideone.com/1YQUTV there we go
So this means that "m.msg.log.string" cannot actually be a char[], assuming this piece of code compiles in gcc 6.3 :)

This means it's likely char[x] and sizeof was correct.
If, however, it is char*, you cannot use sizeof. In that case, you need the size from somewhere else. If it's not initialized and you did not store the length somewhere alongside the pointer, good luck figuring out the length :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup so seems like this is a char[x], so I'll move this back to sizeof.
And for the count we'll use the _TRUNCATE for string truncation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Thanks everyone!

libftl/media.c Outdated
@@ -842,7 +842,7 @@ static int _media_send_slot(ftl_stream_configuration_private_t *ftl, nack_slot_t
int pkt_len;

os_lock_mutex(&ftl->media.mutex);
memcpy(pkt, slot->packet, slot->len);
memcpy_s(pkt, sizeof(pkt), slot->packet, slot->len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be sizeof(pkt) * MAX_PACKET_BUFFER

@QuinnDamerell-MS
Copy link
Contributor

It looks like this doesn't build on Linux currently, can you take a look? Here are the build errors.

[ 38%] Building C object CMakeFiles/ftl.dir/libftl/logging.c.o
In file included from /var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/logging.c:26:0:
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/ftl_private.h:135:43: warning: VA_ARGS can only appear in the expansion of a C99 variadic macro [enabled by default]
#define vsnprintf_s(buf, bufsz, cnt, fmt, VA_ARGS) vsnprintf(buf, cnt, fmt, VA_ARGS)
^
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/ftl_private.h:135:81: warning: VA_ARGS can only appear in the expansion of a C99 variadic macro [enabled by default]
#define vsnprintf_s(buf, bufsz, cnt, fmt, VA_ARGS) vsnprintf(buf, cnt, fmt, VA_ARGS)
^
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/logging.c: In function ‘ftl_log_msg’:
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/logging.c:35:59: error: ‘_TRUNCATE’ undeclared (first use in this function)
vsnprintf_s(m.msg.log.string, sizeof(m.msg.log.string), _TRUNCATE, fmt, args);
^
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/ftl_private.h:135:71: note: in definition of macro ‘vsnprintf_s’
#define vsnprintf_s(buf, bufsz, cnt, fmt, VA_ARGS) vsnprintf(buf, cnt, fmt, VA_ARGS)
^
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/logging.c:35:59: note: each undeclared identifier is reported only once for each function it appears in
vsnprintf_s(m.msg.log.string, sizeof(m.msg.log.string), _TRUNCATE, fmt, args);
^
/var/lib/jenkins/workspace/tl-sdk_fixoacrbuildwarnings-4OYIR3M7N4Z4DCVGVF3LAQBGZZPEVQSZO7DAGCWVR4O427WAWWQA/ftl-sdk/libftl/ftl_private.h:135:71: note: in definition of macro ‘vsnprintf_s’
#define vsnprintf_s(buf, bufsz, cnt, fmt, VA_ARGS) vsnprintf(buf, cnt, fmt, VA_ARGS)
^
make[2]: *** [CMakeFiles/ftl.dir/libftl/logging.c.o] Error 1

@kasontey
Copy link
Author

@QuinnDamerell-MS New commit is up. Can you see if Linux still throws those build errors?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants