Giter VIP home page Giter VIP logo

safe_strn's Introduction

safe_strn

safe_strn is a C library for safe string operation. It includes some basic string functions to prevent buffer overflow.

Run

Firstly, compile the code with following command:

$ gcc lib/*.h lib/*.c test/*.h test/*.c example/*

Then, run the executable file.

$ ./a.out

Usage

Add lib/ folder under your workspace and include the library as follows. You may need to give the relative path depending on where you put location.

#include "safe_string.h"

int main() {
    
    char *src = "hello world";
    char *new_str = (char *) malloc(sizeof(char) * 12); // Allocate memory for string
    str_cpy(src, new_str, 11);  // Copy src to new_str
    
    int len = str_len(new_str); // Length of new_str
    
    char *str1 = "abc";
    char *str2 = "abc";
    int res = str_cmp(str1, str2, 3, 3);
    
    if (res < 0) {
        printf("str1 less than str2\n"): 
    } else if (res > 0){
        printf("str2 less than str1.\n");
    } else {
        printf("str1 and str2 are equal.\n");    // Prints "str1 and str2 are equal."
    }
    
    char *str = str_mk("create a new string.", 50); // Create a null-terminated string with maximum 50 length.
}

Documentation

str_mk

Creates string by preventing potential buffer overflow. Guarantees putting null-terminator at the end of the string. Also, prevents buffer overflow which is caused by integer overflow.

char *str_mk(const char *str, size_t str_len);

str_cpy

Copies the string from src to dst. You need to pass length of the string that you want to copy.

int str_cpy(const char *src, char *dst, size_t n));

str_len

Returns the length of the string.

int str_len(const char *s);

str_cat

Concatenates the given strings. Returns the new length of the src.

int str_cat(char *src, const char *append, size_t src_size, size_t app_size);

str_cmp

Compares the two given strings. If str1 less than str2, returns -1. If str2 less than str1, returns 1. And str1 and str2 are equal, then returns 0.

int str_cmp(const char *str1, const char *str2, size_t str1_size, size_t str2_size);

LICENSE

GNU General Public License v3.0

safe_strn's People

Contributors

gozeloglu avatar

Stargazers

 avatar

Watchers

 avatar  avatar

safe_strn's Issues

Add string type

Add a string type with struct and handle the string operations in functions.

Possible struct definition:

struct sf_str {
    char *str;
    size_t alloc_len;
    size_t len;
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.