Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 0416569

Browse files
DellaBittapnfishergguussmikevoytArpad Tigyi
authored
Merge development into master in prep for Release 1.0.3 (#122)
* fix fopen FILE* unsigned comparison bug iotc_bsp_io_fs_open() in src/bsp/platform/posix/iotc_bsp_io_fs_posix.c sets resource_handle_out to the FILE* returned by fopen() if fopen() doesn't fail. This in turn gets assigned to the resource manager's context->resource_handle. Later, iotc_resource_manager_read() in src/libiotc/io/fs/iotc_resource_manager.c checks to make sure context->resource_handle is not less the zero. But there's nothing about fopen() that ensures that the FILE* returned by fopen() can't be a value less than zero (when used in a signed comparison). If iotc_resource_manager_read() is going to assume a value less than 0 indicates a BSP file io open error, then we need to replace the use of fopen() in iotc_bsp_io_fs_posix.c with open(). * Fix User Guide Links to Headings We must use all lower case, remove the colons, and substitute spaces for hypens to get heading links working correctly. See the code that creates links here: https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb * downcases the string * removes anything that is not a letter, number, space or hyphen (see the source for how Unicode is handled) * changes any space to a hyphen. * if that is not unique, add "-1", "-2", "-3",... to make it unique * Release notes update and in-code version increase to 1.0.3. (#123) * Release notes update and in-code version increase to 1.0.3. Co-authored-by: Philip Fisher <[email protected]> Co-authored-by: Gus Class <[email protected]> Co-authored-by: Mike V <[email protected]> Co-authored-by: Arpad Tigyi <[email protected]>
1 parent 96018ab commit 0416569

File tree

4 files changed

+49
-35
lines changed

4 files changed

+49
-35
lines changed

RELEASE-NOTES.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# Google Cloud IoT Core Device SDK for Embedded C version 1.0.3
2+
#### April 28, 2021
3+
4+
- Updated the POSIX BSP to use `open` instead of `fopen` for more consistent error detection, [#100](https://github.com/GoogleCloudPlatform/iot-device-sdk-embedded-c/pull/100) by [@pnfisher](https://github.com/pnfisher).
5+
- Fixed a non null-terminated issue in message.topic with ‘iotc_user_subscription_callback_’, [#111](https://github.com/GoogleCloudPlatform/iot-device-sdk-embedded-c/issues/111) by [@csobrinho](https://github.com/csobrinho).
6+
- Added overflow protection to ‘__iotc_calloc()’ calls, [#119](https://github.com/GoogleCloudPlatform/iot-device-sdk-embedded-c/pull/119)
7+
- Updates to the Zephyr Troubleshooting guide, [#104](https://github.com/GoogleCloudPlatform/iot-device-sdk-embedded-c/pull/104) by [@galz10](https://github.com/galz10).
8+
- Fixes to links within the User Guide, [#102](https://github.com/GoogleCloudPlatform/iot-device-sdk-embedded-c/pull/102) by [@mikevoyt](https://github.com/mikevoyt).
9+
- Thank you to all of our contributors!
10+
111
# Google Cloud IoT Core Device SDK for Embedded C version 1.0.2
212
#### February 4, 2020
313

doc/user_guide.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ To connect to Cloud IoT Core, call **`iotc_connect()`**. This function enqueues
204204
* TLS handshaking and certificate validation
205205
* MQTT credential handshaking
206206

207-
Note: the call to connect returns immediately. The connection operation is fulfilled on subsequent calls to the Device SDK's event processing functions, as described in [Step 3: Process Events](step-3-process-events).
207+
Note: the call to connect returns immediately. The connection operation is fulfilled on subsequent calls to the Device SDK's event processing functions, as described in [Step 3: Process Events](#step-3-process-events).
208208

209209
#### Connect callback
210210

211-
When the **`iotc_connect()`** function runs, the Device SDK initalizes a callback function. The callback function is invoked when a connection to Cloud IoT Core is established or when the connection is unsuccessful. The callback function is also invoked when an established connection is lost or shut down. See [Step 6: Disconnect and Shut Down](step-6-disconnect-and-shut-down) for details.
211+
When the **`iotc_connect()`** function runs, the Device SDK initalizes a callback function. The callback function is invoked when a connection to Cloud IoT Core is established or when the connection is unsuccessful. The callback function is also invoked when an established connection is lost or shut down. See [Step 6: Disconnect and Shut Down](#step-6-disconnect-and-shut-down) for details.
212212

213213
### Step 3: Process events
214214

@@ -228,7 +228,7 @@ Incoming messages include the topic data as part of the callback parameters. Spe
228228

229229
#### QoS level
230230

231-
The [specified QoS level](#MQTT v3.1.1) is the maximum QoS level of incoming messages. For instance, if you set the QoS level to 0, QoS 1 messages are downgraded to QoS 0.
231+
The [specified QoS level](#mqtt-v3.1.1) is the maximum QoS level of incoming messages. For instance, if you set the QoS level to 0, QoS 1 messages are downgraded to QoS 0.
232232

233233
The QoS level also affects the Device SDK's memory overhead. QoS 2 messages use the most memory overhead, while QoS 0 messages use the least.
234234

@@ -250,17 +250,17 @@ This callback function is for environments with severe memory restrictions. For
250250

251251
### Step 6: Disconnect and shut down
252252

253-
To disconnect from Cloud IoT Core, invoke the **`iotc_shutdown_connection()`** function. This function enqueues an event that cleanly closes the socket connection. After the connection is terminated, the Device SDK invokes the [connect callback](#Step 2: Connect) function.
253+
To disconnect from Cloud IoT Core, invoke the **`iotc_shutdown_connection()`** function. This function enqueues an event that cleanly closes the socket connection. After the connection is terminated, the Device SDK invokes the [connect callback](#step-2-connect) function.
254254

255255
Note: Do not delete the context until the the connect callback is invoked.
256256

257257
#### Disconnection types and reconnecting
258258

259259
The Device SDK checks the parameters of the connect callback function to determine if a device was intentionally or incidentally disconnected. See the [**Connect callback**](#connect-callback) section for more information.
260260

261-
If an error disconnects a device, the client application can safely and immediately call **`iotc_connect()`** from within the connect callback function itself, using the same context that was just disconnected. This ques a new [connection request](#Step 2: Connect).
261+
If an error disconnects a device, the client application can safely and immediately call **`iotc_connect()`** from within the connect callback function itself, using the same context that was just disconnected. This ques a new [connection request](#step-2-connect).
262262

263-
If a client application intentionally closes a connection, it retains the [existing context](#Step 1: Create a context) and invokes the connect function again later.
263+
If a client application intentionally closes a connection, it retains the [existing context](#step-1-create-a-context) and invokes the connect function again later.
264264

265265
#### Freeing memory and shutting down
266266

src/bsp/platform/posix/iotc_bsp_io_fs_posix.c

+31-27
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include <memory.h>
2626
#include <stdio.h>
2727
#include <sys/stat.h>
28+
#include <sys/types.h>
29+
#include <fcntl.h>
30+
#include <unistd.h>
2831

2932
/* The size of the buffer to be used for reads. */
3033
const size_t iotc_bsp_io_fs_buffer_size = 1024;
@@ -41,7 +44,7 @@ const size_t iotc_bsp_io_fs_buffer_size = 1024;
4144
* Database type of file handles and allocated memory chunks.
4245
*/
4346
typedef struct iotc_bsp_io_fs_posix_file_handle_container_s {
44-
FILE* posix_fp;
47+
int posix_fd;
4548
uint8_t* memory_buffer;
4649
struct iotc_bsp_io_fs_posix_file_handle_container_s* __next;
4750
} iotc_bsp_io_fs_posix_file_handle_container_t;
@@ -102,11 +105,11 @@ static iotc_bsp_io_fs_state_t iotc_bsp_io_fs_posix_stat_2_iotc_bsp_io_fs_stat(
102105
* @return 1 if list element is the one with the matching fp 0 otherwise
103106
*/
104107
static uint8_t iotc_bsp_io_fs_posix_file_list_cnd(
105-
iotc_bsp_io_fs_posix_file_handle_container_t* list_element, FILE* fp) {
108+
iotc_bsp_io_fs_posix_file_handle_container_t* list_element, int fd) {
106109
assert(NULL != list_element);
107-
assert(NULL != fp);
110+
assert(fd >= 0);
108111

109-
return (list_element->posix_fp == fp) ? 1 : 0;
112+
return (list_element->posix_fd == fd) ? 1 : 0;
110113
}
111114

112115
iotc_bsp_io_fs_state_t iotc_bsp_io_fs_stat(
@@ -153,12 +156,12 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_open(
153156
iotc_bsp_io_fs_posix_file_handle_container_t* new_entry = NULL;
154157
iotc_bsp_io_fs_state_t ret = IOTC_BSP_IO_FS_STATE_OK;
155158

156-
FILE* fp = fopen(resource_name,
157-
(open_flags & IOTC_BSP_IO_FS_OPEN_READ) ? "rb" : "wb");
159+
int fd = open(resource_name,
160+
(open_flags & IOTC_BSP_IO_FS_OPEN_READ) ? O_RDONLY : O_WRONLY);
158161

159162
/* if error on fopen check the errno value */
160163
IOTC_BSP_IO_FS_CHECK_CND(
161-
NULL == fp, iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno),
164+
fd < 0, iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno),
162165
ret);
163166

164167
/* allocate memory for the files database element */
@@ -167,23 +170,23 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_open(
167170
memset(new_entry, 0, sizeof(iotc_bsp_io_fs_posix_file_handle_container_t));
168171

169172
/* store the posix file pointer */
170-
new_entry->posix_fp = fp;
173+
new_entry->posix_fd = fd;
171174

172175
/* add the entry to the database */
173176
IOTC_LIST_PUSH_BACK(iotc_bsp_io_fs_posix_file_handle_container_t,
174177
iotc_bsp_io_fs_posix_files_container, new_entry);
175178

176179
/* make sure that the size is as expected. */
177-
assert(sizeof(fp) == sizeof(iotc_bsp_io_fs_resource_handle_t));
180+
assert(sizeof(fd) <= sizeof(iotc_bsp_io_fs_resource_handle_t));
178181

179182
/* return fp as a resource handle */
180-
*resource_handle_out = (iotc_bsp_io_fs_resource_handle_t)fp;
183+
*resource_handle_out = (iotc_bsp_io_fs_resource_handle_t)fd;
181184

182185
return ret;
183186

184187
err_handling:
185-
if (NULL != fp) {
186-
fclose(fp);
188+
if (fd < 0) {
189+
close(fd);
187190
}
188191
iotc_bsp_mem_free(new_entry);
189192
*resource_handle_out = iotc_bsp_io_fs_init_resource_handle();
@@ -199,13 +202,13 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_read(
199202
}
200203

201204
iotc_bsp_io_fs_state_t ret = IOTC_BSP_IO_FS_STATE_OK;
202-
FILE* fp = (FILE*)resource_handle;
205+
int fd = (int)resource_handle;
203206
int fop_ret = 0;
204207

205208
iotc_bsp_io_fs_posix_file_handle_container_t* elem = NULL;
206209
IOTC_LIST_FIND(iotc_bsp_io_fs_posix_file_handle_container_t,
207210
iotc_bsp_io_fs_posix_files_container,
208-
iotc_bsp_io_fs_posix_file_list_cnd, fp, elem);
211+
iotc_bsp_io_fs_posix_file_list_cnd, fd, elem);
209212

210213
IOTC_BSP_IO_FS_CHECK_CND(NULL == elem, IOTC_BSP_IO_FS_RESOURCE_NOT_AVAILABLE,
211214
ret);
@@ -218,16 +221,16 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_read(
218221
}
219222

220223
/* let's set an offset */
221-
fop_ret = fseek(fp, offset, SEEK_SET);
224+
fop_ret = lseek(fd, offset, SEEK_SET);
222225

223226
/* if error on fseek check errno */
224227
IOTC_BSP_IO_FS_CHECK_CND(
225-
fop_ret != 0, iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno),
228+
fop_ret < 0 || (unsigned int)fop_ret != offset,
229+
iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno),
226230
ret);
227231

228232
/* use the fread to read the file chunk */
229-
fop_ret =
230-
fread(elem->memory_buffer, (size_t)1, iotc_bsp_io_fs_buffer_size, fp);
233+
fop_ret = read(fd, elem->memory_buffer, iotc_bsp_io_fs_buffer_size);
231234

232235
/* if error on fread check errno */
233236
IOTC_BSP_IO_FS_CHECK_CND(
@@ -260,30 +263,31 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_write(
260263
}
261264

262265
iotc_bsp_io_fs_state_t ret = IOTC_BSP_IO_FS_STATE_OK;
263-
FILE* fp = (FILE*)resource_handle;
266+
int fd = (int)resource_handle;
264267

265268
iotc_bsp_io_fs_posix_file_handle_container_t* elem = NULL;
266269
IOTC_LIST_FIND(iotc_bsp_io_fs_posix_file_handle_container_t,
267270
iotc_bsp_io_fs_posix_files_container,
268-
iotc_bsp_io_fs_posix_file_list_cnd, fp, elem);
271+
iotc_bsp_io_fs_posix_file_list_cnd, fd, elem);
269272

270273
IOTC_BSP_IO_FS_CHECK_CND(NULL == elem, IOTC_BSP_IO_FS_RESOURCE_NOT_AVAILABLE,
271274
ret);
272275

273276
/* let's set the offset */
274-
const int fop_ret = fseek(fp, offset, SEEK_SET);
277+
const int fop_ret = lseek(fd, offset, SEEK_SET);
275278

276279
/* if error on fseek check errno */
277280
IOTC_BSP_IO_FS_CHECK_CND(
278-
fop_ret != 0, iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno),
281+
fop_ret < 0 || (unsigned int)fop_ret != offset,
282+
iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno),
279283
ret);
280284

281-
*bytes_written = fwrite(buffer, (size_t)1, buffer_size, fp);
285+
*bytes_written = write(fd, buffer, buffer_size);
282286

283287
/* if error on fwrite check errno */
284288
IOTC_BSP_IO_FS_CHECK_CND(
285289
buffer_size != *bytes_written,
286-
iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(ferror(fp)), ret);
290+
iotc_bsp_io_fs_posix_errno_2_iotc_bsp_io_fs_state(errno), ret);
287291

288292
err_handling:
289293

@@ -297,13 +301,13 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_close(
297301
}
298302

299303
iotc_bsp_io_fs_state_t ret = IOTC_BSP_IO_FS_STATE_OK;
300-
FILE* fp = (FILE*)resource_handle;
304+
int fd = (int)resource_handle;
301305
iotc_bsp_io_fs_posix_file_handle_container_t* elem = NULL;
302306
int fop_ret = 0;
303307

304308
IOTC_LIST_FIND(iotc_bsp_io_fs_posix_file_handle_container_t,
305309
iotc_bsp_io_fs_posix_files_container,
306-
iotc_bsp_io_fs_posix_file_list_cnd, fp, elem);
310+
iotc_bsp_io_fs_posix_file_list_cnd, fd, elem);
307311

308312
/* if element not on the list return resource not available error */
309313
IOTC_BSP_IO_FS_CHECK_CND(NULL == elem, IOTC_BSP_IO_FS_RESOURCE_NOT_AVAILABLE,
@@ -313,7 +317,7 @@ iotc_bsp_io_fs_state_t iotc_bsp_io_fs_close(
313317
IOTC_LIST_DROP(iotc_bsp_io_fs_posix_file_handle_container_t,
314318
iotc_bsp_io_fs_posix_files_container, elem);
315319

316-
fop_ret = fclose(fp);
320+
fop_ret = close(fd);
317321

318322
/* if error on fclose check errno */
319323
IOTC_BSP_IO_FS_CHECK_CND(

src/libiotc/iotc_version.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2018-2020 Google LLC
1+
/* Copyright 2018-2021 Google LLC
22
*
33
* This is part of the Google Cloud IoT Device SDK for Embedded C.
44
* It is licensed under the BSD 3-Clause license; you may not use this file
@@ -19,6 +19,6 @@
1919

2020
#define IOTC_MAJOR 1
2121
#define IOTC_MINOR 0
22-
#define IOTC_REVISION 2
22+
#define IOTC_REVISION 3
2323

2424
#endif /* __IOTC_VERSION_H__ */

0 commit comments

Comments
 (0)