Skip to content

Commit

Permalink
PVS-Studio bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdremov committed Mar 17, 2021
1 parent 35f64d5 commit f61b1e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.17)
project(CoRoutines C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include_directories(.)
add_executable(coSort
Expand Down
11 changes: 11 additions & 0 deletions src/coSort.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#define _XOPEN_SOURCE /* Mac compatibility. */
Expand Down Expand Up @@ -52,9 +55,17 @@ void fileInputNumbers(ContextData *nowData, stack *input, int id) {
CoPlanner_rollIfLatency(&planner);

char *fileRead = malloc(size + 1);

if (!fileRead){
printf("Error on malloc in coroutine %d", id);
CoPlanner_finishCoroutine(&planner);
return;
}
if (!nowData->userData.file){
printf("Error on file pointer in coroutine %d", id);
CoPlanner_finishCoroutine(&planner);
free(fileRead);
return;
}

CoPlanner_rollIfLatency(&planner);
Expand Down
19 changes: 15 additions & 4 deletions src/stack.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//
// Created by Александр Дремов on 17.03.2021.
//
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

#include "stack.h"

#define handleError(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)

stack* newStack(int capacity) {
stack *pt = (stack*)malloc(sizeof(stack));
if (!pt){
handleError("malloc");
}
Stack_init(pt, capacity);
return pt;
}
Expand All @@ -15,6 +19,9 @@ void Stack_init(stack *pt, int capacity){
pt->maxsize = capacity;
pt->top = -1;
pt->items = (int*)malloc(sizeof(int) * capacity);
if (!pt->items){
handleError("malloc");
}
}

size_t Stack_size(stack *pt) {
Expand All @@ -30,7 +37,11 @@ int Stack_isFull(stack *pt) {
}

void Stack_expand(stack *pt){
pt->items = (int*)realloc(pt->items, sizeof(int) * pt->maxsize * 2);
int* new = (int*)realloc(pt->items, sizeof(int) * pt->maxsize * 2);
if (!new){
handleError("malloc");
}
pt->items = new;
pt->maxsize *= 2;
}

Expand Down
1 change: 1 addition & 0 deletions src/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef COROUTINES_STACK_H
#define COROUTINES_STACK_H
#include <stdlib.h>
#include <stdio.h>

typedef struct {
size_t maxsize; // define max capacity of the stack
Expand Down

0 comments on commit f61b1e3

Please sign in to comment.