From 55d9fdddf46056979247f187c18b5141a22233a9 Mon Sep 17 00:00:00 2001 From: Arpenaige Date: Sun, 3 Mar 2024 04:44:42 +0300 Subject: [PATCH 1/2] fix UB usage of sprintf function in formatting json functions --- src/sink/snk_categories.c | 32 +++++++++++++++++--------------- src/sink/snk_pots.c | 18 ++++++++++-------- src/sink/snk_tracks.c | 21 +++++++++++---------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/sink/snk_categories.c b/src/sink/snk_categories.c index a201bf5..286b703 100644 --- a/src/sink/snk_categories.c +++ b/src/sink/snk_categories.c @@ -336,32 +336,34 @@ void snk_categories_process_format_text_json(snk_categories_obj * obj) { unsigned int iChannel; + int tmpBufferSize; obj->buffer[0] = 0x00; + tmpBufferSize = 0; - sprintf(obj->buffer,"%s{\n",obj->buffer); - sprintf(obj->buffer,"%s \"timeStamp\": %llu,\n",obj->buffer,obj->in->timeStamp); - sprintf(obj->buffer,"%s \"src\": [\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, "{\n"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " \"timeStamp\": %llu,\n", obj->in->timeStamp); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " \"src\": [\n"); for (iChannel = 0; iChannel < obj->nChannels; iChannel++) { - switch(obj->in->categories->array[iChannel]) { + switch (obj->in->categories->array[iChannel]) { - case 0x01: + case 0x01: - sprintf(obj->buffer,"%s { \"category\": \"speech\" }",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"speech\" }"); break; - case 0x00: + case 0x00: - sprintf(obj->buffer,"%s { \"category\": \"nonspeech\" }",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"nonspeech\" }"); break; - default: + default: - sprintf(obj->buffer,"%s { \"category\": \"undefined\" }",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"undefined\" }"); break; @@ -369,16 +371,16 @@ if (iChannel != (obj->nChannels - 1)) { - sprintf(obj->buffer,"%s,",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, ","); } - sprintf(obj->buffer,"%s\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, "\n"); } - - sprintf(obj->buffer,"%s ]\n",obj->buffer); - sprintf(obj->buffer,"%s}\n",obj->buffer); + + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " ]\n"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,"}\n"); obj->bufferSize = strlen(obj->buffer); diff --git a/src/sink/snk_pots.c b/src/sink/snk_pots.c index 8445599..d4710bd 100644 --- a/src/sink/snk_pots.c +++ b/src/sink/snk_pots.c @@ -352,30 +352,32 @@ void snk_pots_process_format_text_json(snk_pots_obj * obj) { unsigned int iPot; + int tmpBufferSize; obj->buffer[0] = 0x00; + tmpBufferSize = 0; - sprintf(obj->buffer,"%s{\n",obj->buffer); - sprintf(obj->buffer,"%s \"timeStamp\": %llu,\n",obj->buffer,obj->in->timeStamp); - sprintf(obj->buffer,"%s \"src\": [\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,"{\n"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," \"timeStamp\": %llu,\n",obj->in->timeStamp); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," \"src\": [\n"); for (iPot = 0; iPot < obj->nPots; iPot++) { - sprintf(obj->buffer,"%s { \"x\": %1.3f, \"y\": %1.3f, \"z\": %1.3f, \"E\": %1.3f }", obj->buffer, + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," { \"x\": %1.3f, \"y\": %1.3f, \"z\": %1.3f, \"E\": %1.3f }", obj->in->pots->array[iPot*4+0], obj->in->pots->array[iPot*4+1], obj->in->pots->array[iPot*4+2], obj->in->pots->array[iPot*4+3]); if (iPot != (obj->nPots - 1)) { - sprintf(obj->buffer,"%s,",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,","); } - sprintf(obj->buffer,"%s\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,"\n"); } - sprintf(obj->buffer,"%s ]\n",obj->buffer); - sprintf(obj->buffer,"%s}\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," ]\n"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,"}\n"); obj->bufferSize = strlen(obj->buffer); diff --git a/src/sink/snk_tracks.c b/src/sink/snk_tracks.c index b92eb46..bb1742c 100644 --- a/src/sink/snk_tracks.c +++ b/src/sink/snk_tracks.c @@ -341,17 +341,18 @@ void snk_tracks_process_format_text_json(snk_tracks_obj * obj) { unsigned int iTrack; + int tmpBufferSize; obj->buffer[0] = 0x00; + tmpBufferSize = 0; - sprintf(obj->buffer,"%s{\n",obj->buffer); - sprintf(obj->buffer,"%s \"timeStamp\": %llu,\n",obj->buffer,obj->in->timeStamp); - sprintf(obj->buffer,"%s \"src\": [\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,"{\n"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," \"timeStamp\": %llu,\n",obj->in->timeStamp); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," \"src\": [\n"); for (iTrack = 0; iTrack < obj->nTracks; iTrack++) { - sprintf(obj->buffer,"%s { \"id\": %llu, \"tag\": \"%s\", \"x\": %1.3f, \"y\": %1.3f, \"z\": %1.3f, \"activity\": %1.3f }", - obj->buffer, + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize," { \"id\": %llu, \"tag\": \"%s\", \"x\": %1.3f, \"y\": %1.3f, \"z\": %1.3f, \"activity\": %1.3f }", obj->in->tracks->ids[iTrack], obj->in->tracks->tags[iTrack], obj->in->tracks->array[iTrack*3+0], @@ -361,16 +362,16 @@ if (iTrack != (obj->nTracks - 1)) { - sprintf(obj->buffer,"%s,",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, ","); } - sprintf(obj->buffer,"%s\n",obj->buffer); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, "\n"); } - - sprintf(obj->buffer,"%s ]\n",obj->buffer); - sprintf(obj->buffer,"%s}\n",obj->buffer); + + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " ]\n"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize,"}\n"); obj->bufferSize = strlen(obj->buffer); From 5b636afc1b47b18a24478534bab4d6cfcde22979 Mon Sep 17 00:00:00 2001 From: Arpenaige Date: Sun, 3 Mar 2024 04:53:04 +0300 Subject: [PATCH 2/2] fix code style --- src/sink/snk_categories.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sink/snk_categories.c b/src/sink/snk_categories.c index 286b703..f2ed1e4 100644 --- a/src/sink/snk_categories.c +++ b/src/sink/snk_categories.c @@ -347,23 +347,23 @@ for (iChannel = 0; iChannel < obj->nChannels; iChannel++) { - switch (obj->in->categories->array[iChannel]) { + switch(obj->in->categories->array[iChannel]) { - case 0x01: + case 0x01: - tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"speech\" }"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"speech\" }"); break; - case 0x00: + case 0x00: - tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"nonspeech\" }"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"nonspeech\" }"); break; - default: + default: - tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"undefined\" }"); + tmpBufferSize += sprintf(obj->buffer + tmpBufferSize, " { \"category\": \"undefined\" }"); break;